当前位置:网站首页>多元聚类分析
多元聚类分析
2022-07-06 08:49:00 【亦是远方】
一、代码
import pandas as pd
from pandas import DataFrame
from sklearn.cluster import KMeans
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
# 读取文件
datafile = u'student-mat.xlsx' # 文件所在位置,u为防止路径中有中文名称,此处没有,可以省略
outfile = 'stu.xlsx'
data = pd.read_excel(datafile) # datafile是excel文件,所以用read_excel,如果是csv文件则用read_csv
d = DataFrame(data)
# 聚类
n = 5 # 聚成 5 类数据
mod = KMeans(n_clusters=n)
mod.fit_predict(d) # y_pred表示聚类的结果
# 聚成 5 类数据,统计每个聚类下的数据量,并且求出他们的中心
r1 = pd.Series(mod.labels_).value_counts() # 每个类下面有多少个样本
r2 = pd.DataFrame(mod.cluster_centers_) # 中心
r = pd.concat([r2, r1], axis=1)
r.columns = list(d.columns) + [u'类别数目']
# 给每一条数据标注上被分为哪一类
r = pd.concat([d, pd.Series(mod.labels_, index=d.index)], axis=1)
r.columns = list(d.columns) + [u'聚类类别']
print(r)
r.to_excel(outfile) # 如果需要保存到本地,就写上这一列
# 可视化过程
ts = TSNE()
ts.fit_transform(r)
ts = pd.DataFrame(ts.embedding_, index=r.index)
a = ts[r[u'聚类类别'] == 0]
plt.plot(a[0], a[1], 'r.')
a = ts[r[u'聚类类别'] == 1]
plt.plot(a[0], a[1], 'go')
a = ts[r[u'聚类类别'] == 2]
plt.plot(a[0], a[1], 'g*')
a = ts[r[u'聚类类别'] == 3]
plt.plot(a[0], a[1], 'b.')
a = ts[r[u'聚类类别'] == 4]
plt.plot(a[0], a[1], 'b*')
plt.show()
二、结果


三、数据集
边栏推荐
- Tdengine biweekly selection of community issues | phase III
- 【嵌入式】使用JLINK RTT打印log
- ROS compilation calls the third-party dynamic library (xxx.so)
- win10系统中的截图,win+prtSc保存位置
- MongoDB 的安装和基本操作
- torch建立的网络模型使用torchviz显示
- Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
- 目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
- Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
- [MySQL] multi table query
猜你喜欢

【嵌入式】Cortex M4F DSP库

Excellent software testers have these abilities

egg. JS getting started navigation: installation, use and learning

广州推进儿童友好城市建设,将探索学校周边200米设安全区域

Deep analysis of C language data storage in memory

个人电脑好用必备软件(使用过)

Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
![[embedded] cortex m4f DSP Library](/img/83/ab421d5cc18e907056ec2bdaeb7d5c.png)
[embedded] cortex m4f DSP Library

Esp8266-rtos IOT development

SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
随机推荐
软件卸载时遇到trying to use is on a network resource that is unavailable
After reading the programmer's story, I can't help covering my chest...
UnsupportedOperationException异常
力扣每日一题(二)
Cesium draw points, lines, and faces
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
有效提高软件产品质量,就找第三方软件测评机构
Problems in loading and saving pytorch trained models
Restful API design specification
[NVIDIA development board] FAQ (updated from time to time)
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
被破解毁掉的国产游戏之光
The network model established by torch is displayed by torch viz
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
游戏解包的危害及资源加密的重要性
LeetCode:214. 最短回文串
移位运算符
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
How to conduct interface test? What are the precautions? Nanny level interpretation
Navicat premium create MySQL create stored procedure