Skip to content

Commit 43e1f53

Browse files
committed
Reformat file
1 parent 5509e7d commit 43e1f53

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

Diff for: machine_learning/dimensionality_reduction.py

+31-26
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
# Copyright (c) 2023 Diego Gasco ([email protected]), Diegomangasco on GitHub
22

3-
import logging
3+
import logging # noqa: I001
44
import numpy as np
55
import scipy
66

77
logging.basicConfig(level=logging.INFO, format='%(message)s')
88

99

10-
def column_reshape(input_array: np.ndarray) -> np.ndarray:
10+
def _column_reshape(input_array: np.ndarray) -> np.ndarray:
1111
"""Function to reshape a row Numpy array into a column Numpy array"""
1212

1313
return input_array.reshape((input_array.size, 1))
1414

1515

16-
def covariance_within_classes(features: np.ndarray, labels: np.ndarray, classes: int) -> np.ndarray:
16+
def _covariance_within_classes(features: np.ndarray, labels: np.ndarray, classes: int) -> np.ndarray:
1717
"""Function to compute the covariance matrix inside each class"""
1818

1919
covariance_sum = np.nan
2020
for i in range(classes):
2121
data = features[:, labels == i]
2222
data_mean = data.mean(1)
2323
# Centralize the data of class i
24-
centered_data = data - column_reshape(data_mean)
24+
centered_data = data - _column_reshape(data_mean)
2525
if i > 0:
2626
# If covariance_sum is not None
2727
covariance_sum += np.dot(centered_data, centered_data.T)
@@ -32,7 +32,7 @@ def covariance_within_classes(features: np.ndarray, labels: np.ndarray, classes:
3232
return covariance_sum / features.shape[1]
3333

3434

35-
def covariance_between_classes(features: np.ndarray, labels: np.ndarray, classes: int) -> np.ndarray:
35+
def _covariance_between_classes(features: np.ndarray, labels: np.ndarray, classes: int) -> np.ndarray:
3636
"""Function to compute the covariance matrix between multiple classes"""
3737

3838
general_data_mean = features.mean(1)
@@ -43,23 +43,25 @@ def covariance_between_classes(features: np.ndarray, labels: np.ndarray, classes
4343
data_mean = data.mean(1)
4444
if i > 0:
4545
# If covariance_sum is not None
46-
covariance_sum += device_data * np.dot(column_reshape(data_mean) - column_reshape(general_data_mean),
47-
(column_reshape(data_mean) - column_reshape(general_data_mean)).T)
46+
covariance_sum += device_data * np.dot(_column_reshape(data_mean) - _column_reshape(general_data_mean),
47+
(_column_reshape(data_mean) - _column_reshape(general_data_mean)).T)
4848
else:
4949
# If covariance_sum is np.nan (i.e. first loop)
50-
covariance_sum = device_data * np.dot(column_reshape(data_mean) - column_reshape(general_data_mean),
51-
(column_reshape(data_mean) - column_reshape(general_data_mean)).T)
50+
covariance_sum = device_data * np.dot(_column_reshape(data_mean) - _column_reshape(general_data_mean),
51+
(_column_reshape(data_mean) - _column_reshape(general_data_mean)).T)
5252

5353
return covariance_sum / features.shape[1]
5454

5555

56-
def PCA(features: np.ndarray, dimensions: int) -> np.ndarray:
57-
"""Principal Component Analysis. \n
58-
For more details, see here: https://en.wikipedia.org/wiki/Principal_component_analysis \n
59-
Parameters: \n
60-
* features: the features extracted from the dataset
61-
* labels: the class labels of the features
62-
* dimensions: to filter the projected data for the desired dimension"""
56+
def principal_component_analysis(features: np.ndarray, dimensions: int) -> np.ndarray:
57+
"""
58+
Principal Component Analysis.
59+
60+
For more details, see here: https://en.wikipedia.org/wiki/Principal_component_analysis.
61+
Parameters:
62+
* features: the features extracted from the dataset
63+
* dimensions: to filter the projected data for the desired dimension
64+
"""
6365

6466
# Check if the features have been loaded
6567
if features.any():
@@ -81,23 +83,26 @@ def PCA(features: np.ndarray, dimensions: int) -> np.ndarray:
8183
raise AssertionError
8284

8385

84-
def LDA(features: np.ndarray, labels: np.ndarray, classes: int, dimensions: int) -> np.ndarray:
85-
"""Linear Discriminant Analysis. \n
86-
For more details, see here: https://en.wikipedia.org/wiki/Linear_discriminant_analysis \n
87-
Parameters: \n
88-
* features: the features extracted from the dataset
89-
* labels: the class labels of the features
90-
* classes: the number of classes present in the dataset
91-
* dimensions: to filter the projected data for the desired dimension"""
86+
def linear_discriminant_analysis(features: np.ndarray, labels: np.ndarray, classes: int, dimensions: int) -> np.ndarray:
87+
"""
88+
Linear Discriminant Analysis.
89+
90+
For more details, see here: https://en.wikipedia.org/wiki/Linear_discriminant_analysis.
91+
Parameters:
92+
* features: the features extracted from the dataset
93+
* labels: the class labels of the features
94+
* classes: the number of classes present in the dataset
95+
* dimensions: to filter the projected data for the desired dimension
96+
"""
9297

9398
# Check if the dimension desired is less than the number of classes
9499
assert classes > dimensions
95100

96101
# Check if features have been already loaded
97102
if features.any:
98103
_, eigenvectors = scipy.linalg.eigh(
99-
covariance_between_classes(features, labels, classes),
100-
covariance_within_classes(features, labels, classes))
104+
_covariance_between_classes(features, labels, classes),
105+
_covariance_within_classes(features, labels, classes))
101106
filtered_eigenvectors = eigenvectors[:, ::-1][:, :dimensions]
102107
svd_matrix, _, _ = np.linalg.svd(filtered_eigenvectors)
103108
filtered_svd_matrix = svd_matrix[:, 0:dimensions]

0 commit comments

Comments
 (0)