[英]Not able to predict the value after creating a classifier and setting up the training data for machine learning. Some error is showing
I am trying to use the online dataset for machine learning.. following is the code... 我正在尝试使用在线数据集进行机器学习..以下是代码...
import numpy as np
import urllib
import pandas
from sklearn import tree
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/tic-tac-toe/tic-tac-toe.data"
raw_data = urllib.request.urlopen(url)
dataset = np.loadtxt(raw_data, delimiter=",")
X = list(dataset[0:8])
y = list(dataset[9])
clf=tree.DecisionTreeClassifier()
clf.fit(X,y)
print(clf.predict(['x','x','x','x','o','o','o','x','o']))
and following is the error it is showing.. 接下来是它显示的错误。
Traceback (most recent call last):
File "4.py", line 13, in <module>
clf.fit(X,y)
File "/home/shravilp/anaconda3/lib/python3.6/site-packages/sklearn/tree/tree.py", line 739, in fit
X_idx_sorted=X_idx_sorted)
File "/home/shravilp/anaconda3/lib/python3.6/site-packages/sklearn/tree/tree.py", line 146, in fit
check_classification_targets(y)
File "/home/shravilp/anaconda3/lib/python3.6/site-packages/sklearn/utils/multiclass.py", line 172, in check_classification_targets
raise ValueError("Unknown label type: %r" % y_type)
ValueError: Unknown label type: 'continuous'
I'm sorry, this is actually a comment but due to my reputation I can't post comments yet :( 抱歉,这实际上是一条评论,但是由于我的声誉,我还不能发表评论:(
Unknown label type means that it thinks y has elements of the type continuous. 未知的标签类型意味着它认为y具有连续类型的元素。 You have to encode them in order for it to work.
您必须对其进行编码才能正常工作。 You could use the pandas method factorize ( http://pandas.pydata.org/pandas-docs/stable/generated/pandas.factorize.html ) or LabelEncoder from sklearn ( http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html )
您可以使用sklearn的pandas方法factorize( http://pandas.pydata.org/pandas-docs/stable/generate/pandas.factorize.html )或LabelEncoder( http://scikit-learn.org/stable/modules /generation/sklearn.preprocessing.LabelEncoder.html )
Once I had the same problem as you do and even after y was encoded it still showed that error, so what I did was passing y.astype(int) to the fit method. 一旦我遇到了与您相同的问题,即使将y编码后,它仍然显示该错误,所以我要做的就是将y.astype(int)传递给fit方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.