使用Sklearn进行Python代码机器学习(Iris Flower)时出错
我刚开始学习机器学习的基础知识,我偶然发现了这个错误 在机器学习中的虹膜花问题中,我面临着一个错误,我无法理解为什么会出现这个错误。 你能解释一下我为什么要面对这样的错误吗 代码 错误使用Sklearn进行Python代码机器学习(Iris Flower)时出错,python,machine-learning,scikit-learn,Python,Machine Learning,Scikit Learn,我刚开始学习机器学习的基础知识,我偶然发现了这个错误 在机器学习中的虹膜花问题中,我面临着一个错误,我无法理解为什么会出现这个错误。 你能解释一下我为什么要面对这样的错误吗 代码 错误 回溯(最近一次呼叫最后一次): 文件“MachineLearning2.py”,第29行,在 clf=clf.fit(列车数据、列车目标) 文件“C:\Anacondas\lib\site packages\sklearn\tree\tree.py”,第790行,在fit中 X_idx_排序=X_idx_排序)
回溯(最近一次呼叫最后一次):
文件“MachineLearning2.py”,第29行,在
clf=clf.fit(列车数据、列车目标)
文件“C:\Anacondas\lib\site packages\sklearn\tree\tree.py”,第790行,在fit中
X_idx_排序=X_idx_排序)
文件“C:\Anacondas\lib\site packages\sklearn\tree\tree.py”,第116行,在fit中
X=检查数组(X,dtype=dtype,accept\u sparse=“csc”)
文件“C:\Anacondas\lib\site packages\sklearn\utils\validation.py”,第441行,在check\u数组中
“如果它包含单个样本。”。格式(数组))
ValueError:应为2D数组,而应为1D数组:
数组=[3.5 1.3999998 0.2 4.90000013.1.3999998
0.2 4.69999981 3.20000005 1.29999995 0.2 4.5999999
3.0999999 1.5 0.2 5. 3.5999999 1.39999998
0.2 5.4000001 3.9000001 1.70000005 0.40000001 4.5999999
3.4000001 1.39999998 0.30000001 5. 3.4000001 1.5 0.2
4.4000001 2.9000001 1.39999998 0.2 4.9000001 3.0999999
1.5 0.1 5.4000001 3.70000005 1.5 0.2
4.80000019 3.4000001 1.60000002 0.2 4.80000019 3. 0.1
4.30000019 3. 1.10000002 0.1 5.80000019 4.
1.20000005 0.2 5.69999981 4.4000001 1.5 0.40000001
5.4000001 3.9000001 1.29999995 0.40000001 5.0999999 3.5
1.39999998 0.30000001 5.69999981 3.79999995 1.70000005 0.30000001
5.0999999 3.79999995 1.5 0.30000001 5.4000001 3.4000001
1.70000005 0.2 5.0999999 3.70000005 1.5 0.40000001
4.5999999 3.5999999 1. 0.2 5.0999999 3.29999995
1.70000005 0.5 4.80000019 3.4000001 1.89999998 0.2 3.
1.60000002 0.2 5. 3.4000001 1.60000002 0.40000001
5.19999981 3.5 1.5 0.2 5.19999981 3.4000001
1.39999998 0.2 4.69999981 3.20000005 1.60000002 0.2
4.80000019 3.0999999 1.60000002 0.2 5.4000001 3.4000001
1.5 0.40000001 5.19999981 4.0999999 1.5 0.1 5.5
4.19999981 1.39999998 0.2 4.9000001 3.0999999 1.5 0.1
5. 3.20000005 1.20000005 0.2 5.5 3.5
1.29999995 0.2 4.9000001 3.0999999 1.5 0.1
4.4000001 3. 1.29999995 0.2 5.0999999 3.4000001
1.5 0.2 5. 3.5 1.29999995 0.30000001
4.5 2.29999995 1.29999995 0.30000001 4.4000001 3.20000005
1.29999995 0.2 5. 3.5 1.60000002 0.60000002
5.0999999 3.79999995 1.89999998 0.40000001 4.80000019 3.
1.39999998 0.30000001 5.0999999 3.79999995 1.60000002 0.2
4.5999999 3.20000005 1.39999998 0.2 5.30000019 3.70000005
1.5 0.2 5. 3.29999995 1.39999998 0.2 7.
3.20000005 4.69999981 1.39999998 6.4000001 3.20000005 4.5 1.5
6.9000001 3.0999999 4.9000001 1.5 5.5 2.29999995
4. 1.29999995 6.5 2.79999995 4.5999999 1.5
5.69999981 2.79999995 4.5 1.29999995 6.30000019 3.29999995
4.69999981 1.60000002 4.9000001 2.4000001 3.29999995 1. 6.5999999
2.9000001 4.5999999 1.29999995 5.19999981 2.70000005 3.9000001
1.39999998 5. 2. 3.5 1. 5.9000001 3.
4.19999981 1.5 6. 2.20000005 4. 1. 6.0999999
2.9000001 4.69999981 1.39999998 5.5999999 2.9000001 3.5999999
1.29999995 6.69999981 3.0999999 4.4000001 1.39999998 5.5999999 3.
4.5 1.5 5.80000019 2.70000005 4.0999999 1.
6.19999981 2.20000005 4.5 1.5 5.5999999 2.5
3.9000001 1.10000002 5.9000001 3.20000005 4.80000019 1.79999995
6.0999999 2.79999995 4. 1.29999995 6.30000019 2.5
4.9000001 1.5 6.0999999 2.79999995 4.69999981 1.20000005
6.4000001 2.9000001 4.30000019 1.29999995 6.5999999 3. 4.4000001
1.39999998 6.80000019 2.79999995 4.80000019 1.39999998 6.69999981
3. 5. 1.70000005 6. 2.9000001 4.5 1.5
5.69999981 2.5999999 3.5 1. 5.5 2.4000001
3.79999995 1.10000002 5.5 2.4000001 3.70000005 1.
5.80000019 2.70000005 3.9000001 1.20000005 6. 2.70000005
5.0999999 1.60000002 5.4000001 3. 4.5 1.5 6.
3.4000001 4.5 1.60000002 6.69999981 3.0999999 4.69999981
1.5 6.30000019 2.29999995 4.4000001 1.29999995 5.5999999 3.
4.0999999 1.29999995 5.5 2.5 4. 1.29999995
5.5 2.5999999 4.4000001 1.20000005 6.0999999 3. 4.5999999
1.39999998 5.80000019 2.5999999 4. 1.20000005 5.
2.29999995 3.29999995 1. 5.5999999 2.70000005 4.19999981
1.29999995 5.69999981 3. 4.19999981 1.20000005 5.69999981
2.9000001 4.19999981 1.29999995 6.19999981 2.9000001 4.30000019
1.29999995 5.0999999 2.5 3. 1.10000002 5.69999981
2.79999995 4.0999999 1.29999995 6.30000019 3.29999995 6. 2.5
5.80000019 2.70000005 5.0999999 1.89999998 7.0999999 3. 5.9000001
2.0999999 6.30000019 2.9000001 5.5999999 1.79999995 6.5 3.
5.80000019 2.20000005 7.5999999 3. 6.5999999 2.0999999
4.9000001 2.5 4.5 1.70000005 7.30000019 2.9000001
6.30000019 1.79999995 6.69999981 2.5 5.80000019 1.79999995
七点一九九九九九八一三
from sklearn.datasets import load_iris
from sklearn import tree
import numpy as np
#load data
iris= load_iris()
#position of the start of the flower names or indexes
test_index=[0,50,100]
#Training data
train_target = np.delete(iris.target,test_index)
train_data=np.delete(iris.data,test_index)
test_target=iris.target[test_index]
test_data=iris.data[test_index]
clf = tree.DecisionTreeClassifier()
clf=clf.fit(train_data , train_target)
print(test_target)
Traceback (most recent call last):
File "MachineLearning2.py", line 29, in <module>
clf=clf.fit(train_data , train_target)
File "C:\Anacondas\lib\site-packages\sklearn\tree\tree.py", line 790, in fit
X_idx_sorted=X_idx_sorted)
File "C:\Anacondas\lib\site-packages\sklearn\tree\tree.py", line 116, in fit
X = check_array(X, dtype=DTYPE, accept_sparse="csc")
File "C:\Anacondas\lib\site-packages\sklearn\utils\validation.py", line 441, in check_array
"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[ 3.5 1.39999998 0.2 4.9000001 3. 1.39999998
0.2 4.69999981 3.20000005 1.29999995 0.2 4.5999999
3.0999999 1.5 0.2 5. 3.5999999 1.39999998
0.2 5.4000001 3.9000001 1.70000005 0.40000001 4.5999999
3.4000001 1.39999998 0.30000001 5. 3.4000001 1.5 0.2
4.4000001 2.9000001 1.39999998 0.2 4.9000001 3.0999999
1.5 0.1 5.4000001 3.70000005 1.5 0.2
4.80000019 3.4000001 1.60000002 0.2 4.80000019 3. 0.1
4.30000019 3. 1.10000002 0.1 5.80000019 4.
1.20000005 0.2 5.69999981 4.4000001 1.5 0.40000001
5.4000001 3.9000001 1.29999995 0.40000001 5.0999999 3.5
1.39999998 0.30000001 5.69999981 3.79999995 1.70000005 0.30000001
5.0999999 3.79999995 1.5 0.30000001 5.4000001 3.4000001
1.70000005 0.2 5.0999999 3.70000005 1.5 0.40000001
4.5999999 3.5999999 1. 0.2 5.0999999 3.29999995
1.70000005 0.5 4.80000019 3.4000001 1.89999998 0.2 3.
1.60000002 0.2 5. 3.4000001 1.60000002 0.40000001
5.19999981 3.5 1.5 0.2 5.19999981 3.4000001
1.39999998 0.2 4.69999981 3.20000005 1.60000002 0.2
4.80000019 3.0999999 1.60000002 0.2 5.4000001 3.4000001
1.5 0.40000001 5.19999981 4.0999999 1.5 0.1 5.5
4.19999981 1.39999998 0.2 4.9000001 3.0999999 1.5 0.1
5. 3.20000005 1.20000005 0.2 5.5 3.5
1.29999995 0.2 4.9000001 3.0999999 1.5 0.1
4.4000001 3. 1.29999995 0.2 5.0999999 3.4000001
1.5 0.2 5. 3.5 1.29999995 0.30000001
4.5 2.29999995 1.29999995 0.30000001 4.4000001 3.20000005
1.29999995 0.2 5. 3.5 1.60000002 0.60000002
5.0999999 3.79999995 1.89999998 0.40000001 4.80000019 3.
1.39999998 0.30000001 5.0999999 3.79999995 1.60000002 0.2
4.5999999 3.20000005 1.39999998 0.2 5.30000019 3.70000005
1.5 0.2 5. 3.29999995 1.39999998 0.2 7.
3.20000005 4.69999981 1.39999998 6.4000001 3.20000005 4.5 1.5
6.9000001 3.0999999 4.9000001 1.5 5.5 2.29999995
4. 1.29999995 6.5 2.79999995 4.5999999 1.5
5.69999981 2.79999995 4.5 1.29999995 6.30000019 3.29999995
4.69999981 1.60000002 4.9000001 2.4000001 3.29999995 1. 6.5999999
2.9000001 4.5999999 1.29999995 5.19999981 2.70000005 3.9000001
1.39999998 5. 2. 3.5 1. 5.9000001 3.
4.19999981 1.5 6. 2.20000005 4. 1. 6.0999999
2.9000001 4.69999981 1.39999998 5.5999999 2.9000001 3.5999999
1.29999995 6.69999981 3.0999999 4.4000001 1.39999998 5.5999999 3.
4.5 1.5 5.80000019 2.70000005 4.0999999 1.
6.19999981 2.20000005 4.5 1.5 5.5999999 2.5
3.9000001 1.10000002 5.9000001 3.20000005 4.80000019 1.79999995
6.0999999 2.79999995 4. 1.29999995 6.30000019 2.5
4.9000001 1.5 6.0999999 2.79999995 4.69999981 1.20000005
6.4000001 2.9000001 4.30000019 1.29999995 6.5999999 3. 4.4000001
1.39999998 6.80000019 2.79999995 4.80000019 1.39999998 6.69999981
3. 5. 1.70000005 6. 2.9000001 4.5 1.5
5.69999981 2.5999999 3.5 1. 5.5 2.4000001
3.79999995 1.10000002 5.5 2.4000001 3.70000005 1.
5.80000019 2.70000005 3.9000001 1.20000005 6. 2.70000005
5.0999999 1.60000002 5.4000001 3. 4.5 1.5 6.
3.4000001 4.5 1.60000002 6.69999981 3.0999999 4.69999981
1.5 6.30000019 2.29999995 4.4000001 1.29999995 5.5999999 3.
4.0999999 1.29999995 5.5 2.5 4. 1.29999995
5.5 2.5999999 4.4000001 1.20000005 6.0999999 3. 4.5999999
1.39999998 5.80000019 2.5999999 4. 1.20000005 5.
2.29999995 3.29999995 1. 5.5999999 2.70000005 4.19999981
1.29999995 5.69999981 3. 4.19999981 1.20000005 5.69999981
2.9000001 4.19999981 1.29999995 6.19999981 2.9000001 4.30000019
1.29999995 5.0999999 2.5 3. 1.10000002 5.69999981
2.79999995 4.0999999 1.29999995 6.30000019 3.29999995 6. 2.5
5.80000019 2.70000005 5.0999999 1.89999998 7.0999999 3. 5.9000001
2.0999999 6.30000019 2.9000001 5.5999999 1.79999995 6.5 3.
5.80000019 2.20000005 7.5999999 3. 6.5999999 2.0999999
4.9000001 2.5 4.5 1.70000005 7.30000019 2.9000001
6.30000019 1.79999995 6.69999981 2.5 5.80000019 1.79999995
7.19999981 3.5999999 6.0999999 2.5 6.5 3.20000005
5.0999999 2. 6.4000001 2.70000005 5.30000019 1.89999998
6.80000019 3. 5.5 2.0999999 5.69999981 2.5 5.
2. 5.80000019 2.79999995 5.0999999 2.4000001 6.4000001
3.20000005 5.30000019 2.29999995 6.5 3. 5.5
1.79999995 7.69999981 3.79999995 6.69999981 2.20000005 7.69999981
2.5999999 6.9000001 2.29999995 6. 2.20000005 5. 1.5
6.9000001 3.20000005 5.69999981 2.29999995 5.5999999 2.79999995
4.9000001 2. 7.69999981 2.79999995 6.69999981 2.
6.30000019 2.70000005 4.9000001 1.79999995 6.69999981 3.29999995
5.69999981 2.0999999 7.19999981 3.20000005 6. 1.79999995
6.19999981 2.79999995 4.80000019 1.79999995 6.0999999 3. 4.9000001
1.79999995 6.4000001 2.79999995 5.5999999 2.0999999 7.19999981
3. 5.80000019 1.60000002 7.4000001 2.79999995 6.0999999
1.89999998 7.9000001 3.79999995 6.4000001 2. 6.4000001
2.79999995 5.5999999 2.20000005 6.30000019 2.79999995 5.0999999
1.5 6.0999999 2.5999999 5.5999999 1.39999998 7.69999981
3. 6.0999999 2.29999995 6.30000019 3.4000001 5.5999999
2.4000001 6.4000001 3.0999999 5.5 1.79999995 6. 3.
4.80000019 1.79999995 6.9000001 3.0999999 5.4000001 2.0999999
6.69999981 3.0999999 5.5999999 2.4000001 6.9000001 3.0999999
5.0999999 2.29999995 5.80000019 2.70000005 5.0999999 1.89999998
6.80000019 3.20000005 5.9000001 2.29999995 6.69999981 3.29999995
5.69999981 2.5 6.69999981 3. 5.19999981 2.29999995
6.30000019 2.5 5. 1.89999998 6.5 3.
5.19999981 2. 6.19999981 3.4000001 5.4000001 2.29999995
5.9000001 3. 5.0999999 1.79999995].
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.
>>>
ValueError: Expected 2D array, got 1D array instead
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.
train_data=np.delete(iris.data,test_index)
train_data=np.delete(iris.data,test_index, axis=0)
The axis along which to delete the subarray defined by obj.
If axis is None, obj is applied to the flattened array.