PCA
compute_pca(data, number_of_components, columns=None, scaler_type='standard', nodata_handling='remove', nodata=None)
Compute defined number of principal components for numeric input data.
Before computation, data is scaled according to specified scaler and NaN values removed or replaced. Optionally, a nodata value can be given to handle similarly as NaN values.
If input data is a Numpy array, interpretation of the data depends on its dimensions. If array is 3D, it is interpreted as a multiband raster/stacked rasters format (bands, rows, columns). If array is 2D, it is interpreted as table-like data, where each column represents a variable/raster band and each row a data point (similar to a Dataframe).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[ndarray, DataFrame, GeoDataFrame]
|
Input data for PCA. |
required |
number_of_components |
int
|
The number of principal components to compute. Should be >= 1 and at most the number of numeric columns if input is (Geo)Dataframe. |
required |
columns |
Optional[Sequence[str]]
|
Select columns used for the PCA. Other columns are excluded from PCA, but added back to the result Dataframe intact. Only relevant if input is (Geo)Dataframe. Defaults to None. |
None
|
scaler_type |
Literal[standard, min_max, robust]
|
Transform data according to a specified Sklearn scaler. Options are "standard", "min_max" and "robust". Defaults to "standard". |
'standard'
|
nodata_handling |
Literal[remove, replace]
|
If observations with nodata (NaN and given |
'remove'
|
nodata |
Optional[Number]
|
Define a nodata value to remove. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Union[ndarray, DataFrame, GeoDataFrame]
|
The computed principal components in corresponding format as the input data and the |
ndarray
|
explained variance ratios for each component. |
Raises:
Type | Description |
---|---|
EmptyDataException
|
The input is empty. |
InvalidColumnException
|
Selected columns are not found in the input Dataframe. |
InvalidNumberOfPrincipalComponents
|
The number of principal components is less than 1 or more than number of columns if input was (Geo)DataFrame. |
InvalidParameterValueException
|
If value for |
Source code in eis_toolkit/exploratory_analyses/pca.py
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 |
|
plot_pca(pca_df, explained_variances=None, color_column_name=None, save_path=None)
Plot a scatter matrix of different principal component combinations.
Automatically filters columns that do not start with "principal_component" for plotting.
This tool is designed to work smoothly on compute_pca
outputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pca_df |
DataFrame
|
A DataFrame containing computed principal components. |
required |
explained_variances |
Optional[ndarray]
|
The explained variance ratios for each principal component. Used for labeling axes in the plot. Optional parameter. Defaults to None. |
None
|
color_column_name |
Optional[str]
|
Name of the column that will be used for color-coding data points. Typically a categorical variable in the original data. Optional parameter, no colors if not provided. Defaults to None. |
None
|
save_path |
Optional[str]
|
The save path for the plot. Optional parameter, no saving if not provided. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
PairGrid
|
A Seaborn pairgrid containing the PCA scatter matrix. |
Raises:
Type | Description |
---|---|
InvalidColumnException
|
DataFrame does not contain the given color column. |
Source code in eis_toolkit/exploratory_analyses/pca.py
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 |
|