`array.array()` 不接受关键字参数

huangapple go评论68阅读模式
英文:

array.array() takes no keyword arguments

问题

在FARS数据集上尝试进行k均值聚类时,出现了array.array()不接受关键字参数的错误,不理解为什么或如何更正它。

最初出现int32错误,我将其更改为np.int32,问题得到解决,但现在出现了array错误,无法弄清楚如何修复。

完整错误信息:

TypeError: array.array()不接受关键字参数

代码

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.]])

希望这能帮助您解决问题。

英文:

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.]])

答案1

得分: 0

不要将您的代码直接复制/粘贴,因为某些行是前一行的输出。这些行不应用作语句:

>>> kmeans.labels_
array([1, 1, 1, 0, 0, 0], dtype=np.int32) # 这是 kmeans.labels_ 的输出

>>> kmeans.predict([[0, 0], [12, 3]])
array([1, 0], dtype=np.int32) # 这是 kmeans.predict(...) 的输出

>>> kmeans.cluster_centers_
array([[10., 2.], # 这是 kmean.cluster_centers_ 的输出
[ 1., 2.]])


代码应为:

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_


用法:

>>> labels
array([1, 1, 1, 0, 0, 0], dtype=int32)

>>> preds
array([1, 0], dtype=int32)

>>> centroids
array([[10., 2.],
[ 1., 2.]])


[文档][1]中提到:

> 我们的符号约定是 `$` 表示 shell 提示,而 `>>>` 表示 Python 解释器提示:


  [1]: https://scikit-learn.org/stable/tutorial/basic/tutorial.html#loading-an-example-dataset
英文:

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:

huangapple
  • 本文由 发表于 2023年6月8日 20:07:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定