Repository navigation
Expand file tree
/
Copy pathpreprocess.py
More file actions
58 lines (52 loc) · 1.85 KB
/
Copy pathpreprocess.py
File metadata and controls
58 lines (52 loc) · 1.85 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
import pandas as pd
from sklearn.feature_selection import RFE, SelectFromModel, SelectKBest, f_regression, SequentialFeatureSelector
from sklearn.linear_model import LinearRegression, Lasso, LogisticRegression
from sklearn.ensemble import RandomForestRegressor, GradientBoostingClassifier
from sklearn.model_selection import StratifiedKFold, train_test_split
raw_data = pd.read_csv('./train.csv')
X = raw_data.iloc[:,:-1]
y = raw_data['defects'].astype(int)
col_names = raw_data.columns
## RFE method
# rfe = RFE(
# estimator=model,
# n_features_to_select=19
# )
# rfe.fit(X, Y)
#
# X_transformed = rfe.transform(X)
# pass
## Select from model method
# model = RandomForestRegressor()
# # select from the model
# sfm = SelectFromModel(estimator=model)
# X_transformed = sfm.fit_transform(X, Y)
# # see which features were selected
# support = sfm.get_support()
# # get feature names
# selected_feature = [
# x for x, y in zip(col_names, support) if y == True
# ]
# print(selected_feature, len(selected_feature))
# print()
# print(sfm.threshold_)
#wrapper method
model = Lasso()
sfs_f = SequentialFeatureSelector(model,
# n_features_to_select=19,
direction='forward',
scoring='roc_auc')
# fit the object to the training data
sfs_f = sfs_f.fit(X, y)
support_f = sfs_f.get_support()
selected_feature_f = [x for x, y in zip(col_names, support_f) if y == True]
print(selected_feature_f)
sfs_b = SequentialFeatureSelector(model,
# n_features_to_select=19,
direction='backward',
scoring='roc_auc')
# fit the object to the training data
sfs_b = sfs_b.fit(X, y)
support_b = sfs_b.get_support()
selected_feature_b = [x for x, y in zip(col_names, support_b) if y == True]
print(selected_feature_b)