我正在尝试将Knearest Neigbors模型适合我的数据。但是,我收到此错误:
log.Println(reflect.Indirect(newitem))
log.Println(newitem.Elem())
这是我的Knearest Neigbhors算法的代码:
ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
以及数据框的前五行:
def knn_train_test_new(training_col, target_col, df):
np.random.seed(1)
df = df.loc[np.random.permutation(len(df))]
# shuffled_index = np.random.permutation(df.index)
# df = df.reindex(shuffled_index)
train_df = df.iloc[0:150] #training set has 75% of the data
test_df = df.iloc[150:] #test set has 25% of the data
k = [5]
rmse = {}
for k_val in k:
model = KNeighborsRegressor(n_neighbors = k_val)
model.fit(train_df[training_col], train_df[target_col])
predictions = model.predict(test_df[training_col])
mse = mean_squared_error(test_df[target_col], predictions)
rmse[k_val] = (mse ** 0.5)
return rmse
two_features = ["width", "wheel-base"]
rmse_val = knn_train_test(two_features, 'price', numeric_cars)
当我使用shuffled_index(我已经评论过)而不是np.random.permutation时,我没有收到此错误。我不清楚两者之间的区别。
完整的错误跟踪:
numeric_cars.head()