-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVD_Plot.py
131 lines (100 loc) · 3.52 KB
/
SVD_Plot.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
from PR_Senators import *
from ControversialBills import *
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
def plot_svd(year, U):
t = pd.DataFrame({"X": U[:, 0], "Y": U[:, 1], "Party": np.array(parties)})
groups = t.groupby("Party")
for name, group in groups:
if name == "PNP":
col = "b"
elif name == "PPD":
col = "r"
elif name == "PIP":
col = "g"
elif name == "indep.":
col = "k"
plt.plot(group["X"], group["Y"], col, marker="o", linestyle="", label=name, color=col)
plt.legend()
plt.title("SVD Analysis " + str(year))
plt.show()
def plot_pc(year, U_S): # U_S is a list of the form [U, S]
A = U_S[0] @ np.diag(U_S[1])
t = pd.DataFrame({"X": A[:, 0], "Y": A[:, 1], "Party": np.array(parties)})
groups = t.groupby("Party")
for name, group in groups:
if name == "PNP":
col = "b"
elif name == "PPD":
col = "r"
elif name == "PIP":
col = "g"
elif name == "indep.":
col = "k"
plt.plot(group["X"], group["Y"], col, marker="o", linestyle="", label=name, color=col)
plt.legend()
plt.title("Principal Component (Scaled SVD) Analysis " + str(year))
plt.show()
myData = read_txt(1, 16)
plot_err("2017-2020", myData)
plt.savefig("2017-2020 error.png")
plt.close()
plot_svd("2017-2020", u_sig(myData)[0])
plt.savefig("2017-2020 SVD.png")
plt.close()
plot_pc("2017-2020", u_sig(myData))
plt.savefig("2017-2020 PCA.png")
plt.close()
for i in range(4):
myData = read_txt(4 * i + 1, 4 * i + 4)
plot_err(i + 2017, myData)
plt.savefig(str(i + 2017) + " error.png")
plt.close()
plot_svd(i + 2017, u_sig(myData)[0])
plt.savefig(str(i + 2017) + " SVD.png")
plt.close()
plot_pc(i + 2017, u_sig(myData))
plt.savefig(str(i + 2017) + " PCA.png")
plt.close()
myData = cont_bills(1, 16)
plot_err("2017-2020 *", myData)
plt.savefig("2017-2020 controversial error.png")
plt.close()
plot_svd("2017-2020 *", u_sig(myData)[0])
plt.savefig("2017-2020 controversial SVD.png")
plt.close()
plot_pc("2017-2020 *", u_sig(myData))
plt.savefig("2017-2020 controversial PCA.png")
plt.close()
myData = not_cont_bills(1, 16)
plot_err("2017-2020 **", myData)
plt.savefig("2017-2020 not controversial error.png")
plt.close()
plot_svd("2017-2020 **", u_sig(myData)[0])
plt.savefig("2017-2020 not controversial SVD.png")
plt.close()
plot_pc("2017-2020 **", u_sig(myData))
plt.savefig("2017-2020 not controversial PCA.png")
plt.close()
for i in range(4):
myData = cont_bills(4 * i + 1, 4 * i + 4)
plot_err(str(i + 2017) + "*", myData)
plt.savefig(str(i + 2017) + " controversial error.png")
plt.close()
plot_svd(str(i + 2017) + "*", u_sig(myData)[0])
plt.savefig(str(i + 2017) + " controversial SVD.png")
plt.close()
plot_pc(str(i + 2017) + "*", u_sig(myData))
plt.savefig(str(i + 2017) + " controversial PCA.png")
plt.close()
myData = not_cont_bills(4 * i + 1, 4 * i + 4)
plot_err(str(i + 2017) + "**", myData)
plt.savefig(str(i + 2017) + " not controversial error.png")
plt.close()
plot_svd(str(i + 2017) + "**", u_sig(myData)[0])
plt.savefig(str(i + 2017) + " not controversial SVD.png")
plt.close()
plot_pc(str(i + 2017) + "**", u_sig(myData))
plt.savefig(str(i + 2017) + " not controversial PCA.png")
plt.close()