-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
186 lines (145 loc) · 7.57 KB
/
functions.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Library of Functions for the OpenClassrooms Multivariate Exploratory Data Analysis Course
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
import numpy as np
import pandas as pd
from scipy.cluster.hierarchy import dendrogram
from pandas.plotting import parallel_coordinates
import seaborn as sns
palette = sns.color_palette("bright", 10)
def display_circles(pcs, n_comp, pca, axis_ranks, labels=None, label_rotation=0, lims=None):
"""Display correlation circles, one for each factorial plane"""
# For each factorial plane
for d1, d2 in axis_ranks:
if d2 < n_comp:
# Initialise the matplotlib figure
fig, ax = plt.subplots(figsize=(10,10))
# Determine the limits of the chart
if lims is not None :
xmin, xmax, ymin, ymax = lims
elif pcs.shape[1] < 30 :
xmin, xmax, ymin, ymax = -1, 1, -1, 1
else :
xmin, xmax, ymin, ymax = min(pcs[d1,:]), max(pcs[d1,:]), min(pcs[d2,:]), max(pcs[d2,:])
# Add arrows
# If there are more than 30 arrows, we do not display the triangle at the end
if pcs.shape[1] < 30 :
plt.quiver(np.zeros(pcs.shape[1]), np.zeros(pcs.shape[1]),
pcs[d1,:], pcs[d2,:],
angles='xy', scale_units='xy', scale=1, color="grey")
# (see the doc : https://matplotlib.org/api/_as_gen/matplotlib.pyplot.quiver.html)
else:
lines = [[[0,0],[x,y]] for x,y in pcs[[d1,d2]].T]
ax.add_collection(LineCollection(lines, axes=ax, alpha=.1, color='black'))
# Display variable names
if labels is not None:
for i,(x, y) in enumerate(pcs[[d1,d2]].T):
if x >= xmin and x <= xmax and y >= ymin and y <= ymax :
plt.text(x, y, labels[i], fontsize='14', ha='center', va='center', rotation=label_rotation, color="blue", alpha=0.5)
# Display circle
circle = plt.Circle((0,0), 1, facecolor='none', edgecolor='b')
plt.gca().add_artist(circle)
# Define the limits of the chart
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
# Display grid lines
plt.plot([-1, 1], [0, 0], color='grey', ls='--')
plt.plot([0, 0], [-1, 1], color='grey', ls='--')
# Label the axes, with the percentage of variance explained
plt.xlabel('PC{} ({}%)'.format(d1+1, round(100*pca.explained_variance_ratio_[d1],1)))
plt.ylabel('PC{} ({}%)'.format(d2+1, round(100*pca.explained_variance_ratio_[d2],1)))
plt.title("Correlation Circle (PC{} and PC{})".format(d1+1, d2+1))
plt.show(block=False)
def display_factorial_planes(X_projected, n_comp, pca, axis_ranks, labels=None, alpha=1, illustrative_var=None):
'''Display a scatter plot on a factorial plane, one for each factorial plane'''
# For each factorial plane
for d1,d2 in axis_ranks:
if d2 < n_comp:
# Initialise the matplotlib figure
fig = plt.figure(figsize=(7,6))
# Display the points
if illustrative_var is None:
plt.scatter(X_projected[:, d1], X_projected[:, d2], alpha=alpha)
else:
illustrative_var = np.array(illustrative_var)
for value in np.unique(illustrative_var):
selected = np.where(illustrative_var == value)
plt.scatter(X_projected[selected, d1], X_projected[selected, d2], alpha=alpha, label=value)
plt.legend()
# Display the labels on the points
if labels is not None:
for i,(x,y) in enumerate(X_projected[:,[d1,d2]]):
plt.text(x, y, labels[i],
fontsize='14', ha='center',va='center')
# Define the limits of the chart
boundary = np.max(np.abs(X_projected[:, [d1,d2]])) * 1.1
plt.xlim([-boundary,boundary])
plt.ylim([-boundary,boundary])
# Display grid lines
plt.plot([-100, 100], [0, 0], color='grey', ls='--')
plt.plot([0, 0], [-100, 100], color='grey', ls='--')
# Label the axes, with the percentage of variance explained
plt.xlabel('PC{} ({}%)'.format(d1+1, round(100*pca.explained_variance_ratio_[d1],1)))
plt.ylabel('PC{} ({}%)'.format(d2+1, round(100*pca.explained_variance_ratio_[d2],1)))
plt.title("Projection of points (on PC{} and PC{})".format(d1+1, d2+1))
#plt.show(block=False)
def display_scree_plot(pca):
'''Display a scree plot for the pca'''
scree = pca.explained_variance_ratio_*100
plt.bar(np.arange(len(scree))+1, scree)
plt.plot(np.arange(len(scree))+1, scree.cumsum(),c="red",marker='o')
plt.xlabel("Number of principal components")
plt.ylabel("Percentage explained variance")
plt.title("Scree plot")
plt.show(block=False)
def append_class(df, class_name, feature, thresholds, names):
'''Append a new class feature named 'class_name' based on a threshold split of 'feature'. Threshold values are in 'thresholds' and class names are in 'names'.'''
n = pd.cut(df[feature], bins = thresholds, labels=names)
df[class_name] = n
def plot_dendrogram(Z, names, figsize=(10,25)):
'''Plot a dendrogram to illustrate hierarchical clustering'''
plt.figure(figsize=figsize)
plt.title('Hierarchical Clustering Dendrogram')
plt.xlabel('distance')
dendrogram(
Z,
labels = names,
orientation = "left",
)
#plt.show()
def addAlpha(colour, alpha):
'''Add an alpha to the RGB colour'''
return (colour[0],colour[1],colour[2],alpha)
def display_parallel_coordinates(df, num_clusters):
'''Display a parallel coordinates plot for the clusters in df'''
# Select data points for individual clusters
cluster_points = []
for i in range(num_clusters):
cluster_points.append(df[df.cluster==i])
# Create the plot
fig = plt.figure(figsize=(12, 15))
title = fig.suptitle("Parallel Coordinates Plot for the Clusters", fontsize=18)
fig.subplots_adjust(top=0.95, wspace=0)
# Display one plot for each cluster, with the lines for the main cluster appearing over the lines for the other clusters
for i in range(num_clusters):
plt.subplot(num_clusters, 1, i+1)
for j,c in enumerate(cluster_points):
if i!= j:
pc = parallel_coordinates(c, 'cluster', color=[addAlpha(palette[j],0.2)])
pc = parallel_coordinates(cluster_points[i], 'cluster', color=[addAlpha(palette[i],0.5)])
# Stagger the axes
ax=plt.gca()
for tick in ax.xaxis.get_major_ticks()[1::2]:
tick.set_pad(20)
def display_parallel_coordinates_centroids(df, num_clusters):
'''Display a parallel coordinates plot for the centroids in df'''
# Create the plot
fig = plt.figure(figsize=(12, 5))
title = fig.suptitle("Parallel Coordinates plot for the Centroids", fontsize=18)
fig.subplots_adjust(top=0.9, wspace=0)
# Draw the chart
parallel_coordinates(df, 'cluster', color=palette)
# Stagger the axes
ax=plt.gca()
for tick in ax.xaxis.get_major_ticks()[1::2]:
tick.set_pad(20)