ValueError: not enough values to unpack (expected 4, got 1), on a dictionary

ValueError: not enough values to unpack (expected 4, got 1), on a dictionary

我试着寻找答案,但我不知道为什么它期待 4...所以我找不到解决方案。我正在尝试制作一个脚本,该脚本将迭代模型,然后根据它们在 iris 数据集和我应用的特征缩放方面的表现创建一个图。目前这是我收到错误的代码段。

代码:


models = {
    "Logistic Regression": LogisticRegression(),
    "Decision Tree": DecisionTreeClassifier(max_leaf_nodes=3),
    "Random Forest": RandomForestClassifier(max_depth=3),
    "svm_model" : SVC(kernel='linear')
}

def evaluate_model(model, dataset):
    x_train, x_test, y_train, y_test = data
    model.fit(x_train, y_train)
    pred = model.predict(x_test)
    return accuracy_score(pred, y_test)

for model_name, model in models.items():
        model_score = evaluate_model(model, dataset)
        #dataset_scores[model_name] = model_score
        
       # model_scores_for_datasets[dataset_name] = dataset_scores

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-531-35544d14a669> in <module>
      7 
      8 for model_name, model in models.items():
----> 9         model_score = evaluate_model(model, dataset)
     10         #dataset_scores[model_name] = model_score
     11 

<ipython-input-521-f26300591060> in evaluate_model(model, dataset)
     13 
     14 def evaluate_model(model, dataset):
---> 15     x_train, x_test, y_train, y_test = data
     16     model.fit(x_train, y_train)
     17     pred = model.predict(x_test)

ValueError: not enough values to unpack (expected 4, got 1)

从你的问题来看,你的 data 变量的类型或形状是什么不清楚。您是否正确拆分了数据以便 return 测试和训练拆分?

例如,假设您有一些配对数据:

x = np.arange(1, 25).reshape(12, 2)
y = np.array([0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0])

您需要使用 sklearn 中的 train_test_split 将其拆分为:

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y)

但是您的 data 变量只包含一个可迭代项,因此无法将其解压缩为 4 个变量 x_train, x_testy_trainy_test。您基本上可能会生成与以下相同的错误:

x_train, x_test, y_train, y_test = [1]

此外,您的 evaluate_model 函数不使用 dataset 输入,而是使用全局 data 解压缩。因此替换该行以使函数按预期工作。

def evaluate_model(model, dataset):
    x_train, x_test, y_train, y_test = dataset
    model.fit(x_train, y_train)
    pred = model.predict(x_test)
    return accuracy_score(pred, y_test)

引发 ValueError 是因为您尝试解包的值多于 data 变量中存在的总数。因此,在您的情况下,错误告诉我们 data 中只有一个值,但您正在尝试解压缩四个值。

确保 data 变量中有四个值要解包。

无法准确回答,因为我们不知道 data 变量中的内容。一个可能的示例是如何拆分 sklearn.

中的数据
from sklearn import model_selection

x_train, x_valid, y_train, y_valid = model_selection.train_test_split(X, y, test_size=0.2, random_state=0)

Edit1:添加您可以参考的代码片段

# imports
from sklearn import datasets

iris_data = datasets.load_iris() # get the iris data directly from sklearn

#put the dataset into a pandas DF using the feature names as columnsç
#rename the column name so they don't include the '(cm)'
#add 2 columns one with the target and another with the target_names 
df = pd.DataFrame(dataset['data'], columns=dataset['feature_names'])
df.columns = ['sepal length', 'sepal width', 'petal length', 'petal width']
df['target'] = dataset['target']
df['class'] = dataset.target_names[dataset.target]

dummy = pd.get_dummies(df_iris, columns=["target"])

X = df_iris[['sepal length','sepal width']].to_numpy() #only selects specified column and converts to numpy arrays
Y = df_iris.target.to_numpy() # target to numpy 

#Split the data into train and test
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, train_size = 0.8, random_state = 42)

#normalize the dataset
#create and fit the scaler object on the training data
# do the training