0 KNN精度

时间:2018-07-03 17:16:07

标签: python-3.x precision data-analysis knn

我正在使用KNN建立预测模型。但是在应用模型后,分类报告中的精度为0,并且K的不同值的错误率恒定为1。

下面提到的是代码

    `import numpy as np
     import pandas as pd 
     import seaborn as sns 
     import matplotlib.pyplot as plt
     %matplotlib inline

     df= pd.read_excel("trend.xlsx")
     df = pd.DataFrame(df , index = range(0,18))


     df = df.drop(index= 0)

     for i in range(0,17):
        df.iloc[i,0] = i+2000

     from sklearn.model_selection import train_test_split


      X = df.Year
      X= X[:, np.newaxis]

      y= df.Aus.astype(int)

      X_train, X_test, y_train, y_test = train_test_split(X, y, 
      test_size=0.30, random_state=101)

     from sklearn.linear_model import LinearRegression
     lm = LinearRegression()
     from sklearn.neighbors import KNeighborsRegressor
     knn = KNeighborsRegressor(n_neighbors= 1)

     knn.fit(X_train,y_train)

     pred = knn.predict(X_test)
     pred

     type(y)

     from sklearn.metrics import classification_report,confusion_matrix

     classification_report(y_test,pred)

     error_rate = []


     for i in range(1,12):

       knn = KNeighborsRegressor(n_neighbors=i)
       knn.fit(X_train,y_train)
       pred_i = knn.predict(X_test)
       error_rate.append(np.mean(pred_i != y_test))

     plt.figure(figsize=(10,6))
     plt.plot(range(1,12),error_rate,color='blue', linestyle='dashed', 
     marker='o',
     markerfacecolor='red', markersize=10)
     plt.title('Error Rate vs. K Value')
     plt.xlabel('K')
     plt.ylabel('Error Rate')`

这是分类报告-

          '             precision    recall  f1-score   support\n\n     1736.0       0.00      0.00      0.00         1\n     2035.0       0.00      0.00      0.00         0\n     2069.0       0.00      0.00      0.00         1\n     2091.0       0.00      0.00      0.00         0\n     2116.0       0.00      0.00      0.00         1\n     2297.0       0.00      0.00      0.00         0\n     2350.0       0.00      0.00      0.00         1\n     2590.0       0.00      0.00      0.00         0\n     2596.0       0.00      0.00      0.00         1\n     2597.0       0.00      0.00      0.00         1\n\navg / total       0.00      0.00      0.00         6\n'

和混淆矩阵-

   array([[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]], dtype=int64)

错误率曲线是一条平线

0 个答案:

没有答案