-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.py
More file actions
43 lines (32 loc) · 1.11 KB
/
Copy pathpipeline.py
File metadata and controls
43 lines (32 loc) · 1.11 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
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import Imputer
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import ElasticNet
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
# ==================================
df = pd.read_csv("gm_2008_region.csv")
X = df['fertility'].values.reshape(-1,1)
y = df['life'].values.reshape(-1,1)
steps = [
('imputation',Imputer(missing_values='NaN',strategy='mean',axis=0)),
('scaler',StandardScaler()),
('elasticnet',ElasticNet())
]
pipeline = Pipeline(steps)
parameters = {
'elasticnet__l1_ratio': np.linspace(0,1,30)
}
gcv = GridSearchCV(pipeline,parameters,cv=5)
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=42)
gcv.fit(X_train,y_train)
score = gcv.score(X_test,y_test)
best_hypaparameters = gcv.best_params_
y_predict = gcv.predict(X_test)
plt.scatter(X_test,y_test)
plt.plot(X_test,y_predict,c='orange')
plt.show()