如何在sklearn/python中修复"ValueError:预期的2D数组,改为获得1D数组"?

Kar*_*raj 1 python arrays numpy scikit-learn

在这里.我刚从机器学习开始,用一个简单的例子来尝试和学习.因此,我想通过使用分类器根据文件类型对磁盘中的文件进行分类.我写的代码是,

import sklearn
import numpy as np


#Importing a local data set from the desktop
import pandas as pd
mydata = pd.read_csv('file_format.csv',skipinitialspace=True)
print mydata


x_train = mydata.script
y_train = mydata.label

#print x_train
#print y_train
x_test = mydata.script

from sklearn import tree
classi = tree.DecisionTreeClassifier()

classi.fit(x_train, y_train)

predictions = classi.predict(x_test)
print predictions
Run Code Online (Sandbox Code Playgroud)

而我收到的错误是,

  script  class  div   label
0       5      6    7    html
1       0      0    0  python
2       1      1    1     csv
Traceback (most recent call last):
  File "newtest.py", line 21, in <module>
  classi.fit(x_train, y_train)
  File "/home/initiouser2/.local/lib/python2.7/site-
packages/sklearn/tree/tree.py", line 790, in fit
    X_idx_sorted=X_idx_sorted)
  File "/home/initiouser2/.local/lib/python2.7/site-
packages/sklearn/tree/tree.py", line 116, in fit
    X = check_array(X, dtype=DTYPE, accept_sparse="csc")
  File "/home/initiouser2/.local/lib/python2.7/site-
packages/sklearn/utils/validation.py", line 410, in check_array
    "if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[ 5.  0.  1.].
Reshape your data either using array.reshape(-1, 1) if your data has a 
single feature or array.reshape(1, -1) if it contains a single sample.
Run Code Online (Sandbox Code Playgroud)

如果有人可以帮我提供代码,那对我来说会很有帮助!!

cs9*_*s95 10

  1. 提取列,并将数据拆分为有效的列车和测试部分.不要使用您的训练数据进行测试 - 这会导致对分类器强度的估计不准确
  2. 我建议对你的标签进行分解,这样你就可以处理整数.这更容易.
  3. 将输入传递给分类器时,传递2D数组,而不是1D数组.这涉及将维度增加一个.

from sklearn.model_selection import train_test_split

# X.shape should be (N, M) where M >= 1
X = mydata[['script']]  
# y.shape should be (N, 1)
y = mydata['label'] 
# perform label encoding if "label" contains strings
# y = pd.factorize(mydata['label'])[0].reshape(-1, 1) 
X_train, X_test, y_train, y_test = train_test_split(
                      X, y, test_size=0.33, random_state=42)
...

clf.fit(X_train, y_train) 
print(clf.score(X_test, y_test))
Run Code Online (Sandbox Code Playgroud)