如果我训练一个 KNeighborsRegressor(通过 scikit-learn),然后想将它的预测与目标变量进行比较,我可以这样做:#Initiate modelknn = neighbors.KNeighborsRegressor(n_neighbors=8)#Define independent and target variablesX = df[['var1', 'var2', 'var3']]Y = df['target']#fit the model and store the predictionsknn.fit(X, Y)predicted = knn.predict(X).ravel()如果我要比较它们,我可以看到这个模型远非完美,这是预期的:compare = pd.DataFrame(predicted,Y).reset_index()compare.columns=['Y', 'predicted']compare.head(3)返回:+------+-----------+| Y | predicted |+------+-----------+| 985 | 2596 |+------+-----------+| 801 | 2464 |+------+-----------+| 1349 | 1907 |+------+-----------+如果我做完全相同的事情,除了按距离加权邻居,predict() 函数将准确地返回目标变量。#Initiate modelknn_dist = neighbors.KNeighborsRegressor(n_neighbors=8, weights='distance')#fit the model and store the predictionsknn_dist.fit(X, Y)predicted2 = knn_dist.predict(X).ravel()compare = pd.DataFrame(predicted2,Y).reset_index()compare.columns=['Y', 'predicted2']compare.head(3)返回相同的列:+------+------------+| Y | predicted2 |+------+------------+| 985 | 985 |+------+------------+| 801 | 801 |+------+------------+| 1349 | 1349 |+------+------------+我知道预测器并不像这暗示的那样完美,并且可以通过交叉验证来证明:score_knn = cross_val_score(knn, X, Y, cv=ShuffleSplit(test_size=0.1))print(score_knn.mean())>>>>0.5306705590672681我究竟做错了什么?根据请求,这是我的数据框中相关列的前五行:| ID | var1 | var2 | var3 | target ||----|----------|----------|----------|--------|| 1 | 0.363625 | 0.805833 | 0.160446 | 985 || 2 | 0.353739 | 0.696087 | 0.248539 | 801 || 3 | 0.189405 | 0.437273 | 0.248309 | 1349 || 4 | 0.212122 | 0.590435 | 0.160296 | 1562 || 5 | 0.22927 | 0.436957 | 0.1869 | 1600 |
添加回答
举报
0/150
提交
取消