0

I am trying to plot a KNN graph for my data but i keep getting this error which i cant figure out.

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

Below i have attached my code(excluding imports):

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:

Also:

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


features1 = ["Label"]

Can anyone see what im doing wrong, or if there are any other errors that stand out?

Thom Elliott
  • 197
  • 3
  • 6
  • 18

1 Answers1

0

The problem seems to be with the import. Try:

from sklearn.neighbors import KNeighborsClassifier

And then just use KNeighborsClassifier directly.

Fujii
  • 101
  • 2
  • 3
  • I get this error: `ValueError: query data dimension must match training data dimension` – Thom Elliott Apr 04 '17 at 20:15
  • 1
    Are you serious,@Thom ? A half known code gives a ValueError in an unknown line with an unknown traceback but you expect someone to find out why???? – ImportanceOfBeingErnest Apr 04 '17 at 20:53
  • Thom, check the shape of your input (X and Y) printing their shape. (print X.shape should work) and then try the reshape method to fix their shape. That's my best guess, otherwise give us more information. – Fujii Apr 05 '17 at 19:03