Commit a33d52f2 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Added L2B_P._MachineLearner_RSImage(), L2B_P.LinearRegression_RSImage(),...

Added L2B_P._MachineLearner_RSImage(), L2B_P.LinearRegression_RSImage(), L2B_P.RidgeRegression_RSImage()


Former-commit-id: 979bdc22
Former-commit-id: d99f8c50
parent daa46810
......@@ -11,11 +11,14 @@ import scipy as sp
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from pandas import DataFrame
from pandas.plotting import scatter_matrix
from typing import Union, List # noqa F401 # flake8 issue
from tqdm import tqdm
from multiprocessing import Pool, cpu_count
from sklearn.cluster import k_means_ # noqa F401 # flake8 issue
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, RidgeClassifier
from geoarray import GeoArray # noqa F401 # flake8 issue
from py_tools_ds.processing.progress_mon import ProgressBar
......@@ -320,6 +323,90 @@ class KMeansRSImage(object):
return random_samples
class _MachineLearner_RSImage(object):
def __init__(self, im_X, im_Y, test_size):
self.im_X = GeoArray(im_X)
self.im_Y = GeoArray(im_Y)
# Set spectra (3D to 2D conversion)
self.spectra_X = self._im2spectra(self.im_X)
self.spectra_Y = self._im2spectra(self.im_Y)
# Set train and test variables
# NOTE: If random_state is set to an Integer, train_test_split will always select the same 'pseudo-random' set
# of the input data.
self.train_X, self.test_X, self.train_Y, self.test_Y = \
train_test_split(self.spectra_X, self.spectra_Y, test_size=test_size, shuffle=True, random_state=0)
@staticmethod
def _im2spectra(geoArr):
return geoArr.reshape((geoArr.rows * geoArr.cols, geoArr.bands))
def plot_scatter_matrix(self, figsize=(15, 15)):
train_X = self.train_X[np.random.choice(self.train_X.shape[0], 1000, replace=False), :]
train_Y = self.train_Y[np.random.choice(self.train_Y.shape[0], 1000, replace=False), :]
df = DataFrame(train_X, columns=['Band %s' % b for b in range(1, self.im_X.bands + 1)])
scatter_matrix(df, figsize=figsize, marker='.', hist_kwds={'bins': 50}, s=30, alpha=0.8)
plt.suptitle('Image X')
df = DataFrame(train_Y, columns=['Band %s' % b for b in range(1, self.im_Y.bands + 1)])
scatter_matrix(df, figsize=figsize, marker='.', hist_kwds={'bins': 50}, s=30, alpha=0.8)
plt.suptitle('Image Y')
class LinearRegression_RSImage(_MachineLearner_RSImage):
def __init__(self, im_X, im_Y, test_size=0.6):
super(LinearRegression_RSImage, self).__init__(im_X, im_Y, test_size=test_size)
self.linRegressor = LinearRegression().fit(self.train_X, self.train_Y)
@property
def coefficients_(self):
# type: () -> np.ndarray
"""Return derived coefficients of the fitted function (also called 'weights' or 'slope' parameters)."""
return self.linRegressor.coef_
@property
def intercept_(self):
# type: () -> np.ndarray
"""Return derived intercept (or 'offset') of the fitted function."""
return self.linRegressor.intercept_
@property
def scores(self):
"""The R² values of the regression for the train and the test data."""
return dict(train=self.linRegressor.score(self.train_X, self.train_Y),
test=self.linRegressor.score(self.test_X, self.test_Y))
class RidgeRegression_RSImage(_MachineLearner_RSImage):
def __init__(self, im_X, im_Y, test_size=0.6):
# TODO
raise NotImplementedError()
super(RidgeRegression_RSImage, self).__init__(im_X, im_Y, test_size=test_size)
self.ridgeRegressor = LinearRegression().fit(self.train_X, self.train_Y)
@property
def coefficients_(self):
# type: () -> np.ndarray
"""Return derived coefficients of the fitted function (also called 'weights' or 'slope' parameters)."""
return self.ridgeRegressor.coef_
@property
def intercept_(self):
# type: () -> np.ndarray
"""Return derived intercept (or 'offset') of the fitted function."""
return self.ridgeRegressor.intercept_
@property
def scores(self):
"""The R² values of the regression for the train and the test data."""
return dict(train=self.ridgeRegressor.score(self.train_X, self.train_Y),
test=self.ridgeRegressor.score(self.test_X, self.test_Y))
class SpecHomo_Classifier(object):
def __init__(self, filelist_refs, v=False, logger=None, CPUs=None):
# type: (List[str], bool, logging.Logger, Union[None, int]) -> None
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment