machine_learning.dimensionality_reduction¶
- Requirements:
numpy version 1.21
scipy version 1.3.3
- Notes:
Each column of the features matrix corresponds to a class item
Functions¶
|
Function to reshape a row Numpy array into a column Numpy array |
|
Function to compute the covariance matrix between multiple classes |
|
Function to compute the covariance matrix inside each class. |
|
Linear Discriminant Analysis. |
|
Principal Component Analysis. |
t-Distributed Stochastic Neighbor Embedding (t-SNE) algorithm for |
|
Test t-SNE algorithm with various input conditions. |
Module Contents¶
- machine_learning.dimensionality_reduction.column_reshape(input_array: numpy.ndarray) numpy.ndarray¶
Function to reshape a row Numpy array into a column Numpy array >>> input_array = np.array([1, 2, 3]) >>> column_reshape(input_array) array([[1],
[2], [3]])
- machine_learning.dimensionality_reduction.covariance_between_classes(features: numpy.ndarray, labels: numpy.ndarray, classes: int) numpy.ndarray¶
Function to compute the covariance matrix between multiple classes >>> features = np.array([[9, 2, 3], [4, 3, 6], [1, 8, 9]]) >>> labels = np.array([0, 1, 0]) >>> covariance_between_classes(features, labels, 2) array([[ 3.55555556, 1.77777778, -2.66666667],
[ 1.77777778, 0.88888889, -1.33333333], [-2.66666667, -1.33333333, 2. ]])
- machine_learning.dimensionality_reduction.covariance_within_classes(features: numpy.ndarray, labels: numpy.ndarray, classes: int) numpy.ndarray¶
Function to compute the covariance matrix inside each class. >>> features = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> labels = np.array([0, 1, 0]) >>> covariance_within_classes(features, labels, 2) array([[0.66666667, 0.66666667, 0.66666667],
[0.66666667, 0.66666667, 0.66666667], [0.66666667, 0.66666667, 0.66666667]])
- machine_learning.dimensionality_reduction.linear_discriminant_analysis(features: numpy.ndarray, labels: numpy.ndarray, classes: int, dimensions: int) numpy.ndarray¶
Linear Discriminant Analysis.
For more details, see: https://en.wikipedia.org/wiki/Linear_discriminant_analysis. Parameters:
features: the features extracted from the dataset
labels: the class labels of the features
classes: the number of classes present in the dataset
dimensions: to filter the projected data for the desired dimension
>>> test_linear_discriminant_analysis()
- machine_learning.dimensionality_reduction.principal_component_analysis(features: numpy.ndarray, dimensions: int) numpy.ndarray¶
Principal Component Analysis.
For more details, see: https://en.wikipedia.org/wiki/Principal_component_analysis. Parameters:
features: the features extracted from the dataset
dimensions: to filter the projected data for the desired dimension
>>> test_principal_component_analysis()
- machine_learning.dimensionality_reduction.t_distributed_stochastic_neighbor_embedding(features: numpy.ndarray, dimensions: int = 2, perplexity: float = 30.0, learning_rate: float = 200.0, max_iterations: int = 1000, random_state: int = 42) numpy.ndarray¶
t-Distributed Stochastic Neighbor Embedding (t-SNE) algorithm for dimensionality reduction.
t-SNE is a machine learning algorithm for visualization developed by Laurens van der Maaten and Geoffrey Hinton. It is a nonlinear dimensionality reduction technique particularly well suited for the visualization of high-dimensional datasets.
For more details, see: https://en.wikipedia.org/wiki/T-distributed_stochastic_neighbor_embedding Original paper: https://www.jmlr.org/papers/volume9/vandermaaten08a/vandermaaten08a.pdf
- Parameters:
features: Input data matrix where each column represents a data point
dimensions: Number of dimensions for the output (typically 2 or 3)
perplexity: Controls the effective number of neighbors (typically 5-50)
learning_rate: Learning rate for gradient descent
max_iterations: Maximum number of optimization iterations
random_state: Random seed for reproducible results
- Returns:
projected_data: Low-dimensional representation of the input data
>>> # Test with simple 3D to 2D reduction >>> features = np.array([[1, 2], [3, 4], [5, 6], [7, 8]], dtype=float).T >>> result = t_distributed_stochastic_neighbor_embedding( ... features, dimensions=2, max_iterations=10 ... ) >>> result.shape (2, 4)
>>> # Test with invalid dimensions >>> try: ... t_distributed_stochastic_neighbor_embedding(features, dimensions=0) ... except ValueError as e: ... print("ValueError raised for invalid dimensions") ValueError raised for invalid dimensions
- machine_learning.dimensionality_reduction.test_linear_discriminant_analysis() None¶
- machine_learning.dimensionality_reduction.test_principal_component_analysis() None¶
- machine_learning.dimensionality_reduction.test_t_distributed_stochastic_neighbor_embedding() None¶
Test t-SNE algorithm with various input conditions.