해결된 질문
작성
·
240
0
col_id = ['id']
col_num = ['Age','Work_Experience','Family_Size']
col_cat = ['Gender','Ever_Married','Graduated','Profession','Spending_Score','Var_1']
col_y = ['Segmentation']
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
train[col_num] = scaler.fit_transform(train[col_num])
test[col_num] = scaler.transform(test[col_num])
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
for col in col_cat:
train[col] = le.fit_transform(train[col])
test[col] = le.transform(test[col])
from sklearn.model_selection import train_test_split
X_tr, X_val, y_tr, y_val = train_test_split(train[col_cat + col_num], train['Segmentation'], test_size = 0.3, random_state = 123 )
수업내용을 제가 편한 방식으로 조금 바꿔봤는데 이런 식으로 스케일링이랑 인코딩 진행해도 문제없을까요?
아래 내용으로 계속 진행했을때 결과는 나오는데 제대로 한게 맞는지 잘 모르겠습니다...
from sklearn.ensemble import RandomForestClassifier
RF = RandomForestClassifier(n_estimators=300, max_depth =9 ,random_state = 123)
RF.fit(X_tr, y_tr)
pred = RF.predict(X_val)
import lightgbm as lgb
lgb = lgb.LGBMClassifier(learning_rate = 0.02, n_estimators = 400, max_depth = 5, random_state=123)
lgb.fit(X_tr, y_tr)
pred = lgb.predict(X_val)
from sklearn.metrics import f1_score
print(f1_score(y_val, pred, average = 'macro'))
pred = RF.predict(test[col_cat + col_num])
pred
submit = pd.DataFrame({
'ID': test.ID,
'Segmentation': pred
})
submit.to_csv("result.csv", index=False)