-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.1关联关系-相关系数.py
95 lines (88 loc) · 3.72 KB
/
4.1关联关系-相关系数.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""计算各化学成分的皮尔逊相关系数,并用热力图的形式进行可视化"""
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import HeatMap
from pyecharts.globals import ThemeType
def plot_heatmap(x_label, y_label, value, title):
c = (
HeatMap(init_opts=opts.InitOpts(theme=ThemeType.WHITE))
.add_xaxis(x_label)
.add_yaxis(
"",
y_label,
value,
label_opts=opts.LabelOpts(is_show=True, position="inside"),
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="",
pos_left="center",
),
visualmap_opts=opts.VisualMapOpts(
max_=max(i[2] for i in value),
min_=min(i[2] for i in value),
pos_right="right",
pos_top="center",
),
xaxis_opts=opts.AxisOpts(
name="",
name_location="middle",
name_gap=25,
name_textstyle_opts=opts.TextStyleOpts(
font_size=16,
font_weight="bold",
)
),
yaxis_opts=opts.AxisOpts(
name="",
name_location="middle",
name_gap=35,
name_textstyle_opts=opts.TextStyleOpts(
font_size=16,
font_weight="bold",
)
),
toolbox_opts=opts.ToolboxOpts(
feature=opts.ToolBoxFeatureOpts(
save_as_image=opts.ToolBoxFeatureSaveAsImageOpts(
pixel_ratio=6, # 设置保存图片的分辨率
background_color='white', # 设置导出图片的背景颜色
)
)
),
)
.render("output/" + title + "heatmap.html")
)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
filename = 'data/附件.xlsx'
info = pd.read_excel(filename, sheet_name='表单1')
chem = pd.read_excel(filename, sheet_name='表单2')
chem.fillna(0, inplace=True)
chem['累加和'] = chem.iloc[:, 1:].apply(lambda x: x.sum(), axis=1)
chem = chem[chem['累加和'] <= 105]
chem = chem[chem['累加和'] >= 85]
chem['文物编号'] = chem['文物采样点'].apply(lambda x: int(x[:2]))
chem['采样点'] = chem['文物采样点'].apply(lambda x: x[2:])
chem.index = list(range(len(chem)))
chem['类型'] = [0] * len(chem)
chem['表面风化'] = [0] * len(chem)
for i in chem.index:
chem['类型'][i] = info[info['文物编号'] == chem['文物编号'][i]]['类型'].tolist()[0]
chem['表面风化'][i] = info[info['文物编号'] == chem['文物编号'][i]]['表面风化'].tolist()[0]
highK = chem[chem['类型'] == '高钾']
highK = highK.iloc[:, 1:-5]
highK_cor = highK.corr(method='pearson').applymap(lambda x: round(x, 2))
print(highK_cor)
highK_cor = highK_cor.to_numpy()
label = ['SiO2', 'Na2O', 'K2O', 'CaO', 'MgO', 'Al2O3', 'Fe2O3', 'CuO', 'PbO', 'BaO', 'P2O5', 'SrO', 'SnO2', 'SO2']
highK_value = [[i, j, highK_cor[i][j]] for i in range(14) for j in range(14)]
plot_heatmap(label, label, highK_value, "高钾玻璃相关系数")
PbBa = chem[chem['类型'] == '铅钡']
PbBa = PbBa.iloc[:, 1:-5]
PbBa_cor = PbBa.corr(method='pearson').applymap(lambda x: round(x, 2))
print(PbBa_cor)
PbBa_cor = PbBa_cor.to_numpy()
label = ['SiO2', 'Na2O', 'K2O', 'CaO', 'MgO', 'Al2O3', 'Fe2O3', 'CuO', 'PbO', 'BaO', 'P2O5', 'SrO', 'SnO2', 'SO2']
PbBa_value = [[i, j, PbBa_cor[i][j]] for i in range(14) for j in range(14)]
plot_heatmap(label, label, PbBa_value, "铅钡玻璃相关系数")