我想知道为什么 r2_score 在 train_test_split 和 pipeline cross_val_score 之间有很大不同?我怀疑这是因为模型可以通过管道中的 CountVectorizer() 看到未知单词。但是基于 Pipeline 的概念,CountVectorizer() 应该只适用于按 cross_val 分割的训练集?
pipe=Pipeline([('Vect', CountVectorizer()), ('rf', RandomForestRegressor(random_state=1)) ])
X_train, X_test, y_train, y_test=train_test_split(df['X'], df['price'], shuffle= False, test_size=0.5)
reg=pipe.fit(X_train,y_train )
mypred= reg.predict(X_test)
r2_score(mypred, y_test)
# result is -0.2
cross_val_score(pipe,df['X'], df['price'],cv=2)
# result is about 0.3
最佳答案
r2_score(mypred, y_test)
是错误的。
您需要提供真实值作为第一个输入,并提供预测值作为第二个输入。将其更正为:
r2_score(y_test, mypred)
然后检查结果。
关于python - 为什么 r2_score 在 train_test_split 和 pipeline cross_val_score 之间有很大不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54729109/