[英]Fit function in sklearn KNN model does not work: 'n_neighbors does not take <class 'float'> value, enter integer value'
I am working on a project where I want to use the KNN model from the sklearn library.我正在做一个项目,我想在其中使用 sklearn 库中的 KNN model。 I simplified the original problem to the following one.
我将原始问题简化为以下问题。 X1, X2 and X3 are the predicters to assign each row to a category (Y- variable), which is either 1 or 2. I used a online instruction and all went fine untill I use the fit function. Here is the code:
X1、X2 和 X3 是将每一行分配给类别(Y 变量)的预测变量,类别为 1 或 2。我使用了在线说明,一切正常,直到我使用拟合 function。这是代码:
#Importing necessary libraries
import pandas as pd
import numpy as np
#Imports for KNN models
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
#Imports for testing the model
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score
#Import the data file
data = pd.read_csv("/content/drive/MyDrive/Python/Colab Notebooks/Onlyinttest.csv")
#Split data
X = data.loc[:,['X1','X2','X3']]
Y = data.loc[:,'Y']
X_train, X_test, Y_train, Y_test = train_test_split(X,Y, random_state=0, test_size=0.2)
#Determine k by using sqrt
import math
k = math.sqrt(len(Y_test))
print(k)
#Make k uneven
k = k-1
#KNN Model
classifer = KNeighborsClassifier(n_neighbors=k, p=2,metric='euclidean')
classifer.fit(X_train,Y_train)
The error: ''n_neighbors does not take <class 'float'> value, enter integer value''错误:''n_neighbors 不采用 <class 'float'> 值,输入 integer 值''
All the data from the original data were float data, but in every online example I read the algorithm also works with float data, so I do not understand this error.原始数据中的所有数据都是浮点数据,但在我阅读的每个在线示例中,该算法也适用于浮点数据,所以我不明白这个错误。
To double check I created the csv used in the code above (Onlyinttest.csv), which only contains int values, but still the some error occures: CSV data为了仔细检查,我创建了上面代码 (Onlyinttest.csv) 中使用的 csv,它只包含 int 值,但仍然出现一些错误: CSV 数据
Can someone help me out here?有人可以帮我吗?
In your example, k
is a float, not an integer. The n_neighbors
value in KNeighborsClassifier(n_neighbors=k, p=2,metric='euclidean')
has to be an integer, not a float.在您的示例中,
k
是浮点数,而不是KNeighborsClassifier(n_neighbors=k, p=2,metric='euclidean')
中的n_neighbors
值必须是 integer,而不是浮点数。
You could convert k into an integer in this example using the math.ceil()
function which return the integer that is equal to or greater than the float value.在此示例中,您可以使用
math.ceil()
function 将 k 转换为 integer,它返回等于或大于浮点值的 integer。 Alternately, you could use the math.floor()
function which will return the integer that less than or equal to the input float.或者,您可以使用
math.floor()
function,它将返回小于或等于输入浮点数的 integer。
For example:例如:
#Determine k by using sqrt
import math
k = math.sqrt(len(Y_test))
print(k)
#Make k uneven
k = k-1
k = math.ceil(k)
print(k) # should now be an integer
print(type(k)) # <class 'int'>
#KNN Model
classifer = KNeighborsClassifier(n_neighbors=k, p=2, metric='euclidean')
classifer.fit(X_train, Y_train)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.