![](/img/trans.png)
[英]Hyperparameter Optimisation for Supervised classification Algorithms in SCI-KIT learn?
[英]Python Sci-Kit Learn : Multilabel Classification ValueError: could not convert string to float:
我正在嘗試使用sci-kit進行多標記分類學習0.17我的數據看起來像
訓練
Col1 Col2
asd dfgfg [1,2,3]
poioi oiopiop [4]
測試
Col1
asdas gwergwger
rgrgh hrhrh
我的代碼到目前為止
import numpy as np
from sklearn import svm, datasets
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier
def getLabels():
traindf = pickle.load(open("train.pkl","rb"))
X = traindf['Col1']
y = traindf['Col2']
# Binarize the output
from sklearn.preprocessing import MultiLabelBinarizer
y=MultiLabelBinarizer().fit_transform(y)
random_state = np.random.RandomState(0)
# Split into training and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5,
random_state=random_state)
# Run classifier
from sklearn import svm, datasets
classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True,
random_state=random_state))
y_score = classifier.fit(X_train, y_train).decision_function(X_test)
但現在我明白了
ValueError: could not convert string to float: <value of Col1 here>
上
y_score = classifier.fit(X_train, y_train).decision_function(X_test)
我是否也必須將X二進制化? 為什么我需要將X維度轉換為浮點數?
是的,您必須將X轉換為數字表示(不是必需的二進制)以及y。 那是因為所有機器學習方法都在數字矩陣上運行。
怎么做到這一點? 如果Col1中的每個樣本都可以包含不同的單詞(即它代表一些文本) - 您可以使用CountVectorizer轉換該列
from sklearn.feature_extraction.text import CountVectorizer
col1 = ["cherry banana", "apple appricote", "cherry apple", "banana apple appricote cherry apple"]
cv = CountVectorizer()
cv.fit_transform(col1)
#<4x4 sparse matrix of type '<class 'numpy.int64'>'
# with 10 stored elements in Compressed Sparse Row format>
cv.fit_transform(col1).toarray()
#array([[0, 0, 1, 1],
# [1, 1, 0, 0],
# [1, 0, 0, 1],
# [2, 1, 1, 1]], dtype=int64)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.