【问题标题】:y should be a 1d array, got an array of shape (234, 3) instead. how to solve this?y 应该是一维数组,取而代之的是一个形状为 (234, 3) 的数组。如何解决这个问题?
【发布时间】:2022-01-17 10:38:26
【问题描述】:

'y 应该是一维数组,取而代之的是一个形状为 (234, 3) 的数组。'

data = dataset.iloc[:,:-1].values
label = dataset.iloc[:,-1].values
 
labelencoder = LabelEncoder()


from sklearn.svm import SVC
classifier = SVC(kernel="linear",random_state=0)
classifier.fit(X_train,y_train)

y_pred=classifier.predict(X_test)

详细代码,知道我能做什么

label = label_binarize(label, classes=[0, 1, 2])
n_classes=label.shape[1]
X_train,X_test,y_train,y_test=train_test_split(data_full,label,test_size=0.2,random_state=0)

classif= OneVsRestClassifier(classifier)
y_score = classif.fit(X_train, y_train).decision_function(X_test)

'这个错误'

from sklearn.svm import SVC
model = SVC(kernel="linear",random_state=0)
model.fit(X_train,y_train) //this error

linkcode

【问题讨论】:

  • 预计y_train 中只有一列,而您有三列
  • 如何更改为 1 列?我做 te iloc 的事情,但没有任何改变。同样的错误

标签: python scikit-learn


【解决方案1】:

您的 y(即“标签”变量)必须是一维数组,这意味着它必须只是数据集的 1 列。 但是你为它选择了 3 列。应该是这样的:

Label = dataset["name or index of the column "]

【讨论】:

  • 感谢解决
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 2021-10-15
  • 2021-03-10
  • 1970-01-01
  • 2021-11-05
  • 2021-08-20
相关资源
最近更新 更多