mirror of
https://github.com/13hannes11/UU_NCML_Project.git
synced 2024-09-03 20:50:59 +02:00
refactor plot mps and put plotting parties in function
This commit is contained in:
@@ -172,8 +172,22 @@ def plot_mps(fig, ax, x, y, labels, colors, cmap = plt.cm.RdYlGn):
|
|||||||
fig.canvas.mpl_connect("motion_notify_event", hover)
|
fig.canvas.mpl_connect("motion_notify_event", hover)
|
||||||
#plt.show()
|
#plt.show()
|
||||||
|
|
||||||
|
def plot_mps(names, xs, ys, party_affiliation):
|
||||||
|
# converting parties to numeric format
|
||||||
|
party_index_mapping, party_ids = np.unique(party_affiliation, return_inverse=True)
|
||||||
|
|
||||||
def plot_parties(xs, ys, party_ids, party_index_mapping):
|
# add random offset to show points that are in the same location
|
||||||
|
ys_disp = ys + np.random.rand(ys.shape[0])
|
||||||
|
xs_disp = xs + np.random.rand(xs.shape[0])
|
||||||
|
parties = party_index_mapping[party_ids]
|
||||||
|
plot_hoverscatter(xs_disp, ys_disp, data[:,0] + " (" + parties + ")", party_ids)
|
||||||
|
|
||||||
|
|
||||||
|
def plot_parties(xs, ys, party_affiliation):
|
||||||
|
# converting parties to numeric format
|
||||||
|
party_index_mapping, party_ids = np.unique(party_affiliation, return_inverse=True)
|
||||||
|
|
||||||
|
# calculate average position of party
|
||||||
party_count = np.zeros(party_index_mapping.shape[0])
|
party_count = np.zeros(party_index_mapping.shape[0])
|
||||||
party_xs = np.zeros(party_index_mapping.shape[0])
|
party_xs = np.zeros(party_index_mapping.shape[0])
|
||||||
party_ys = np.zeros(party_index_mapping.shape[0])
|
party_ys = np.zeros(party_index_mapping.shape[0])
|
||||||
@@ -181,9 +195,9 @@ def plot_parties(xs, ys, party_ids, party_index_mapping):
|
|||||||
party_xs[party_id] += x
|
party_xs[party_id] += x
|
||||||
party_ys[party_id] += y
|
party_ys[party_id] += y
|
||||||
party_count[party_id] += 1
|
party_count[party_id] += 1
|
||||||
|
|
||||||
party_xs /= party_count
|
party_xs /= party_count
|
||||||
party_ys /= party_count
|
party_ys /= party_count
|
||||||
|
|
||||||
plt.figure()
|
plt.figure()
|
||||||
plt.scatter(party_xs, party_ys)
|
plt.scatter(party_xs, party_ys)
|
||||||
# plotting labels
|
# plotting labels
|
||||||
@@ -227,23 +241,19 @@ plt.axis('off')
|
|||||||
plt.colorbar()
|
plt.colorbar()
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
fig,ax = plt.subplots()
|
# predicting mp positions
|
||||||
prediction = sofmnet.predict(X)
|
prediction = sofmnet.predict(X)
|
||||||
print(f'prediction: {prediction}')
|
print(f'prediction: {prediction}')
|
||||||
|
|
||||||
# converting to x and y coordinates
|
# converting to x and y coordinates
|
||||||
ys, xs = np.unravel_index(np.argmax(X, axis=1), (h, w))
|
ys, xs = np.unravel_index(np.argmax(X, axis=1), (h, w))
|
||||||
|
|
||||||
# add random offset to show points that are in the same location
|
# plotting mps
|
||||||
ys_disp = ys + np.random.rand(ys.shape[0])
|
plot_mps(data[:,0], xs, ys, data[:,1])
|
||||||
xs_disp = xs + np.random.rand(xs.shape[0])
|
|
||||||
|
|
||||||
party_index_mapping, party_ids = np.unique(data[:,1], return_inverse=True)
|
|
||||||
|
|
||||||
plot_mps(fig, ax, xs_disp, ys_disp, data[:,0] + " (" + data[:,1] + ")", party_ids)
|
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
# plotting parties
|
# plotting parties
|
||||||
plot_parties(xs, ys, party_ids, party_index_mapping)
|
plot_parties(xs, ys, data[:,1])
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
#Simple SOFM for UK
|
#Simple SOFM for UK
|
||||||
|
|||||||
Reference in New Issue
Block a user