2017-04-04 70 views
0

我想绘制我的数据的KNN图形,但是我不断收到这个我不能弄清楚的错误。为多个特征绘制KNN分类器图形

clf = neighbors.KNeighborsClassifier(k, weights=weights) 
AttributeError: 'list' object has no attribute 'KNeighborsClassifier' 

下面我重视我的代码(不包括进口):

data_df = pd.DataFrame.from_csv("fvectors.csv") 
X = np.array(data_df[features].values) 

data_df2 = pd.DataFrame.from_csv("fvectors.csv") 
y = np.array(data_df2[features1].replace("Circle",0).replace("Equilateral Triangle",1) 
      .replace("Right Angle Triangle",2).replace("Acute Triangle",3) 
      .replace("Obtuse Triangle",4).replace("Square",5) 
      .replace("Parallelogram",6).replace("Rectangle",7) 
      .replace("Pentagon",8).replace("Seal",9).values.tolist()) 

#step size in the mesh 
h = .02 

#Create color maps 
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF']) 
cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF']) 

for weights in ['uniform', 'distance']: 
    #we create an instance of Neighbours Classifier and fit the data. 
    clf = neighbors.KNeighborsClassifier(k, weights=weights) 
    clf.fit(X, y) 

    #Plot the decision boundary. For that, we will assign a color to each 
    #point in the mesh [x_min, x_max]x[y_min, y_max]. 
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), 
         np.arange(y_min, y_max, h)) 
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) 

    #Put the result into a color plot 
    Z = Z.reshape(xx.shape) 
    plt.figure() 
    plt.pcolormesh(xx, yy, Z, cmap=cmap_light) 

    #Plot also the training points 
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold) 
    plt.xlim(xx.min(), xx.max()) 
    plt.ylim(yy.min(), yy.max()) 
    plt.title("3-Class classification (k = %i, weights = '%s')" % (k)) 

plt.show() 

And my fvectors.csv file looks like this:

另外:

features = ["Number of Sides", "Standard Deviation of Number of Sides/Perimeter", 
     "Standard Deviation of the Angles", "Largest Angle"] 


features1 = ["Label"] 

有人能看到什么即时通讯做错了,或者有是否有其他出类拔萃的错误?

+0

什么是“邻居”?你没有在代码中定义它。确保你已经阅读了如何创建[mcve]。 – ImportanceOfBeingErnest

回答

0

问题似乎与导入。试试:

from sklearn.neighbors import KNeighborsClassifier

然后直接使用KNeighborsClassifier。

+0

我收到此错误: 'ValueError:查询数据维度必须与训练数据维度匹配' –

+1

您是认真的,@ Thom?已知的一半代码在未知线路中给出了一个ValueError,但是您希望有人找出原因? – ImportanceOfBeingErnest

+0

Thom,检查你的输入(X和Y)的形状打印他们的形状。 (打印X.shape应该工作),然后尝试重塑方法来修复它们的形状。这是我最好的猜测,否则给我们更多的信息。 – Fujii