目录
- 问题 1
- 1.1 对这些玻璃文物的表面风化与其玻璃类型、纹饰和颜色的关系进行分析
- 数据探索 -- 单个分类变量的绘图
- 树形图
- 条形图
- 扇形图
- 雷达图
- Cramer’s V 相关分析
- 统计检验
- 列联表分析
- 卡方检验
- Fisher检验
- 绘图
- 堆积条形图
- 分组条形图
- 分类模型
- Logistic回归
- 随机森林
python">import matplotlib.pyplot as plt
# Linux show Chinese characters *** important
plt.rcParams['font.family'] = 'WenQuanYi Micro Hei' import warnings
warnings.filterwarnings("ignore")
python">import pandas as pd# read by default 1st sheet of an excel file
path = '/home/shiyu/Desktop/path_acdemic/ant/数模/历年题目/2022/附件.xlsx'
d1 = pd.read_excel(path, sheet_name='表单1')
d2 = pd.read_excel(path, sheet_name='表单2')
d3 = pd.read_excel(path, sheet_name='表单3')print(d1.shape)
print(d2.shape)
print(d3.shape)
(58, 5)
(69, 15)
(8, 16)
问题 1
1.1 对这些玻璃文物的表面风化与其玻璃类型、纹饰和颜色的关系进行分析
数据探索 – 单个分类变量的绘图
树形图
https://www.geeksforgeeks.org/treemaps-in-python-using-squarify/
python">import seaborn as sb
import matplotlib.pyplot as plt
import squarifydata = list(d1['颜色'].value_counts())
labels = list(d1['颜色'].value_counts().index)plt.rcParams['figure.dpi'] = 300
plt.rcParams['savefig.dpi'] = 300plt.figure(figsize=(15,8))
squarify.plot(sizes=data, label=labels, text_kwargs={'fontsize': 20},color=sb.color_palette("Spectral",len(data))) plt.axis("off")
(0.0, 100.0, 0.0, 100.0)
条形图
python">df = pd.DataFrame(d1['颜色'].value_counts())
df['颜色'] = df.index
df
count | 颜色 | |
---|---|---|
颜色 | ||
浅蓝 | 20 | 浅蓝 |
蓝绿 | 15 | 蓝绿 |
深绿 | 7 | 深绿 |
紫 | 4 | 紫 |
浅绿 | 3 | 浅绿 |
深蓝 | 2 | 深蓝 |
黑 | 2 | 黑 |
绿 | 1 | 绿 |
python">import matplotlib.pyplot as plt
import plotly.express as px
import numpy as np# Linux show Chinese characters *** important
plt.rcParams['font.family'] = 'WenQuanYi Micro Hei' fig = px.bar(df, x="颜色", y="count", title="颜色类别计数")
# center title
fig.update_layout(title_x=0.5)
# remove background color
fig.update_layout({
'plot_bgcolor': 'rgba(0, 0, 0, 0)',
'paper_bgcolor': 'rgba(0, 0, 0, 0)',
})fig.show()
扇形图
python">print(d1['纹饰'].value_counts())
纹饰
C 30
A 22
B 6
Name: count, dtype: int64
python">import plotly.express as px
fig = px.pie(d1, names='纹饰', title="玻璃纹饰类别的比例分布")
# center title
fig.update_layout(title_x=0.5)
fig.show()
python">print(d1['类型'].value_counts())
fig = px.pie(d1, names='类型', title="玻璃类型的比例分布")
# center title
fig.update_layout(title_x=0.5)
fig.show()
类型
铅钡 40
高钾 18
Name: count, dtype: int64
python">print(d1['颜色'].value_counts