简体   繁体   English

Scikit-learn不适用于KNN上的字符串值

[英]Scikit-learn does not work with string value on KNN

I'm using Scikit learn to do a K-Nearest Neigbour Classification: 我正在使用Scikit学习做一个K-Nearest Neigbour分类:

from sklearn.neighbors import KNeighborsClassifier

model=KNeighborsClassifier() 
model.fit(train_input,train_labels)

If I print my data: 如果我打印我的数据:

print("train_input:")
print(train_input.iloc[0])
print("\n")
print("train_labels:")
print(train_labels.iloc[0]) 

I get this: 我明白了:

train_input:
PassengerId                          1
Pclass                               3
Name           Braund, Mr. Owen Harris
Sex                               male
Age                                 22
SibSp                                1
Parch                                0
Ticket                       A/5 21171
Fare                              7.25
Cabin                              NaN
Embarked                             S
Name: 0, dtype: object


train_labels:
0

The code fails with this error: 代码失败并出现此错误:

ValueError                                Traceback (most recent call last)
<ipython-input-21-1f18eec1e602> in <module>()
     63 
     64 model=KNeighborsClassifier()
---> 65 model.fit(train_input,train_labels)
ValueError: could not convert string to float: 'Q'

So, does the KNN algorithm not work with String values? 那么,KNN算法不能使用String值吗?

How can I modify my data such that it fits the KNN implementation in Scikit-Learn? 如何修改我的数据以使其符合Scikit-Learn中的KNN实现?

For nominal String features, consider one hot encoding: http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html . 对于名义String功能,请考虑一个热门编码: http//scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html

For ordinal String features, consider label encoding (with a sensible ordering based on your understanding of the feature): http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html . 对于序数String功能,请考虑标签编码(根据您对该功能的理解,使用合理的排序): http//scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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