def knnPredictor(df):
dataTrainX, dataTrainY, dataTestX, dataTestY = sample(df)
corelationCoefficiantDictionary = {}
corelationCoefficiantArray = []
for k in range(1, 200, 1):
knnModel = KNeighborsRegressor(n_neighbors=k)
knnModel.fit(dataTrainX, dataTrainY)
knnpredicted = knnModel.predict(dataTestX)
corelationCoefficient = pearsonr(dataTestY, knnpredicted)
corelationCoefficiantDictionary[k] = corelationCoefficient[0]
corelationCoefficiantArray.append(corelationCoefficient[0])
# plotter.plot(corelationCoefficiantArray)
bestK = max(corelationCoefficiantDictionary, key=corelationCoefficiantDictionary.get)
knnModelBest = KNeighborsRegressor(n_neighbors=bestK)
knnModelBest.fit(dataTrainX, dataTrainY)
print("K = ")
print(bestK)
print("Corelation Coeff:")
print(corelationCoefficiantDictionary[bestK])
knnpredictedBest = knnModelBest.predict(dataTestX)
fig, ax = plotter.subplots()
corelationCoefficient = pearsonr(dataTestY, knnpredictedBest)
print(corelationCoefficient[0])
ax.set_ylabel('Predicted KNN Weekly')
ax.scatter(dataTestY, knnpredictedBest)
ax.set_xlabel('Measured')
plotter.show()
评论列表
文章目录