0

My target variable is Survived and has only 0 and 1 values, have my following code resulted in error:

kfold = StratifiedKFold(n_splits=8,shuffle=True, random_state=42)

rs = 15
clrs = []

clrs.append(AdaBoostClassifier(random_state=rs))
clrs.append(GradientBoostingClassifier(random_state=rs))
clrs.append(RandomForestClassifier(random_state=rs))
clrs.append(ExtraTreesClassifier(random_state = rs))
clrs.append(DecisionTreeClassifier(random_state = rs))

cv_results = []
for clr in clrs :
    cv_results.append(cross_val_score(clr, X_train, y_train , scoring = 'accuracy', cv = kfold, n_jobs=-1))

Here is error:

ValueError: Supported target types are: ('binary', 'multiclass'). Got 'unknown' instead.

And here is my target:

enter image description here

1 Answers1

0

You showed of screenshot of df, and I assume your y_train comes from the Survived column. It is dtype object, meaning it could be a string or mixed. Hence you get that error. You need to convert it into an integer:

For example:

df = pd.DataFrame(np.random.uniform(0,1,(100,3)))
df['Survived'] = np.random.choice(['0','1'],100)
X_train = df.drop('Survived',axis=1)
y_train = df['Survived'].astype(int)

kfold = StratifiedKFold(n_splits=8,shuffle=True, random_state=42)

rs = 15
clrs = []

clrs.append(AdaBoostClassifier(random_state=rs))
clrs.append(GradientBoostingClassifier(random_state=rs))
clrs.append(RandomForestClassifier(random_state=rs))

cv_results = []
for clr in clrs :
    cv_results.append(cross_val_score(clr, X_train, y_train,cv = kfold))
StupidWolf
  • 45,075
  • 17
  • 40
  • 72