太多值无法解包(预期 3)

too many values to unpack (expected 3)

使用 python 将数据拆分为训练和测试时,出现以下错误

" too many values to unpack (expected 3)"

这是我的代码:

from sklearn.model_selection import train_test_split
X_train, X_test, y_train = train_test_split(features,prices, test_size=0.2, random_state=10)
print("Training and testing split was succesful")

这是预期的输出:'Training and testing split was successful'

看起来你错过了 y_test。

试试这个:

X_train, X_test, y_train, y_test = train_test_split(features,prices, test_size=0.2, random_state=10) 

这是sklearn中train_test_split的文档:sklearn.model_selection.train_test_split.

这里是 return:

splitting : list, length=2 * len(arrays)

List containing train-test split of inputs.

如果您输入功能和价格,则意味着您输入了两个输入,每个输入将分为训练和测试两部分。所以为了获得它们,你应该使用X_train, X_test, y_train, y_test,但是你错过了最后一个参数。

也许您可以这样更正您的代码:

from sklearn.model_selection import train_test_split 

X_train, X_test, y_train, y_test = train_test_split(features,prices, test_size=0.2, random_state=10) 
print("Training and testing split was successful")