Logistic regression
load_model(path)
Load a Sklearn model from a .joblib file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Path
|
Path from where the model should be loaded. Include the .joblib file extension. |
required |
Returns:
Type | Description |
---|---|
BaseEstimator
|
Loaded model. |
Source code in eis_toolkit/prediction/machine_learning_general.py
45 46 47 48 49 50 51 52 53 54 55 56 |
|
prepare_data_for_ml(feature_raster_files, label_file=None)
Prepare data ready for machine learning model training.
Performs the following steps: - Read all bands of all feature/evidence rasters into a stacked Numpy array - Read label data (and rasterize if a vector file is given) - Create a nodata mask using all feature rasters and labels, and mask nodata cells out
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature_raster_files |
Sequence[Union[str, PathLike]]
|
List of filepaths of feature/evidence rasters. Files should only include raster that have the same grid properties and extent. |
required |
label_file |
Optional[Union[str, PathLike]]
|
Filepath to label (deposits) data. File can be either a vector file or raster file. If a vector file is provided, it will be rasterized into similar grid as feature rasters. If a raster file is provided, it needs to have same grid properties and extent as feature rasters. Optional parameter and can be omitted if preparing data for predicting. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
Feature data (X) in prepared shape. |
Optional[ndarray]
|
Target labels (y) in prepared shape (if |
Profile
|
Refrence raster metadata . |
Any
|
Nodata mask applied to X and y. |
Raises:
Type | Description |
---|---|
InvalidDatasetException
|
Input feature rasters contains only one path. |
NonMatchingRasterMetadataException
|
Input feature rasters, and optionally rasterized label file, don't have same grid properties. |
Source code in eis_toolkit/prediction/machine_learning_general.py
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 |
|
read_data_for_evaluation(rasters)
Prepare data ready for evaluating modeling outputs.
Reads all rasters (only first band), reshapes them (flattens) and masks out all NaN and nodata pixels by creating a combined mask from all input rasters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rasters |
Sequence[Union[str, PathLike]]
|
List of filepaths of input rasters. Files should only include raster that have the same grid properties and extent. |
required |
Returns:
Type | Description |
---|---|
Sequence[ndarray]
|
List of reshaped and masked raster data. |
Profile
|
Refrence raster profile. |
Any
|
Nodata mask applied to raster data. |
Raises:
Type | Description |
---|---|
InvalidDatasetException
|
Input rasters contains only one path. |
NonMatchingRasterMetadataException
|
Input rasters don't have same grid properties. |
Source code in eis_toolkit/prediction/machine_learning_general.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
reshape_predictions(predictions, height, width, nodata_mask=None)
Reshape 1D prediction ouputs into 2D Numpy array.
The output is ready to be visualized and saved as a raster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
ndarray
|
A 1D Numpy array with raw prediction data from |
required |
height |
int
|
Height of the output array |
required |
width |
int
|
Width of the output array |
required |
nodata_mask |
Optional[ndarray]
|
Nodata mask used to reconstruct original shape of data. This is the same mask applied to data before predicting to remove nodata. If any nodata was removed before predicting, this mask is required to reconstruct the original shape of data. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
Predictions as a 2D Numpy array in the original array shape. |
Source code in eis_toolkit/prediction/machine_learning_general.py
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 |
|
save_model(model, path)
Save a trained Sklearn model to a .joblib file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
BaseEstimator
|
Trained model. |
required |
path |
Path
|
Path where the model should be saved. Include the .joblib file extension. |
required |
Source code in eis_toolkit/prediction/machine_learning_general.py
33 34 35 36 37 38 39 40 41 42 |
|
split_data(*data, split_size=0.2, random_state=None, shuffle=True)
Split data into two parts. Can be used for train-test or train-validation splits.
For more guidance, read documentation of sklearn.model_selection.train_test_split: (https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*data |
Union[ndarray, DataFrame, csr_matrix, List[Number]]
|
Data to be split. Multiple datasets can be given as input (for example X and y), but they need to have the same length. All datasets are split into two and the parts returned (for example X_train, X_test, y_train, y_test). |
()
|
split_size |
float
|
The proportion of the second part of the split. Typically this is the size of test/validation part. The first part will be complemental proportion. For example, if split_size = 0.2, the first part will have 80% of the data and the second part 20% of the data. Defaults to 0.2. |
0.2
|
random_state |
Optional[int]
|
Seed for random number generation. Defaults to None. |
None
|
shuffle |
bool
|
If data is shuffled before splitting. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
List[Union[ndarray, DataFrame, csr_matrix, List[Number]]]
|
List containing splits of inputs (two outputs per input). |
Source code in eis_toolkit/prediction/machine_learning_general.py
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 |
|