ValueError: Ожидаемый двумерный массив, вместо него получен одномерный массив: array = [0.31818181 0.40082645 0.49173555 ... 0.14049587 0.14876033 0.15289256] - PullRequest
1 голос
/ 30 апреля 2019
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: Ожидаемый 2D-массив, вместо него получен 1D-массив: array = [0.31818181 0.40082645 0.49173555... 0,14049587 0,14876033 0,15289256].Измените ваши данные, используя array.reshape (-1, 1), если ваши данные имеют один элемент, или array.reshape (1, -1), если он содержит один образец.

Как я могу это исправить?

1 Ответ

1 голос
/ 30 апреля 2019

Ваш код имеет проблему здесь:

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 ,:] в 2d массив

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...