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
我收到的错误是,
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.
如果有人可以帮我解决这些问题,那对我来说会很有帮助!!
答案 0 :(得分:9)
from sklearn.model_selection import train_test_split
X = mydata[['script']]
y = pd.factorize(mydata['label'].values)[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)
答案 1 :(得分:0)
SELECT party_name,party_electorial_sign,party_flag
FROM parties
where party_name IN
(SELECT n.CANDIDATE_PARTY
FROM voters V, na_candidates N
WHERE voter_cnic= 'nic' AND V.NA_CONSTITUENCY=N.NA_CONSTITUENCY)
我有以下代码。整形算子不是就位算子。因此,我们必须像上面给出的那样在重塑后用值替换它的值。
答案 2 :(得分:0)
一个自动重塑它的简单解决方案是 而不是使用:
X=dataset.iloc[:, 0].values
您可以使用:
X=dataset.iloc[:, :-1].values
也就是说,如果您只有两列,而您正试图获得第一列 代码获取除最后一列之外的所有列
答案 3 :(得分:0)
假设最初你有,
X = dataset.iloc[:, 1].values
表示您有包含所有行的第一列。 现在制作如下
X = dataset.iloc[:, 1:2].values
这里的 1:2 表示 [1,2) 类似于上限形成。
答案 4 :(得分:0)
轻松选择列使其成为 2 d。
x_train = mydata[['script']]
y_train = mydata[['label']]