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轴生成了散点图
10.手写数字识别,理解 load_digits库
15.手写数字识别-小数据集(load_digits) - Qing#Ci - 博客园 (cnblogs.com)
11.如何使用subplot
plt.subplot(nrows, ncols, index)
plt.subplot(nrows, ncols, figsize=(8,8))
nrows 与 ncols 表示要划分几行几列的子区域(nrows*nclos表示子图数量),index 的初始值为1,用来选定具体的某个子区域。
(5条消息) fig, ax = plt.subplots(figsize = (a, b))解析 与 plt.subplot()函数解析_哎呦-_-不错的博客-CSDN博客_plt.subplots(figsize
12.根据这个实现了如何根据列添加列的功能
帮助很大很大!!!!谢谢:)
(6条消息) pandas DataFrame 根据其他列新建列并赋值_梦因you而美的博客-CSDN博客_pandas新增一列并赋值
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)