/
sklearn_helpers.py
117 lines (102 loc) · 4.21 KB
/
sklearn_helpers.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
'''
Created on 03.04.2016
@author: Tobias
'''
import numpy as np
from sklearn.calibration import CalibratedClassifierCV
from sklearn.cross_validation import StratifiedKFold
import matplotlib.pyplot as plt
from sklearn.cross_validation import ShuffleSplit
from sklearn.learning_curve import learning_curve
from sklearn.ensemble.forest import ExtraTreesClassifier
def predict_proba(clfs,X,y,X_test,weights,calibartion=False):
skf = StratifiedKFold(y, n_folds=5,random_state=571)
n = len(clfs)
preds = []
for clf in clfs:
if calibartion == True:
clf = CalibratedClassifierCV(clf,method="isotonic",cv=skf)
clf.fit(X,y)
y_pred = clf.predict_proba(X_test)
preds.append(y_pred)
final_pred = preds.pop(0)
for pred,weight in zip(preds,weights):
final_pred += weight * pred
final_pred = final_pred/np.array(weights).sum()
return final_pred
def feature_importance(clf,X,y,feature_names):
'''
print importance of the features
'''
print("Feature importance of the fitted model")
try:
importances = clf.feature_importances_
indices = np.argsort(importances)[::-1]
for f in range(len(feature_names)):
print("%s : (%f)" % (feature_names[indices[f]], importances[indices[f]]))
except:
print("Error! Classifier has no attribut feature_importances_!")
def select_features(X,y,X_test,n_features=100):
'''
select the top n_features
'''
forest = ExtraTreesClassifier(n_estimators=100,random_state=571)
forest.fit(X,y)
importances = forest.feature_importances_
indices = np.argsort(importances)[::-1]
X = X[:,indices[0:n_features]]
X_test = X_test[:,indices[0:n_features]]
return X,X_test
def plot_learning_curve(estimator, X, y, scoring="accuracy", title="", ylim=None, cv=None,
n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
"""
Generate a simple plot of the test and traning learning curve.
Parameters
----------
estimator : object type that implements the "fit" and "predict" methods
An object of that type which is cloned for each validation.
title : string
Title for the chart.
X : array-like, shape (n_samples, n_features)
Training vector, where n_samples is the number of samples and
n_features is the number of features.
y : array-like, shape (n_samples) or (n_samples, n_features), optional
Target relative to X for classification or regression;
None for unsupervised learning.
ylim : tuple, shape (ymin, ymax), optional
Defines minimum and maximum yvalues plotted.
cv : integer, cross-validation generator, optional
If an integer is passed, it is the number of folds (defaults to 3).
Specific cross-validation objects can be passed, see
sklearn.cross_validation module for the list of possible objects
n_jobs : integer, optional
Number of jobs to run in parallel (default 1).
"""
if cv is None:
cv = ShuffleSplit(len(y), n_iter=10, test_size=0.2, random_state=0)
if title == "":
title = "Learning curves"
plt.figure()
plt.title(title)
if ylim is not None:
plt.ylim(*ylim)
plt.xlabel("Training examples")
plt.ylabel("Score")
train_sizes, train_scores, test_scores = learning_curve(estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes, scoring=scoring)
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
test_scores_std = np.std(test_scores, axis=1)
plt.grid()
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
test_scores_mean + test_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training score")
plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
label="Cross-validation score")
plt.legend(loc="best")
plt.show()
# return plt