Issue
Trying to do a k means clustering on FARS dataset, getting the array.array() takes no keyword arguments error and don't understand why or how to correct it.
Initially was getting errors for int32 which I changed to np.int32 and that corrected but now I am getting the array error and can't figure out how to fix it.
Full error:
TypeError Traceback (most recent call last)
<ipython-input-11-29801179ab6c> in <cell line: 5>()
3 kmeans = KMeans(n_clusters=2, random_state=0, n_init="auto").fit(X)
4 kmeans.labels_
----> 5 array([1, 1, 1, 0, 0, 0], dtype=np.int32)
6 kmeans.predict([[0, 0], [12, 3]])
7 array([1, 0], dtype=np.int32)
TypeError: array.array() takes no keyword arguments
Code:
X = np.array([[1, 2], [1, 4], [1, 0],
... [10, 2], [10, 4], [10, 0]])
kmeans = KMeans(n_clusters=2, random_state=0, n_init="auto").fit(X)
kmeans.labels_
array([1, 1, 1, 0, 0, 0], dtype=np.int32)
kmeans.predict([[0, 0], [12, 3]])
array([1, 0], dtype=np.int32)
kmeans.cluster_centers_
array([[10., 2.],
[ 1., 2.]])
Solution
Don't copy/paste your code as it, some lines are the output of the previous line. This lines are not intended to be used as statements:
>>> kmeans.labels_
array([1, 1, 1, 0, 0, 0], dtype=np.int32) # this is the output of kmeans.labels_
>>> kmeans.predict([[0, 0], [12, 3]])
array([1, 0], dtype=np.int32) # this is the output of kmeans.predict(...)
>>> kmeans.cluster_centers_
array([[10., 2.], # this is the output of kmean.cluster_centers_
[ 1., 2.]])
The code should be:
import numpy as np
from sklearn.cluster import KMeans
X = np.array([[1, 2], [1, 4], [1, 0],
[10, 2], [10, 4], [10, 0]])
kmeans = KMeans(n_clusters=2, random_state=0, n_init="auto").fit(X)
labels = kmeans.labels_
preds = kmeans.predict([[0, 0], [12, 3]])
centroids = kmeans.cluster_centers_
Usage:
>>> labels
array([1, 1, 1, 0, 0, 0], dtype=int32)
>>> preds
array([1, 0], dtype=int32)
>>> centroids
array([[10., 2.],
[ 1., 2.]])
The documentation said:
Our notational convention is that
$
denotes the shell prompt while>>>
denotes the Python interpreter prompt:
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.