Pycharm 中的无监督聚类
Unsupervised clustering in Pycharm
我在 pycharm 中为无监督聚类编写了一个脚本。我想知道为什么 pycharm 会给我这个错误。我知道缺少参数,但我应该如何更正它?
Traceback (most recent call last):
File "F:/Python/Projects/Kmeans.Clustering.py", line 36, in <module>
y_Kmeans = KMeans.fit_predict(points)
TypeError: fit_predict() missing 1 required positional argument: 'X'
这是我的代码:
import tensorflow
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import make_blobs
## 1. Generate Dataset
dataset = make_blobs(n_samples=200,
centers=4,
n_features=2,
cluster_std=1.6,
random_state=50)
print(dataset)
points = dataset[0] # First array which is the Points ( X,Y Coordinates)
#print(points)
# 2. Import KMEANS
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=4) # As specified for Center of Cluster
# 3. Begin the Lloyd algorithm for This Dataset
kmeans.fit(points)
# 4. Plot the dataSet
plt.figure(1)
plt.scatter(dataset[0][:,0],dataset[0][:,1])
plt.show()
# 5. Create and Print Clusters
Clusters = kmeans.cluster_centers_ # Which will give us the Centers of the Clusters which are found by the Kmeans Algorithm
print(Clusters)
y_Kmeans = KMeans.fit_predict(points)
plt.scatter(points[y_Kmeans==0,0],points[y_Kmeans==0,1],s=50,edgecolors='red')
plt.show()
你忘记了括号:
y_Kmeans = KMeans().fit_predict(points)
我在 pycharm 中为无监督聚类编写了一个脚本。我想知道为什么 pycharm 会给我这个错误。我知道缺少参数,但我应该如何更正它?
Traceback (most recent call last):
File "F:/Python/Projects/Kmeans.Clustering.py", line 36, in <module>
y_Kmeans = KMeans.fit_predict(points)
TypeError: fit_predict() missing 1 required positional argument: 'X'
这是我的代码:
import tensorflow
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import make_blobs
## 1. Generate Dataset
dataset = make_blobs(n_samples=200,
centers=4,
n_features=2,
cluster_std=1.6,
random_state=50)
print(dataset)
points = dataset[0] # First array which is the Points ( X,Y Coordinates)
#print(points)
# 2. Import KMEANS
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=4) # As specified for Center of Cluster
# 3. Begin the Lloyd algorithm for This Dataset
kmeans.fit(points)
# 4. Plot the dataSet
plt.figure(1)
plt.scatter(dataset[0][:,0],dataset[0][:,1])
plt.show()
# 5. Create and Print Clusters
Clusters = kmeans.cluster_centers_ # Which will give us the Centers of the Clusters which are found by the Kmeans Algorithm
print(Clusters)
y_Kmeans = KMeans.fit_predict(points)
plt.scatter(points[y_Kmeans==0,0],points[y_Kmeans==0,1],s=50,edgecolors='red')
plt.show()
你忘记了括号:
y_Kmeans = KMeans().fit_predict(points)