![](/img/trans.png)
[英]ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). for my knn model
[英]KNN ValueError: Input contains NaN, infinity or a value too large for dtype('float64')
這是我的代碼,應該是一個簡單的回歸算法。 數據集約有500個樣本,每個樣本都有12個因子。 我雖然收到此錯誤:
ValueError:輸入包含NaN,無窮大或dtype('float64')太大的值。
碼:
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))
您是否在數據集中檢查了NaN(不是數字)2? 例如。 與dataset2.isnull().values.any()
?
可能導致錯誤的另一件事:您需要像對待訓練數據一樣對待樣本:
knn.predict(dataset2.loc[:, 1:12].values)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.