-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
250 lines (192 loc) · 9.17 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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)
def calculate_centroid(group):
"""
Calculates the centroid of a group of latitude and longitude values.
The centroid formula takes into account the geographical location of each point and calculates the weighted average of the latitude and longitude values, which gives a more accurate representation.
The formula for the centroid of a set of points in spherical coordinates is:
lat = atan2(sum(sin(lat_i)), sum(cos(lat_i)))
lon = atan2(sum(sin(lon_i)), sum(cos(lon_i)))
Parameters
----------
group : pandas.DataFrame
A pandas DataFrame containing latitude and longitude values.
Returns
-------
pandas.Series
A pandas Series with the centroid latitude and longitude.
"""
# Extract the latitude and longitude values from the group
latitudes = group['Lat'].values
longitudes = group['Lon'].values
# Calculate the number of data points in the group
n = len(group)
# Convert the latitude and longitude values to 3D Cartesian coordinates
x = np.cos(np.radians(latitudes)) * np.cos(np.radians(longitudes))
y = np.cos(np.radians(latitudes)) * np.sin(np.radians(longitudes))
z = np.sin(np.radians(latitudes))
# Calculate the mean of the 3D Cartesian coordinates
x_mean = np.mean(x)
y_mean = np.mean(y)
z_mean = np.mean(z)
# Convert the mean 3D Cartesian coordinates back to latitude and longitude
lon_mean = np.degrees(np.arctan2(y_mean, x_mean))
lat_mean = np.degrees(np.arcsin(z_mean))
# Create a pandas Series with the centroid latitude and longitude
return pd.Series({'centroid_latitude': lat_mean, 'centroid_longitude': lon_mean})