0
点赞
收藏
分享

微信扫一扫

K-mean

1.np.array函数:

2.如何使用scatter函数:

PYthon——plt.scatter各参数详解_yuanCruise的博客-CSDN博客_plt.scatter

3.如何使用Kmeans函数:

sklearn.cluster.KMeans — scikit-learn 1.0.2 documentation

4.关于函数报错:

python的缩进是自动对齐的,输入函数体后按空格 会自动对齐

但是你不按照它的那个自动对齐,自己对齐,看着是一样的,但还是会报错。

5.make_blobs的使用方法:

官网:

sklearn.datasets.make_blobs — scikit-learn 1.2.dev0 documentation

中文:

make_blobs方法的使用_bingbangx的博客-CSDN博客_make_blobs函数

该函数可自动生成一些聚类数据。

6.函数报错:

 setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (2, 100) + inhomogeneous part.

b=make_blobs(n_samples=100,random_state=170)

#将上述改为:
a,b=make_blobs(n_samples=100,random_state=170)
#由此解决

7.自己设定的函数报错:

#try to do better 用函数做
#return clustering labels and kmeans object
def doKmeans(data,k):
    kmeans=KMeans(n_clusters=k,random_state=0)
    label=kmeans.fit_predict(data)
    return label,kmeans

x0_label,kmeans=doKmeans(x0,2)
print(kmeans.cluster_centers_)
print(x0_label)
#以上为第一段

#try lab_1
#generate 100 points with class labels by running the following codes
a,b=make_blobs(n_samples=100,random_state=170)

fig, ax1 = plt.subplots(1)
ax1.scatter(X[:, 0], X[:, 1]
            ,marker='o' #点的形状
            ,s=8 #点的大小
           )
plt.show()

y1,x=doKmeans(b,3)


print('\n check the class label of each point\n')
print(y1)

print('\n checking the dataset\n')
print(x)
#为第二段
报错如下:
ValueError: Expected 2D array, got 1D array instead:
array=[0. 2. 2. 0. 0. 1. 1. 0. 2. 1. 1. 1. 0. 1. 0. 2. 1. 1. 1. 2. 2. 2. 2. 2.
 1. 1. 0. 2. 0. 1. 2. 0. 1. 2. 1. 0. 0. 0. 0. 0. 2. 0. 2. 0. 0. 1. 2. 2.
 0. 1. 2. 2. 1. 2. 0. 1. 2. 1. 0. 0. 1. 0. 0. 2. 0. 2. 2. 1. 1. 1. 2. 0.
 1. 0. 0. 0. 2. 1. 1. 1. 1. 1. 2. 1. 0. 0. 1. 2. 2. 1. 2. 2. 1. 0. 2. 2.
 0. 0. 0. 2.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

错误这样解决:

#try lab_1
#generate 100 points with class labels by running the following codes
X,y = make_blobs(n_samples=100,n_features=2,centers=None,cluster_std=1.0,center_box=(-10.0,10,0),shuffle=True,random_state=170)
#cluster = KMeans(n_clusters=3,random_state=0).fit(X)

plt.scatter(X[:, 0], X[:, 1]
            #,marker='o' #点的形状
           # ,s=8 #点的大小
           )
plt.show()


def doKmeans1(data,k):
    kmeans=KMeans(n_clusters=k,random_state=0).fit(data)
    label=kmeans.fit_predict(data)
    return label,kmeans

y1,kmeans=doKmeans1(X,3)


print('\n check the class label of each point\n')
print(kmeans.cluster_centers_)

print('\n checking the dataset\n')
print(y1)

9.关于python中 x[:,0]和x[:,1] 理解和实例解析

(5条消息) python中 x[:,0]和x[:,1] 理解和实例解析_jobschu的博客-CSDN博客_x[:,0]

上面那句相当于用数组的x轴,y轴生成了散点图

举报

相关推荐

0 条评论