From 219b05507355d83393946900780c997ed93439c8 Mon Sep 17 00:00:00 2001 From: Mostafa Abdollahi Sarvi Date: Mon, 28 Jun 2021 18:36:10 +0330 Subject: [PATCH] Add 4 Normalizing methods for data preprocessing and update Readme.md Add 4 Normalizing methods (MinMaxScaler,MaxAbsScale, StandardScaler, StandardScaler with selective mean and variance) for data preprocessing including add 1 newfolder + 5 py files and update Readme.md --- README.md | 6 ++++++ mlfromscratch/normalizing/__init__.py | 4 ++++ mlfromscratch/normalizing/maxabsscaler.py | 11 +++++++++++ mlfromscratch/normalizing/minmaxscaler.py | 13 +++++++++++++ mlfromscratch/normalizing/standardscaler.py | 16 ++++++++++++++++ .../normalizing/standardscalerwithmv.py | 12 ++++++++++++ 6 files changed, 62 insertions(+) create mode 100644 mlfromscratch/normalizing/__init__.py create mode 100644 mlfromscratch/normalizing/maxabsscaler.py create mode 100644 mlfromscratch/normalizing/minmaxscaler.py create mode 100644 mlfromscratch/normalizing/standardscaler.py create mode 100644 mlfromscratch/normalizing/standardscalerwithmv.py diff --git a/README.md b/README.md index 808f72b1..e829a3c9 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ but rather to present the inner workings of them in a transparent and accessible + [Genetic Algorithm](#genetic-algorithm) + [Association Analysis](#association-analysis) * [Implementations](#implementations) + + [Normalizing](#normalizing) + [Supervised Learning](#supervised-learning) + [Unsupervised Learning](#unsupervised-learning) + [Reinforcement Learning](#reinforcement-learning) @@ -267,6 +268,11 @@ but rather to present the inner workings of them in a transparent and accessible ## Implementations +### Normalizing +- [MinMaxScaler](mlfromscratch/normalizing/minmaxscaler.py) +- [StandardScaler](mlfromscratch/normalizing/standardscaler.py) +- [StandardScaler With Selective mean and variance](mlfromscratch/normalizing/standardscalerwithmv.py) +- [MaxAbsScaler](mlfromscratch/normalizing/maxabsscaler.py) ### Supervised Learning - [Adaboost](mlfromscratch/supervised_learning/adaboost.py) - [Bayesian Regression](mlfromscratch/supervised_learning/bayesian_regression.py) diff --git a/mlfromscratch/normalizing/__init__.py b/mlfromscratch/normalizing/__init__.py new file mode 100644 index 00000000..6a168e08 --- /dev/null +++ b/mlfromscratch/normalizing/__init__.py @@ -0,0 +1,4 @@ +from .minmaxscaler import MinMaxScaler +from .standardscaler import StandardScaler +from .standardscalerwithmv import StandardScalerAdv +from .maxabsscaler import MaxAbsScaler diff --git a/mlfromscratch/normalizing/maxabsscaler.py b/mlfromscratch/normalizing/maxabsscaler.py new file mode 100644 index 00000000..779a507e --- /dev/null +++ b/mlfromscratch/normalizing/maxabsscaler.py @@ -0,0 +1,11 @@ +from __future__ import print_function, division +import numpy as np + +def MaxAbsScaler(x): + """ + Parameters : + x : Dataset + + """ + normalized = x/np.max(abs(x)) + return normalized diff --git a/mlfromscratch/normalizing/minmaxscaler.py b/mlfromscratch/normalizing/minmaxscaler.py new file mode 100644 index 00000000..d30a7689 --- /dev/null +++ b/mlfromscratch/normalizing/minmaxscaler.py @@ -0,0 +1,13 @@ +from __future__ import print_function, division +import numpy as np + +def MinMaxScaler(x,minmax): + """ + Parameters : + x : Dataset + minmax : the minimum and maximum range of your features + + """ + normalized = (x-np.min(x))/(np.max(x)-np.min(x)) + scale = (normalized * (np.max(minmax)-np.min(minmax))) + np.min(minmax) + return scale diff --git a/mlfromscratch/normalizing/standardscaler.py b/mlfromscratch/normalizing/standardscaler.py new file mode 100644 index 00000000..10eaef37 --- /dev/null +++ b/mlfromscratch/normalizing/standardscaler.py @@ -0,0 +1,16 @@ +from __future__ import print_function, division +import numpy as np + +def StandardScaler(x,standard=True): + """ + Parameters : + x : Dataset + standard : True or False (True = setting the mean of 0 and var of 1, False = computing mean and var of data) + + """ + if standard == True: + normalized = (x-0)/np.sqrt(1) + + else: + normalized = (x-np.mean(x))/np.std(x) + return normalized diff --git a/mlfromscratch/normalizing/standardscalerwithmv.py b/mlfromscratch/normalizing/standardscalerwithmv.py new file mode 100644 index 00000000..10be47f5 --- /dev/null +++ b/mlfromscratch/normalizing/standardscalerwithmv.py @@ -0,0 +1,12 @@ +from __future__ import print_function, division +import numpy as np + +def StandardScalerAdv(x,mean,var): + """ + Parameters : + x : Dataset + mean : your favorite mean for scaling features + var : your favorite variance for scaling features + + """ + normalized = (x-mean)/np.sqrt(var)