【问题标题】:ValueError: Expected 2D array, got 1D array instead: array=[0.31818181 0.40082645 0.49173555 ... 0.14049587 0.14876033 0.15289256]ValueError:预期的二维数组,得到一维数组:array=[0.31818181 0.40082645 0.49173555 ... 0.14049587 0.14876033 0.15289256]
【发布时间】:2019-04-29 22:53:59
【问题描述】:
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
        data, target, test_size=0.25, random_state=0)
from sklearn.model_selection import cross_val_score, KFold
from scipy.stats import sem

def evaluate_cross_validation(clf, X, y, K):
    # create a k-fold cross validation iterator
    cv = KFold( K , shuffle=True, random_state=0)
    # by default the score used is the one returned by score method of the estimator (accuracy)
    scores = cross_val_score(clf, X, y, cv=cv)
    print (scores)
    print ("Mean score: {0:.3f} (+/-{1:.3f})".format(
        np.mean(scores), sem(scores)))
evaluate_cross_validation(svc_1, X_train, y_train, 5)
from sklearn import metrics

def train_and_evaluate(clf, X_train, X_test, y_train, y_test):

    clf.fit(X_train, y_train)

    print ("Accuracy on training set:")
    print (clf.score(X_train, y_train))
    print ("Accuracy on testing set:")
    print (clf.score(X_test, y_test))

    y_pred = clf.predict(X_test)

    print ("Classification Report:")
    print (metrics.classification_report(y_test, y_pred))
    print ("Confusion Matrix:")
    print (metrics.confusion_matrix(y_test, y_pred))
train_and_evaluate(svc_1, X_train, X_test, y_train, y_test)

random_image_button = Button(description="New image!")

def display_face_and_prediction(b):
    index = randint(0, 400)
    face = faces.images[index]
    display_face(face)
    print("this person is smiling: {0}".format(svc_1.predict(faces.data[index, :])==1))

random_image_button.on_click(display_face_and_prediction)
display(random_image_button)
display_face_and_prediction(0)

当我从random_image_button = Button(description="New image!") 开始运行代码时,它给了我以下错误:

ValueError:预期二维数组,得到一维数组:array=[0.31818181 0.40082645 0.49173555 ... 0.14049587 0.14876033 0.15289256]。如果您的数据只有一个,请使用 array.reshape(-1, 1) 重塑您的数据 如果包含单个样本,则为 feature 或 array.reshape(1, -1)。

我该如何解决这个问题?

【问题讨论】:

  • 您对错误有什么不明白的地方?
  • @gmds 我该如何解决这个错误?
  • 它确实要求您将其重塑为 2D。你试过吗?
  • @gmds ,我对 X_test、X_train、y_train 和 y_test 进行了重塑,但没有成功
  • @gmds,我这样做了:X_train= X_train.reshape(-1, 1) y_train= y_train.reshape(-1, 1) X_test = X_test.reshape(-1, 1)跨度>

标签: python python-3.x machine-learning jupyter-notebook anaconda


【解决方案1】:

你的代码在这里有问题:

def display_face_and_prediction(b):
index = randint(0, 400)
face = faces.images[index]
display_face(face)
print("this person is smiling: {0}".format(svc_1.predict(faces.data[index, :])==1))

您的模型需要使用 2d 数组进行预测,但您适合 faces.data[index,:] 您可以将 faces.data[index,:] 重塑为二维数组

【讨论】:

  • 我这样做了:print("this person is smiling: {0}".format(svc_1.predict(faces.data[index, :])==1).reshape(-1,1)),仍然没有工作
  • 哦,现在成功了,我用了这个:print("this person is smiling: {0}".format(svc_1.predict(faces.data[index, :].reshape(1,-1))==1)),非常感谢@joyzaza
猜你喜欢
  • 1970-01-01
  • 2021-03-01
  • 2019-08-17
  • 2021-02-23
  • 2018-06-06
  • 2021-09-19
  • 2019-10-07
  • 2019-03-28
  • 2021-04-16
相关资源
最近更新 更多