-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyExercise5.py
More file actions
64 lines (47 loc) · 1.8 KB
/
pyExercise5.py
File metadata and controls
64 lines (47 loc) · 1.8 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
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)
from sklearn.model_selection import cross_val_score
model = linear_model.LogisticRegression(penalty = 'l2', dual = False)
scores = cross_val_score(model, dataset.data, dataset.target, cv = 5)
scores.mean(), scores.std() * 2
cross_val_score(model, dataset.data, dataset.target, cv = 5, scoring = 'f1_macro')
###
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
# 1
Pipeline([('pca', PCA()), ('classifier', SVC())])
# or 2
from sklearn.pipeline import make_pipeline
pipe = make_pipeline(PCA(), SVC())
pipe.steps[1]
pipe.named_steps["svc"]
pipe.named_steps["svc"].decision_function_shape
###
pipe.fit(X_train, y_train)
pipe.score(X_train, y_train)
pipe.score(X_test, y_test)
model.score(X_test, y_test)
###
param_grid = [
{ 'C': [1, 10, 100, 1000], 'kernel': ['linear']},
{'C': [1, 10, 100, 1000], 'gamma':[0.001, 0.0001], 'kernel': ['rbf']},
]
from sklearn.model_selection import GridSearchCV
model = GridSearchCV(SVC(), param_grid, cv = 5, return_train_score = True)
model.fit(X_train, y_train)
import pandas as pd
pd.DataFrame(model.cv_results_)
##
model = GridSearchCV(pipe, dict(svc__C = [0.1, 10, 100]), cv = 5, return_train_score = True)
model.fit(X_train, y_train)