-
Notifications
You must be signed in to change notification settings - Fork 2
/
pipe.py
224 lines (185 loc) · 7.88 KB
/
pipe.py
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
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
import pandas as pd
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.calibration import LabelEncoder
from sklearn.discriminant_analysis import StandardScaler
from sklearn.impute import KNNImputer, SimpleImputer
from sklearn.pipeline import Pipeline
class FullPipeline1:
def __init__(self) :
# Define columns based on their types
self.date_cols=['Date/Time']
self.numerical_cols=['Wind Speed (m/s)', 'Theoretical_Power_Curve (KWh)', 'Wind Direction (°)','Week','Month','Hour','Day']
self.MLE_cols=['Season']
# Define the full preprocessing pipeline
self.full_pipeline=Pipeline([
('extract_date',DateExtractor(date_cols=self.date_cols)),
('impute_num',DataFrameImputer(knn_cols=self.numerical_cols)),
('remove_outlier',OutlierThresholdTransformer(column=self.numerical_cols)),
('scale', StandardScaleTransform(cols=self.numerical_cols)),
('one_hot_encode', CustomOneHotEncoder(columns=self.MLE_cols)),
('drop', DropColumnsTransformer(columns=self.date_cols)),
])
def fit_transform(self, X_train):
# Fit and transform the training data
X_train = self.full_pipeline.fit_transform(X_train)
return X_train
def transform(self, X_test):
# Transform the testing data
X_test = self.full_pipeline.transform(X_test)
return X_test
# Custom Transformer to Label Encode Categorical Columns
class LabelEncodeColumns(BaseEstimator, TransformerMixin):
def __init__(self, columns):
self.columns = columns
self.encoders_ = {}
def fit(self, X, y=None):
for col in self.columns:
encoder = LabelEncoder()
encoder.fit(X[col])
self.encoders_[col] = encoder
return self
def transform(self, X):
X_copy = X.copy()
for col, encoder in self.encoders_.items():
X_copy[col] = encoder.transform(X_copy[col])
return X_copy
def fit_transform(self, X, y=None):
self.fit(X, y)
return self.transform(X)
# Custom Transformer to apply One-Hot Encoding
class CustomOneHotEncoder(BaseEstimator, TransformerMixin):
def __init__(self, columns=None):
self.columns = columns
self.unique_values = {}
self.feature_names_ = None
def fit(self, X, y=None):
if self.columns is None:
self.columns = X.columns.tolist()
self.unique_values = {col: X[col].unique() for col in self.columns}
self.feature_names_ = self._get_feature_names()
return self
def _get_feature_names(self):
feature_names = []
for col in self.columns:
for value in self.unique_values[col]:
feature_names.append(f"{col}_{value}")
return feature_names
def transform(self, X):
X_transformed = pd.DataFrame(index=X[self.columns].index)
for col in self.columns:
for value in self.unique_values[col]:
X_transformed[f"{col}_{value}"] = (X[col] == value).astype(int)
X = pd.concat([X, X_transformed], axis=1)
return X.drop(columns=['Season'])
def fit_transform(self, X, y=None):
self.fit(X)
return self.transform(X)
# Custom Transformer to Drop Columns
class DropColumnsTransformer(BaseEstimator, TransformerMixin):
def __init__(self, columns=None):
self.columns = columns
def fit(self, X, y=None):
return self
def transform(self, X):
if self.columns is None:
return X
else:
return X.drop(self.columns, axis=1)
# Custom Transformer to Remove Outliers
class OutlierThresholdTransformer(BaseEstimator, TransformerMixin):
def __init__(self, column, q1=0.25, q3=0.75):
self.column = column
self.q1 = q1
self.q3 = q3
def outlier_threshhold(self, dataframe, column):
Q1 = dataframe[column].quantile(self.q1)
Q3 = dataframe[column].quantile(self.q3)
iqr = Q3 - Q1
up_limit = Q3 + 1.5 * iqr
low_limit = Q1 - 1.5 * iqr
return low_limit, up_limit
def fit(self, X, y=None):
return self
def transform(self, X):
X_copy = X.copy()
for col in self.column:
low_limit, up_limit = self.outlier_threshhold(X_copy, col)
X_copy.loc[(X_copy[col] < low_limit), col] = low_limit
X_copy.loc[(X_copy[col] > up_limit), col] = up_limit
return X_copy
def fit_transform(self, X, y=None):
return self.transform(X)
# Custom Transformer to Extract Date Features
class DateExtractor(BaseEstimator, TransformerMixin):
def __init__(self, date_cols):
self.date_cols = date_cols
def fit(self, X, y=None):
return self
def transform(self, X):
extracted_features = []
for col in self.date_cols:
dates = pd.to_datetime(X[col], format='%d %m %Y %H:%M')
for date in dates:
month_val = date.month
week_val = date.day // 7 + 1
day_val = date.day
hour_val = date.hour + 1
# Determining season based on month
if month_val in [3, 4, 5]:
season_val = 'Spring'
elif month_val in [6, 7, 8]:
season_val = 'Summer'
elif month_val in [9, 10, 11]:
season_val = 'Autumn'
else:
season_val = 'Winter'
extracted_features.append([month_val, week_val, day_val, season_val, hour_val])
# Convert the extracted features list to a DataFrame
X_date = pd.DataFrame(extracted_features, columns=['Month', 'Week', 'Day', 'Season', 'Hour'])
X_new=pd.concat([X.reset_index(drop=True),X_date],axis=1)
return X_new
def fit_transform(self, X, y=None):
self.fit(X)
return self.transform(X)
# Custom Transformer to Impute Missing Values in DataFrame
class DataFrameImputer(TransformerMixin, BaseEstimator):
def __init__(self, median_cols=None, knn_cols=None):
self.median_cols = median_cols
self.knn_cols = knn_cols
def fit(self, X, y=None):
self.median_imputer = SimpleImputer(strategy='median')
self.knn_imputer = KNNImputer()
if self.median_cols is not None:
self.median_imputer.fit(X[self.median_cols])
if self.knn_cols is not None:
self.knn_imputer.fit(X[self.knn_cols])
return self
def transform(self, X):
X_imputed = X.copy()
if self.median_cols is not None:
X_median = pd.DataFrame(self.median_imputer.transform(X[self.median_cols]),
columns=self.median_cols, index=X.index)
X_imputed = pd.concat([X_imputed.drop(self.median_cols, axis=1), X_median], axis=1)
if self.knn_cols is not None:
X_knn = pd.DataFrame(self.knn_imputer.transform(X[self.knn_cols]),
columns=self.knn_cols, index=X.index)
X_imputed = pd.concat([X_imputed.drop(self.knn_cols, axis=1), X_knn], axis=1)
return X_imputed
def fit_transform(self, X, y=None):
self.fit(X)
return self.transform(X)
# Custom Transformer to Standardize Numerical Columns
class StandardScaleTransform(BaseEstimator, TransformerMixin):
def __init__(self, cols):
self.cols = cols
self.scaler_ = None
def fit(self, X, y=None):
self.scaler_ = StandardScaler().fit(X.loc[:, self.cols])
return self
def transform(self, X):
X_copy = X.copy()
X_copy.loc[:, self.cols] = self.scaler_.transform(X_copy.loc[:, self.cols])
return X_copy
def fit_transform(self, X, y=None):
self.scaler_ = StandardScaler().fit(X.loc[:, self.cols])
return self.transform(X)