ValueError:找到样本数不一致的输入变量:[100,7]

时间:2017-12-05 19:22:45

标签: python pandas

目前正在尝试让程序根据动物园数据库中包含的功能猜测动物。  当我运行此代码时,它会收到错误''ValueError:Found sample variables with samples number of samples:[100,7]''。它显示错误发生在这一行''X_train,X_validation,Y_train,Y_validation = model_selection.train_test_split(X,Y,test_size = testing_size,random_state = seed)''

def zoo_that():
    zoodatabase = pd.read_csv('C:/Users/Quentin Clayton/Documents/Class work/Quarter 9/Data Analytics Project I/Final Project for Project Course/zoo.csv', header = 0)
    classtypes = pd.read_csv('C:/Users/Quentin Clayton/Documents/Class work/Quarter 9/Data Analytics Project I/Final Project for Project Course/class.csv',header = 0,)
    zoodatabase_v2 = zoodatabase.merge(classtypes,how = 'left',left_on = 'class_type',right_on = 'Class_Number')
    X = zoodatabase_v2.loc[:, 'hair':'catsize']
    Y = zoodatabase_v2.loc[:, 'class_type':'Class_Number']
    testing_size = 0.2
    seed = 2
    X_train, X_validation, Y_train, Y_validation = model_selection.train_test_split(X, Y, test_size=testing_size, random_state=seed)

    # Test options and evaluation metric|
    scoring = 'accuracy'

    models = []
    models.append(('LR', LogisticRegression()))
    models.append(('LDA', LinearDiscriminantAnalysis()))
    models.append(('KNN', KNeighborsClassifier()))
    models.append(('CART', DecisionTreeClassifier()))
    models.append(('NB', GaussianNB()))
    models.append(('SVM', SVC()))
    # evaluate each model in turn
    results = []
    names = []
    for name, model in models:
        kfold = model_selection.KFold(n_splits=4, random_state=seed)
        cv_results = model_selection.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring)
        results.append(cv_results)
        names.append(name)
        msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
        print(msg)

    # Make predictions on validation dataset
    LR = LogisticRegression()
    LR.fit(X_train, Y_train)
    predictions = LR.predict(X_validation)
    print("Accuracy score\n",accuracy_score(Y_validation, predictions))
    print("Confusion matrix\n",confusion_matrix(Y_validation, predictions))
    print("Final Report\n",classification_report(Y_validation, predictions))
    print(scoring)
zoo_that()
Traceback (most recent call last):

  File "<ipython-input-20-396e334d1676>", line 1, in <module>
    zoo_that()

  File "C:/Users/Quentin Clayton/Documents/Class work/Quarter 9/Data Analytics Project I/Final Project for Project Course/Final Submission.py", line 35, in zoo_that
    X_train, X_validation, Y_train, Y_validation = model_selection.train_test_split(X, Y, test_size=testing_size, random_state=seed)

  File "D:\Anaconda\lib\site-packages\sklearn\model_selection\_split.py", line 2031, in train_test_split
    arrays = indexable(*arrays)

  File "D:\Anaconda\lib\site-packages\sklearn\utils\validation.py", line 229, in indexable
    check_consistent_length(*result)

  File "D:\Anaconda\lib\site-packages\sklearn\utils\validation.py", line 204, in check_consistent_length
    " samples: %r" % [int(l) for l in lengths])

ValueError: Found input variables with inconsistent numbers of samples: [100, 7]

文件图片   [1]:https://i.stack.imgur.com/OaJmO.jpg [这是Csv类] [1]   [2]:https://i.stack.imgur.com/FL0by.jpg [这是Zoo Csv] [2]

1 个答案:

答案 0 :(得分:0)

问题出在这一部分:

X = zoodatabase_v2.loc[1:101,'hair':'catsize']
Y = zoodatabase_v2.loc[0:6,'Class_Type':'Animal_Names']

X是长度为100(1:101)的DataFrame,Y是长度为6的系列。要训练模型(监督学习),您需要为所有输入记录提供目标标签。此外,您需要提供单个目标标签,而目前看起来好像您给出2('Animal_Names'和'Class_Type')。如果删除子集,它应该可以工作。即。

X = zoodatabase_v2.loc[:, 'hair':'catsize']
Y = zoodatabase_v2.loc[:, 'Class_Type']

应该可以正常工作。