-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyExercise6.py
More file actions
103 lines (71 loc) · 3.06 KB
/
pyExercise6.py
File metadata and controls
103 lines (71 loc) · 3.06 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
from sklearn.datasets import load_boston
boston_data = load_boston()
X = boston_data.data
y = boston_data.target
from sklearn.linear_model import LinearRegression
model = LinearRegression()
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 42)
model.fit(X_train, y_train)
model.score(X_train, y_train)
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X_train, y_train, cv = 5)
scores
from sklearn.metrics import explained_variance_score
explained_variance_score(y_test, model.predict(X_test))
from sklearn.metrics import mean_absolute_error
mean_absolute_error(y_test, model.predict(X_test))
from sklearn.metrics import median_absolute_error
median_absolute_error(y_test, model.predict(X_test))
from sklearn.metrics import r2_score
r2_score(y_test, model.predict(X_test))
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.pipeline import make_pipeline
pipe = make_pipeline(StandardScaler(), PolynomialFeatures(degree = 3), LinearRegression())
pipe.fit(X_train, y_train)
pipe.score(X_train, y_train)
pipe.score(X_test, y_test)
pipe.steps[2][1].coef_
pipe.steps[2][1].coef_.max(), pipe.steps[2][1].coef_.min(), pipe.steps[2][1].coef_.std()
##
from sklearn.linear_model import Ridge
pipe = make_pipeline(StandardScaler(), PolynomialFeatures(degree = 3), Ridge())
pipe.fit(X_train, y_train)
pipe.score(X_train, y_train)
pipe.score(X_test, y_test)
pipe.steps[2][1].coef_.max(), pipe.steps[2][1].coef_.min(), pipe.steps[2][1].coef_.std()
##
from sklearn.linear_model import Lasso
pipe = make_pipeline(StandardScaler(), PolynomialFeatures(degree = 3), Lasso())
pipe.fit(X_train, y_train)
pipe.score(X_train, y_train)
pipe.score(X_test, y_test)
pipe.steps[2][1].coef_.max(), pipe.steps[2][1].coef_.min(), pipe.steps[2][1].coef_.std()
##
from sklearn.linear_model import ElasticNet
pipe = make_pipeline(StandardScaler(), PolynomialFeatures(degree = 3), ElasticNet())
pipe.fit(X_train, y_train)
pipe.score(X_train, y_train)
pipe.score(X_test, y_test)
pipe.steps[2][1].coef_.max(), pipe.steps[2][1].coef_.min(), pipe.steps[2][1].coef_.std()
####
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn import linear_model
dataset = datasets.load_iris()
dataset.data.shape
dataset.target.shape
holdout_percent = 0.4
X_train, X_test, y_train, y_test = train_test_split(dataset.data, \
dataset.target, test_size = holdout_percent, random_state = 42)
model = linear_model.LogisticRegression(penalty = 'l2', dual = False, tol = 0.0001, \
C = 1.0, fit_intercept = True).fit(X_train, y_train)
model.score(X_train, y_train)
from sklearn.metrics import accuracy_score
accuracy_score(y_test, model.predict(X_test))
from sklearn.metrics import cohen_kappa_score
cohen_kappa_score(y_test, model.predict(X_test))
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, model.predict(X_test))
###