-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocessing_functions.py
More file actions
executable file
·197 lines (145 loc) · 6.61 KB
/
Copy pathPreprocessing_functions.py
File metadata and controls
executable file
·197 lines (145 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
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
189
190
191
192
193
194
195
196
197
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.feature_selection import SelectKBest
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import learning_curve
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import IsolationForest
### dropping unusfull data now
def imputation(data) :
# if there is problem of data, just decomment these two lines and comment the next one
#data.drop('Category', axis=1, inplace=True)
#data.dropna(axis=0, inplace=True)
data.fillna(0, inplace=True)
return data
## converting string to numbers
def encodage(data):
for col in data.select_dtypes("object") :
data[col]= data[col].astype("category").cat.codes
return data
def scaler(data):
min_max = MinMaxScaler()
data = pd.DataFrame(min_max.fit_transform(data), columns=data.columns)
return data
def preprocess_pipe(data=None):
data = imputation(data)
data = encodage(data)
data = scaler(data)
target = data['MSRP']
features = data.drop('MSRP', axis=1)
X_train, X_test, y_train, y_test = train_test_split(features, target, random_state=42, test_size=0.3)
return X_train, X_test, y_train, y_test
### MODELISATION
def modelisation(model=None, X_train=None,
y_train=None, X_test=None,
y_test=None):
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
train_size, train_score, val_score = learning_curve(model, X_train, y_train, train_sizes=np.linspace(0.1, 1., 10))
plt.figure(figsize=(14,9))
plt.plot(train_size, train_score.mean(axis=1), label='training')
plt.plot(train_size, val_score.mean(axis=1), label='validation')
plt.scatter( [], [], label=model)
plt.legend()
plt.show()
def poly_pipeline(model, degre=2, k=13):
return make_pipeline(PolynomialFeatures
(degree=degre, include_bias=False),
SelectKBest(k=k), model)
Linear = poly_pipeline(LinearRegression())
class Preprocess :
def __repr__(self):
return 'Here we are in class Preprocessing \nType "dir(Preprocess)", to see all alternatives'
## Preprocess : Splitting in train and test sets
def split(self, data, target):
self.X = data.drop(target, axis=1)
self.Y = data[target]
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(self.X, self.Y, random_state=0, train_size=0.8)
print('Train : ',self.X_train.shape,'\nTest : ', self.X_test.shape)
return self.X_train, self.X_test, self.y_train, self.y_test
## Preprocess : dealing with missing values
def missings(self, data):
# if to delete data use dropna from pandas
data = data.dropna(axis=0)
return data
# if need to fill NA values use impute methods
#from sklearn.impute import SimpleImputer
#self.model = SimpleImputer(strategy='most_frequent')
#self.model.fit(data)
#return self.model.transform(data)
## Preprocess : converting string to int
def encoder(self, data):
for self.dt in data.select_dtypes("object"):
data[self.dt] = data[self.dt].astype('category').cat.codes
return data
# if need for onehotencoding
#from sklearn.preprocessing import OneHotEncoder
#self.model = OneHotEncoder()
#for dt in data.select_dtypes(include='object'):
#data[dt] = self.model.fit_transform(data[dt])
## Preprocess : Standardisation
def standar(self, data=None):
self.model = StandardScaler()
return pd.DataFrame(self.model.fit_transform(data), columns=data.columns)
## Preprocess : handling outilers and inliers
def outliers(self,data=None):
# in case of needof PCA
#from sklearn.decomposition import PCA
#self.model = PCA(n_components=0.95)
#self.model.fit(data)
#return self.model.transform(data)
self.model = IsolationForest(contamination= 0.02)
return self.model.fit_transform(data)
## Preprocess : in case of need of polynomialFeatures
def poly_features(self, degre=2):
self.model = PolynomialFeatures(degre)
return self.model
## Preprocess : regression model just for testing preprocess
def regression(self, model=None, X_train=None, y_train=None, X_test=None, y_test=None):
self.model = model
self.model.fit(X_train, y_train)
return self.model.score(X_test, y_test)
## Preprocess : classification model just for testing preprocess
def classification(self, model=None, X_train=None, y_train=None, X_test=None, y_test=None):
self.model = model
self.model.fit(X_train, y_train)
return self.model.score(X_test, y_test)
def curves(self,model=None, X_train=None, y_train=None):
N, train, val = learning_curve(model, X_train, y_train, train_sizes=np.linspace(.1,1.,10))
plt.plot(N, train.mean(axis=1), label="training")
plt.plot(N, val.mean(axis=1), label="validation")
plt.scatter([],[], label=model.__class__.__name__)
plt.legend()
# data of float type distributions
def plotFloat(data):
import seaborn as sb
from random import choice
liste =["#900", "yellow", "#0ba", "#330", "#109", '#df8', "#890", "#109", "#FDC", "#011",'#50b']
length = int(len(data.select_dtypes(exclude='object').columns))
plt.figure(figsize=(12,10))
for num,i in zip(data.select_dtypes(exclude='object'), range(1, length+1 )):
plt.subplot((length//4)+1, 4, i)
sb.distplot(data[num], color=choice(liste))
plt.show()
def print_objects(df):
""" Print each object type with its names and features names """
for types in df.dtypes():
print(f"Main type of {types}")
for col in df.select_dtypes(str(types)):
print(f'Pour la variable {col :-<40} on a les éléments {df[col].unique()}')
def plot_objects_pie(df):
""" Plot all object pielike"""
p = int(np.sqrt(df.columns[df.dtypes == 'object'].size))+1
plt.figure(figsize=(20,18))
i = 0
for var in df.columns[df.dtypes == 'object']:
plt.subplot(p, p, i+1)
df[var].value_counts().plot.pie()
i += 1
plt.tight_layout()
plt.show()