【发布时间】:2020-11-08 23:08:22
【问题描述】:
我知道我应该在某处添加average=None,但我真的不知道,目标变量是一组数字:
from sklearn.model_selection import train_test_split
trainset, testset = train_test_split(df, test_size=0.2, random_state=0)
def preprocessing(df):
X = df.drop('log_price', axis=1)
y = df['log_price']
print(y.value_counts())
return X, y
X_train, y_train = preprocessing(trainset)
X_test, y_test = preprocessing(testset)
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.model_selection import learning_curve
def evaluation(model):
model.fit(X_train, y_train)
ypred = model.predict(X_test)
print(confusion_matrix(y_test, ypred))
print(classification_report(y_test, ypred))
N, train_score, val_score = learning_curve(model, X_train, y_train,
cv=4, scoring='f1',
train_sizes=np.linspace(0.1, 1, 10))
plt.figure(figsize=(12, 8))
plt.plot(N, train_score.mean(axis=1), label='train score')
plt.plot(N, val_score.mean(axis=1), label='validation score')
plt.legend()
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
preprocessor = make_pipeline(PolynomialFeatures(2, include_bias=False), SelectKBest(f_classif, k=10))
KNN = make_pipeline(preprocessor, StandardScaler(), KNeighborsClassifier())
dict_of_models = {
'KNN': KNN
}
for name, model in dict_of_models.items():
print(name)
evaluation(model)
我收到此错误:
ValueError: Target is multiclass but average='binary'. Please choose another average setting, one of [None, 'micro', 'macro', 'weighted'].
谢谢。
【问题讨论】:
-
请提供错误回溯。第一个猜测:如果要设置其选项,则需要提供
learning_curve的scoring参数而不是字符串f1。 -
Traceback: pastebin.com/yhPRtmAH 我无法将它添加到我的原始帖子中,因为它太长了。请注意,他的代码与二进制目标完美配合。如果我删除 learning_curve 我不会收到错误...
-
查看链接中的混淆矩阵...您在此处使用分类而不是回归是否有特殊原因?
标签: python pandas scikit-learn