简体   繁体   English

KNN ValueError:输入包含NaN,无穷大或对于dtype('float64')而言太大的值

[英]KNN ValueError: Input contains NaN, infinity or a value too large for dtype('float64')

here is my code, it's supposed to be a simple regression algorithm. 这是我的代码,应该是一个简单的回归算法。 The data set has about 500 samples, each of them have 12 factors. 数据集约有500个样本,每个样本都有12个因子。 I received this error though: 我虽然收到此错误:

ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). ValueError:输入包含NaN,无穷大或dtype('float64')太大的值。

Code: 码:

dataset = pd.read_csv('/Users/chrisrivas/Documents/Andrew     Haines/Datasets/GRD.csv', header=None, sep=',')

#coverts dataset into 2d array of values and seperates target column
#[1st to: last rows, and 1st to: 12th columns ]
samples = dataset.loc[:, 1:12].values
targets = dataset[13].values

print(samples)
print(targets)

#training and testing of dataset
X_train, X_test, y_train, y_test = cross_validation.train_test_split(
samples, targets, test_size=0.35, random_state=0)

knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)

y_pred = knn.predict(X_test)

#calculates accuracy of algorithm    
print("Test set score: {:.2f}%".format(np.mean(y_pred == y_test)*100))

#opens new data for algorithm to make classification predictions 
dataset2 = pd.read_csv('/Users/chrisrivas/Documents/Datasets/GoldRushDataset-41.csv', header=None, sep=',').values

#continues to loop for each sample and their classification prediction
for sample in dataset2:
    prediction = knn.predict([sample])
    print("Prediction: {}".format(prediction))
    print('    ')    

#other format for predictions: all at a time in array
prediction = knn.predict(dataset2)
print("Prediction: {}".format(prediction))

Have you checked for NaNs (not a number) in your dataset2? 您是否在数据集中检查了NaN(不是数字)2? eg. 例如。 with dataset2.isnull().values.any() ? dataset2.isnull().values.any()

Another thing that might be the cause of your error: You need to treat the samples the same way as you treat your training data: 可能导致错误的另一件事:您需要像对待训练数据一样对待样本:

knn.predict(dataset2.loc[:, 1:12].values)

暂无
暂无

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

相关问题 ValueError: 输入包含 NaN、无穷大或对于 dtype('float64') 来说太大的值。 对于我的 knn 模型 - ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). for my knn model ValueError:在预处理数据时,输入包含NaN,无穷大或对于dtype('float64')而言太大的值 - ValueError: Input contains NaN, infinity or a value too large for dtype('float64') while preprocessing Data StandardScaler -ValueError:输入包含NaN,无穷大或对于dtype('float64')而言太大的值 - StandardScaler -ValueError: Input contains NaN, infinity or a value too large for dtype('float64') ValueError:输入包含 NaN、无穷大或对于 scikit-learn 的 dtype('float64') 来说太大的值 - ValueError: Input contains NaN, infinity or a value too large for dtype('float64') with scikit-learn ValueError:输入包含 NaN、无穷大或对于 dtype('float64') 来说太大的值。 在安装时 model - ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). While fitting in model ValueError:使用 sklearn IterativeImputer 时,输入包含 NaN、无穷大或对于 dtype('float64') 来说太大的值 - ValueError: Input contains NaN, infinity or a value too large for dtype('float64'), when using sklearn IterativeImputer ValueError:输入包含 NaN、无穷大或对于线性回归中的 dtype('float64') 而言太大的值 - ValueError: Input contains NaN, infinity or a value too large for dtype('float64') in linear regression 如何解决ValueError:输入包含NaN,无穷大或对于dtype来说太大的值('float64') - How to resolve ValueError: Input contains NaN, infinity or a value too large for dtype('float64') 如何修复 ValueError:输入包含 NaN、无穷大或对于 dtype('float64') 来说太大的值。 错误 - How to fix ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). Error sklearn错误ValueError:输入包含NaN,无穷大或对于dtype('float64')来说太大的值 - sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype('float64')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM