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()
Using sklearn.cluster to calculate KMeans but as the title states getting an error in the line kmeans = KMeans(n_clusters = 2).fit(arr)
According to the parameters listed here Kmeans n_clusters is correct. What's weird is I have a similar program which works, only difference is that I declared the data set stored in "arr" instead of reading from a file
arr = np.array([[1,2], [1,4], [1,0], [4,2], [4,4], [4,0]])
use debug code below, you overwrite the Kmeans Class with Kmeans function, be careful in naming functions.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.