简体   繁体   中英

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. 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 )

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM