簡體   English   中英

創建分類器並設置用於機器學習的訓練數據后,無法預測該值。 顯示一些錯誤

[英]Not able to predict the value after creating a classifier and setting up the training data for machine learning. Some error is showing

我正在嘗試使用在線數據集進行機器學習..以下是代碼...

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']))

接下來是它顯示的錯誤。

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'

抱歉,這實際上是一條評論,但是由於我的聲譽,我還不能發表評論:(

未知的標簽類型意味着它認為y具有連續類型的元素。 您必須對其進行編碼才能正常工作。 您可以使用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

一旦我遇到了與您相同的問題,即使將y編碼后,它仍然顯示該錯誤,所以我要做的就是將y.astype(int)傳遞給fit方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM