这个需求主要是k-means聚类算法,再加一个优化k-means++算法,这些直接调库就可以
from sklearn import metrics
import warnings
warnings.filterwarnings("ignore")
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import jieba
test=pd.read_excel('data/国家非文化遗产1.xlsx')
test["内容"]=test["内容"].astype('str')
cuttxt = lambda x: " ".join(jieba.lcut(x))
test["clean"] = test["内容"].apply(cuttxt)
stpwrdpath ="data/停用词.txt"
with open(stpwrdpath, 'rb') as fp:
stopword = fp.read().decode('utf-8')
stpwrdlst = stopword.splitlines()
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
vectorizer=CountVectorizer(stop_words=stpwrdlst)
transformer=TfidfTransformer()
tfidf=transformer.fit_transform(vectorizer.fit_transform(test["clean"]))
weight=tfidf.toarray()
word=vectorizer.get_feature_names()
from sklearn.cluster import KMeans
wcss = []
for i in range(1,11):
kmeans = KMeans(n_clusters = i, random_state = 42)
kmeans.fit(weight)
wcss.append(kmeans.inertia_)
plt.plot(range(1, 11), wcss)
plt.title('K-means算法')
plt.xlabel('Number of clusters')
plt.show()
D:\sofewore\anaconda\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 31639 missing from current font.
font.set_text(s, 0.0, flags=flags)
D:\sofewore\anaconda\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 27861 missing from current font.
font.set_text(s, 0.0, flags=flags)
D:\sofewore\anaconda\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 31639 missing from current font.
font.set_text(s, 0, flags=flags)
D:\sofewore\anaconda\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 27861 missing from current font.
font.set_text(s, 0, flags=flags)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/db19b3ebca922302409996f707bbe41a.png)
kmeans = KMeans(n_clusters = 2, random_state = 42)
y=kmeans.fit_predict(weight)
data={
'类别':y
}
test1=pd.DataFrame(data)
test1["类别"].value_counts()
housetype=test1["类别"].value_counts()
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
asd,sdf = plt.subplots(1,1,dpi=100)
housetype.head(10).plot(kind='bar',x='housetype',y='size',title='类别数量分布',ax=sdf)
plt.legend(['数量'])
plt.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c22519d8fb614fd51ae79b20430ed1a3.png)
from sklearn.cluster import KMeans
wcss = []
for i in range(1,11):
kmeans = KMeans(n_clusters = i, init = 'k-means++', random_state = 42)
kmeans.fit(weight)
wcss.append(kmeans.inertia_)
plt.plot(range(1, 11), wcss)
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7b5fd7caa839819788cf187625617090.png)
kmeans = KMeans(n_clusters = 2, init = 'k-means++', random_state = 42)
y=kmeans.fit_predict(weight)
data={
'类别':y
}
test1=pd.DataFrame(data)
test1["类别"].value_counts()
1 2692
0 458
Name: 类别, dtype: int64
housetype=test1["类别"].value_counts()
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
asd,sdf = plt.subplots(1,1,dpi=100)
housetype.head(10).plot(kind='bar',x='housetype',y='size',title='类别数量分布',ax=sdf)
plt.legend(['数量'])
plt.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7793cb7ceb828ba9adf6482a0f668f05.png)
plt.scatter(weight[y == 0, 0], weight[y == 0, 1], s = 100, c = 'red', label = '0')
plt.scatter(weight[y == 1, 0], weight[y == 1, 1], s = 100, c = 'blue', label = '1')
plt.title('Clusters of customers')
plt.xlabel('Annual Income (k$)')
plt.ylabel('Spending Score (1-100)')
plt.legend()
plt.show()