This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.py
161 lines (149 loc) · 4.47 KB
/
func.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
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
"""
fuctions to be used
"""
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from io import BytesIO
import base64
import scipy
def gethtml(lst: list):
lenth = len(lst)
html = "<table><tr>"
for line in range(lenth):
if line == 0:
tmp = lst[line].split(',')
for i in tmp:
html += "<th>{}</th>".format(i)
html += "</tr>"
else:
html += "<tr>"
tmp = lst[line].split(',')
for i in tmp:
html += "<td>{}</td>".format(i)
html += "</tr>"
return html + "</table>"
def get_corr(data: pd):
"""
get corr matrix in html table
:param data: dataframe
:return:
"""
corr = data.corr()
lst = corr.values.tolist()
name = list(corr.columns)
# print(name)
length = len(name)
for i in range(length):
lst[i].insert(0, name[i])
lst.insert(0, ["corr"] + name)
# ans="<table border=\"1\">"
a = data.corr()
plt.subplots(figsize=(9, 9))
sns.heatmap(a, annot=True, vmax=1, square=True, cmap="Blues")
buffer = BytesIO()
plt.savefig(buffer)
plot_data = buffer.getvalue()
imb = base64.b64encode(plot_data)
ims = imb.decode()
imd = "data:image/png;base64," + ims
plt.clf()
# for i in lst:
# ans+="<tr>"
# for j in i:
# if j not in name:
# ans+="<td bgcolor='#{}'>{}</td>".format(colorize(j),str(j)[:4])
# else:
# ans+="<td bgcolor='#{}'>{}</td>".format(colorize(j),str(j))
# ans+="</tr>"
return "<center><img src=\"{}\"><br></center>".format(
imd) # ans+"</table>"
def create_b_figure(ans: dict) -> str:
args = ans["var"][1:]
bvalue = ans["coefficient"]
plt.bar(args, bvalue)
for a, b in zip(args, bvalue):
plt.text(a,
b + 0.003,
'%.3f' % b,
ha='center',
va='bottom',
fontsize=11)
plt.xlabel('variables')
plt.ylabel('b-value')
plt.ylim(min(0, min(bvalue) * 1.2), max(0, max(bvalue) * 1.2))
plt.rcParams['figure.figsize'] = (8.0, 4.0)
plt.title = "b value"
buffer = BytesIO()
plt.savefig(buffer)
plot_data = buffer.getvalue()
imb = base64.b64encode(plot_data)
ims = imb.decode()
imd = "data:image/png;base64," + ims
plt.clf()
return imd
def create_p_figure(ans: dict) -> str:
args = ans["var"][1:]
pvalue = ans["P>|t|"]
plt.bar(args, pvalue)
for a, b in zip(args, pvalue):
plt.text(a,
b + 0.003,
'%.3f' % b,
ha='center',
va='bottom',
fontsize=11)
plt.xlabel('variables')
plt.ylabel('p-value')
plt.ylim(min(0, min(pvalue) * 1.2), max(0, max(pvalue) * 1.2))
plt.axhline(y=0.01, ls="-", c="violet", label="1%")
plt.axhline(y=0.05, ls="-", c="mediumpurple", label="5%")
plt.axhline(y=0.1, ls="-", c="cornflowerblue", label="10%")
plt.rcParams['figure.figsize'] = (8.0, 4.0)
plt.title = "p value"
plt.legend()
buffer = BytesIO()
plt.savefig(buffer)
plot_data = buffer.getvalue()
imb = base64.b64encode(plot_data)
ims = imb.decode()
imd = "data:image/png;base64," + ims
plt.clf()
return imd
def create_t_figure(ans: dict) -> str:
n = ans["observation"]
args = ans["var"][1:]
k = ans["df"]
tvalue = ans["t"]
t_1 = scipy.stats.t.ppf(0.995, n - k)
t_5 = scipy.stats.t.ppf(0.975, n - k)
t_10 = scipy.stats.t.ppf(0.95, n - k)
plt.bar(args, tvalue)
for a, b in zip(args, tvalue):
plt.text(a,
b + 0.003,
'%.3f' % b,
ha='center',
va='bottom',
fontsize=11)
plt.xlabel('variables')
plt.ylabel('t-value')
plt.ylim(min(0, min(tvalue) * 1.2), max(0, max(tvalue) * 1.2))
plt.axhline(y=t_1, ls="-", c="violet", label="1%")
plt.axhline(y=t_5, ls="-", c="mediumpurple", label="5%")
plt.axhline(y=t_10, ls="-", c="cornflowerblue", label="10%")
plt.rcParams['figure.figsize'] = (8.0, 4.0)
plt.title = "t value"
plt.legend()
buffer = BytesIO()
plt.savefig(buffer)
plot_data = buffer.getvalue()
imb = base64.b64encode(plot_data)
ims = imb.decode()
imd = "data:image/png;base64," + ims
plt.clf()
return imd
if __name__ == '__main__':
# data = pd.read_csv("./tk/daily_Ashare.csv")
# print(get_corr(data))
pass