![](/img/trans.png)
[英]TypeError: __init__() got an unexpected keyword argument 'n_jobs' and n_clusters
[英]TypeError: KMeans() got an unexpected keyword argument 'n_clusters'
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
def KMeans():
n = 4
data = open("testSet.txt", "r")
nums = []
arr = np.empty((0,2), float)
#Gets dataset from file
for x in data.read().split(' '):
nums.append(float(x))
data.close()
print(nums)
#Stores numbers in a 2D array (X, Y axis)
for x in range(0, len(nums), 2):
arr = np.append(arr, np.array([[nums[x],nums[x+1]]]), axis=0)
print(arr)
kmeans = KMeans(n_clusters = 2).fit(arr)
#Example 2, using make blobs to create random data
X, y = make_blobs(n_samples=13, centers=5)
print("Shape:", X.shape, y.shape)
#Plotting the data
plt.figure(0)
plt.grid(True)
plt.scatter(X[:, 0], X[:, 1])
plt.show()
clf = KMeans(n_clusters=5)
clf.fit(X)
print(clf.labels_)
z = clf.cluster_centers_
print(z)
plt.scatter(X[:,0], X[:,1], clf.labels_)
plt.scatter(z[:,0],z[:,1], c='blue')
plt.show()
KMeans()
使用sklearn.cluster计算KMeans,但标题显示行kmeans = KMeans(n_clusters = 2).fit(arr)
根据此处列出的参数, Kmeans n_clusters是正确的。 奇怪的是我有一个类似的程序可以工作,唯一的区别是我声明了存储在“ arr”中的数据集,而不是从文件中读取
arr = np.array([[1,2], [1,4], [1,0], [4,2], [4,4], [4,0]])
使用下面的调试代码,用Kmeans函数覆盖Kmeans类,在命名函数时要小心。
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
def K_Means():
n = 4
data = open("testSet.txt", "r")
nums = []
arr = np.empty((0, 2), float)
# Gets dataset from file
for x in data.read().split(' '):
nums.append(float(x))
data.close()
print(nums)
# Stores numbers in a 2D array (X, Y axis)
for x in range(0, len(nums), 2):
arr = np.append(arr, np.array([[nums[x], nums[x + 1]]]), axis=0)
print(arr)
kmeans = KMeans(n_clusters=2).fit(arr)
# Example 2, using make blobs to create random data
X, y = make_blobs(n_samples=13, centers=5)
print("Shape:", X.shape, y.shape)
# Plotting the data
plt.figure(0)
plt.grid(True)
plt.scatter(X[:, 0], X[:, 1])
plt.show()
clf = KMeans(n_clusters=2)
clf.fit(X)
print(clf.labels_)
z = clf.cluster_centers_
print(z)
plt.scatter(X[:, 0], X[:, 1], clf.labels_)
plt.scatter(z[:, 0], z[:, 1], c='blue')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.