TypeError:未找到必需参数'object'(pos 1)

时间:2016-07-06 03:35:11

标签: python

我是初学者,在我的代码中遇到错误:

此错误已解决:

Traceback (most recent call last):
  File "/Users/ankitchaudhari/PycharmProjects/Learn/datascience/gg.py", line 26, in <module>
    label = kmeans(data, k)
  File "/Users/ankitchaudhari/PycharmProjects/Learn/datascience/gg.py", line 20, in kmeans
    labels = KMeans(n_clusters=k).fit_predict(data)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sklearn/cluster/k_means_.py", line 830, in fit_predict
    return self.fit(X).labels_
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sklearn/cluster/k_means_.py", line 812, in fit
    X = self._check_fit_data(X)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sklearn/cluster/k_means_.py", line 787, in _check_fit_data
    if X.shape[0] < self.n_clusters:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

但新错误是: 新错误:d = np.array()

TypeError:未找到必需参数'object'(pos 1)

这是我的代码:

import matplotlib.pyplot as plt

import pandas as pd

import numpy as np

from sklearn.cluster import KMeans

data = np.array([[1, 2],
             [5, 8],
             [1.5, 1.8],
             [8, 8],
             [9, 11],
             [1, 0.6],
             [2, 2]])

k = np.array([2, 10])

df = pd.DataFrame(data)

df

def kmeans(data, i):

    labels = KMeans(n_clusters=k).fit_predict(data)
    return labels

sse = 0

for i in k:

    label = kmeans(data, k)
    cluster_mean = df.mean()
    d = np.array([])
    for j in range(len(label)):
        sse += sum(pow((data[j]) - cluster_mean, 2))
        d.append(sse)

plt.plot(d, k)
plt.show()

上一个错误已解决。请帮我解决这个错误。

1 个答案:

答案 0 :(得分:0)

在'for'循环中,第二个参数应为'i'而不是'k':

label = kmeans(data, i)