해결된 질문
작성
·
462
0
df = pd.concat([X_train,y_train['Reached.on.Time_Y.N']], axis=1)
n_train = df.select_dtypes(exclude = object).copy()
c_train = df.select_dtypes(include = object).copy()
n_test = df.select_dtypes(exclude = object).copy()
c_test = df.select_dtypes(include = object).copy()
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
cols = ['Customer_care_calls','Customer_rating', 'Cost_of_the_Product', 'Prior_purchases', 'Discount_offered', 'Weight_in_gms']
n_train[cols] = scaler.fit_transform(n_train[cols])
n_test[cols] = scaler.transform(n_test[cols])
c_train = pd.get_dummies(c_train)
c_test = pd.get_dummies(c_test)
train = pd.concat([n_train, c_train], axis =1)
test = pd.concat([n_test, c_test], axis =1)
train = train.drop('ID', axis =1)
test_id = test.pop('ID')
from sklearn.model_selection import train_test_split
X_tr, X_val, y_tr, y_val = train_test_split(
train.drop('Reached.on.Time_Y.N', axis =1),
train['Reached.on.Time_Y.N'],
test_size = 0.2,
random_state = 2023)
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score
model = RandomForestClassifier(random_state = 2023)
model.fit(X_tr, y_tr)
pred = model.predict_proba(X_val)[:,1]
print(roc_auc_score(y_val, pred))
pred = model.predict_proba(test)[:,1]
submit = pd.DataFrame({
'ID': test_id,
'Reached.on.Time_Y.N': pred
})
submit.to_csv("0000.csv", index = False)
작업형2를 n_train, n_test, c_train, c_test로 나누어서 풀어보는 연습을 하고있는데요
위와 같이 코딩을 했는데 roc_auc_score는 잘 출력이 되지만 아래와 같은 워닝이 뜹니다. 이유를 알 수 있을까요?
0.7289745856473446
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-81-0b0c32327ea7> in <cell line: 38>()
36 print(roc_auc_score(y_val, pred))
37
---> 38 pred = model.predict_proba(test)[:,1]
39 submit = pd.DataFrame({
40 'ID': test_id,
3 frames
/usr/local/lib/python3.10/dist-packages/sklearn/base.py in _check_feature_names(self, X, reset)
479 )
480
--> 481 raise ValueError(message)
482
483 def _validate_data(
ValueError: The feature names should match those that were passed during fit.
Feature names unseen at fit time:
- Reached.on.Time_Y.N
답변 1
0
이건 워닝이 아니고 에러입니다.
에러는 반드시 코드를 수정해야 합니다.
train과 test 컬럼명을 확인해주겠어요? 뭔가 다른 컬럼명이 있는 것 같아요!
df.columns