简体   繁体   English

创建分类器并设置用于机器学习的训练数据后,无法预测该值。 显示一些错误

[英]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.

相关问题 我正在尝试创建一个图像分类器机器学习 model。 在将我的训练数据拟合到管道时,它向我显示了这个错误 - I'm trying to create a image classifier machine learning model. while fitting my training data to pipeline, it's showing me this error 机器学习:代表第一个数据集训练的分类器预测第二个数据集 - Machine Learning: Predict second dataset on behalf of first dataset trained classifier 机器学习算法预测下一个价值 - Machine learning algorithm to predict next value 训练过程中过多的步骤会否影响机器学习的训练过程? - Will excessive steps during training mess up the training process in Machine Learning? Python 加载机器学习后 hstack 和 predict_proba 出错 model - Python Error on hstack and predict_proba after load the machine learning model 使用随机森林分类器训练模型时的值误差 - Value error when training model with randomforest classifier 在生产中训练机器学习 - Training Machine Learning in Production Python-机器学习:从数组列表创建训练和测试集 - Python - machine learning: Creating a training and test set from a list of arrays 机器学习随机森林分类器 - Machine learning random forest classifier 评估机器学习文本分类器 - Evaluate Machine Learning Text Classifier
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM