-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
176 lines (156 loc) · 7.17 KB
/
Copy pathutils.py
File metadata and controls
176 lines (156 loc) · 7.17 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
import streamlit as st
train_pre_metric, test_pre_metric, pre_train, pre_test = [], [], 0, 0
def model_performance(metrics, site, mode):
"""
可视化模型表现
"""
# 转换为DataFrame
df = pd.DataFrame.from_dict(metrics, orient='index', columns=['Values'])
index = df.index.tolist()
# 显示表格
st.markdown("### {}. 模型表现".format(site))
if df.values[0] == -1:
st.write('因为所给的数据不包含\'label\'列,所以无法查看模型的表现,不过可以下载预测数据')
else:
col1, col2, col3, col4 = st.columns(4)
if mode == 'train':
global train_pre_metric, pre_train
if pre_train == 0:
col1.metric(index[0], round(float(df.values[0]), 4))
col2.metric(index[1], round(float(df.values[1]), 4))
col3.metric(index[2], round(float(df.values[2]), 4))
col4.metric(index[3], round(float(df.values[3]), 4))
pre_train = 1
train_pre_metric = df
else:
col1.metric(index[0], round(float(df.values[0]), 4),
round(float(df.values[0]) - float(train_pre_metric.values[0]), 4))
col2.metric(index[1], round(float(df.values[1]), 4),
round(float(df.values[1]) - float(train_pre_metric.values[1]), 4))
col3.metric(index[2], round(float(df.values[2]), 4),
round(float(df.values[2]) - float(train_pre_metric.values[2]), 4))
col4.metric(index[3], round(float(df.values[3]), 4),
round(float(df.values[3]) - float(train_pre_metric.values[3]), 4))
train_pre_metric = df
elif mode == 'test':
global test_pre_metric, pre_test
if pre_test == 0:
col1.metric(index[0], round(float(df.values[0]), 4))
col2.metric(index[1], round(float(df.values[1]), 4))
col3.metric(index[2], round(float(df.values[2]), 4))
col4.metric(index[3], round(float(df.values[3]), 4))
test_pre_metric = df
pre_test = 1
else:
col1.metric(index[0], round(float(df.values[0]), 4),
round(float(df.values[0]) - float(test_pre_metric.values[0]), 4))
col2.metric(index[1], round(float(df.values[1]), 4),
round(float(df.values[1]) - float(test_pre_metric.values[1]), 4))
col3.metric(index[2], round(float(df.values[2]), 4),
round(float(df.values[2]) - float(test_pre_metric.values[2]), 4))
col4.metric(index[3], round(float(df.values[3]), 4),
round(float(df.values[3]) - float(test_pre_metric.values[3]), 4))
test_pre_metric = df
return df
def data_value_count(data, site, mode='training'):
"""
可视化每一个类别的个数
"""
if mode == 'training':
st.markdown("### {}. 上传数据各类别数量".format(site))
trace = [go.Bar(x=[0, 1, 2, 3, 4, 5], y=data['label'].value_counts(), width=0.6)]
else:
st.markdown("### {}. 预测数据各类别数量".format(site))
trace = [go.Bar(x=[0, 1, 2, 3, 4, 5], y=data['pred_label'].value_counts(), width=0.6)]
fig = go.Figure(data=trace)
st.plotly_chart(fig, use_container_width=True)
def data_percentage(data, site, mode='training'):
"""
可视化每个类所占的比例
"""
labels = [0, 1, 2, 3, 4, 5]
if mode == 'training':
st.markdown("### {}. 上传数据各类别所占百分比".format(site))
values = data['label'].value_counts()
else:
st.markdown("### {}. 预测数据各类别所占百分比".format(site))
values = data['pred_label'].value_counts()
trace = [go.Pie(labels=labels, values=values, hole=0.4)]
# layout = go.Layout(title='各类别所占百分比')
fig = go.Figure(data=trace)
st.plotly_chart(fig, use_container_width=True)
def data_nan_distribution(data, site):
"""
可视化空缺值(NaN值)在数据集中所占位置
"""
st.markdown("### {}. 上传数据的缺失值分布".format(site))
data = data.drop('sample_id', axis=1)
try:
data = data.drop('label', axis=1)
except KeyError:
pass
def find_nan(data_s):
indexes = data_s.columns.tolist() # 将列名变为列表
tmp = pd.DataFrame(index=data_s.index, columns=data_s.columns)
for i in range(data_s.shape[1]):
tmp[indexes[i]] = data_s[indexes[i]].apply(lambda x: 1 if np.isnan(x) else 2)
return tmp
df = find_nan(data)
fig = px.imshow(df, color_continuous_scale='PuBu')
st.plotly_chart(fig, use_container_width=True)
def data_distribution(data, site):
"""
可视化数据集总体数据分布(在标准化之后的,因为要是同一种颜色)
"""
st.markdown("### {}. 上传数据的总体数据分布".format(site))
df = data.drop('sample_id', axis=1)
try:
df = df.drop('label', axis=1)
except KeyError:
pass
def has_single_value(series):
begin = series[0]
for value in series:
if begin != value:
return False
return True
def min_max_scaler(x):
if has_single_value(x):
return x.apply(lambda y: 0.5)
else:
return (x - np.min(x)) / (np.max(x) - np.min(x))
df = df.apply(min_max_scaler)
# print(df)
fig = px.imshow(df, color_continuous_scale='PuBu')
st.plotly_chart(fig, use_container_width=True)
def data_classification(data, label, site):
"""
可视化数据集分类结果
不要用在训练集上,会很慢
"""
st.markdown("### {}. 数据数据预测结果可视化".format(site))
# label = data['label']
data = data[['feature5', 'feature10', 'feature15', 'feature22', 'feature45', 'feature71']].copy()
data.fillna(data.mean(), inplace=True, axis=0)
if len(data) < 3 or isinstance(data, pd.Series):
data = pd.DataFrame(data[['feature10', 'feature15', 'feature71']])
data.fillna(0, inplace=True)
print(data)
data.columns = ['x', 'y', 'z']
data_after = pd.concat([data, label], axis=1)
elif len(data) < 30:
pca = PCA(n_components=3).fit_transform(data)
pca = pd.DataFrame(pca, columns=['x', 'y', 'z'])
data_after = pd.concat([pca, label], axis=1)
else:
tsne = TSNE(n_components=3, perplexity=25.0, learning_rate=30, random_state=21).fit_transform(data)
tsne = pd.DataFrame(tsne, columns=['x', 'y', 'z'])
data_after = pd.concat([tsne, label], axis=1)
fig = px.scatter_3d(data_after, x='x', y='y', z='z', color='pred_label', color_continuous_scale='Spectral')
st.plotly_chart(fig, use_container_width=True)