-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseleccion.py
156 lines (129 loc) · 5.67 KB
/
seleccion.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# coding=UTF-8
import pandas as pd
import numpy as np
import sklearn.linear_model as lm
from sklearn.preprocessing import StandardScaler
from matplotlib import pyplot as plt
def fss(x, y, names_x, test_data, k=10000):
p = x.shape[1] - 1
k = min(p, k)
names_x = np.array(names_x)
remaining = range(0, p)
selected = [p]
points_training = []
points_test = []
while remaining and len(selected) <= k:
score_candidates = []
for candidate in remaining:
model = lm.LinearRegression(fit_intercept=False)
indexes = selected + [candidate]
x_train = x[:, indexes]
model.fit(x_train, y) # fitting with training data
# predicting training data
predictions_train = model.predict(x_train)
residuals_train = predictions_train - y
# predicting test data
x_test, y_test = test_data
x_test = x_test[:, indexes]
predictions_test = model.predict(x_test)
residuals_test = predictions_test - y_test
mse_candidate = np.mean(np.power(residuals_train, 2))
mse_test = np.mean(np.power(residuals_test, 2))
# Computing z scores
var = (mse_candidate * x_train.shape[0]) / (x_train.shape[0] - x_train.shape[1] - 1)
diag_values = np.diag(np.linalg.pinv(np.dot(x_train.T, x_train)))
z_scores = np.divide(model.coef_, np.sqrt(np.multiply(var, diag_values)))
# getting the z score that belongs to the last feature added
z_score_candidate = z_scores[-1]
# Using absolute value of the z score
score_candidates.append((abs(z_score_candidate), mse_candidate, mse_test, candidate))
score_candidates.sort()
best_new_z_score, mse_candidate, mse_test, best_candidate = score_candidates.pop()
remaining.remove(best_candidate)
selected.append(best_candidate)
points_training.append((len(selected), mse_candidate))
points_test.append((len(selected), mse_test))
print "selected= %s..." % names_x[best_candidate]
print "totalvars=%d, best z_score = %f, mse = %f" % (len(indexes), best_new_z_score, mse_candidate)
return selected, points_training, points_test
def bss(x, y, names_x, test_data, k=10000):
p = x.shape[1] - 1
k = min(p, k)
names_x = np.array(names_x)
selected = range(0, p)
removed = []
points_training = []
points_test = []
while len(selected) >= 1:
model = lm.LinearRegression(fit_intercept=False)
indexes = selected + [p]
x_train = x[:, indexes]
model.fit(x_train, y) # fitting with training data
# predicting training data
predictions_train = model.predict(x_train)
residuals_train = predictions_train - y
# predicting test data
x_test, y_test = test_data
x_test = x_test[:, indexes]
predictions_test = model.predict(x_test)
residuals_test = predictions_test - y_test
mse_train = np.mean(np.power(residuals_train, 2))
mse_test = np.mean(np.power(residuals_test, 2))
# Computing z scores
var = (mse_train * x_train.shape[0]) / (x_train.shape[0] - x_train.shape[1] - 1)
diag_values = np.diag(np.linalg.pinv(np.dot(x_train.T, x_train)))
z_scores = np.divide(model.coef_, np.sqrt(np.multiply(var, diag_values)))
score_candidates = zip(np.abs(z_scores), indexes)
score_candidates.sort(reverse=True)
# removing the worst
worst_new_z_score, worst_candidate = score_candidates.pop()
selected.remove(worst_candidate)
removed.append(worst_candidate)
points_training.append((len(indexes), mse_train))
points_test.append((len(indexes), mse_test))
print "selected= %s..." % names_x[worst_candidate]
print "totalvars=%d, worst z_score = %f, mse = %f" % (len(indexes), worst_new_z_score, mse_train)
return removed, points_training, points_test
def plot(training, test, desc=True):
x_training, y_training = zip(*training)
x_test, y_test = zip(*test)
plt.plot(x_training, y_training, color="b", linestyle="-", marker='o', label='Training set')
plt.plot(x_test, y_test, color="r", linestyle="-", marker='o', label='Test set')
if desc:
plt.xlim(max(x_training), min(x_training))
plt.ylabel('MSE')
plt.xlabel(u'Número de atributos')
plt.legend(loc=1)
plt.show()
# url = 'http://statweb.stanford.edu/~tibs/ElemStatLearn/datasets/prostate.data'
# df = pd.read_csv(url, sep='\t', header=0)
df = pd.read_csv('prostate.data', sep='\t', header=0)
df = df.drop('Unnamed: 0', axis=1)
istrain_str = df['train']
istrain = np.asarray([True if s == 'T' else False for s in istrain_str])
istest = np.logical_not(istrain)
df = df.drop('train', axis=1)
scaler = StandardScaler()
df_scaled = pd.DataFrame(scaler.fit_transform(df), columns=df.columns)
df_scaled['lpsa'] = df['lpsa']
X = df_scaled.ix[:, :-1]
N = X.shape[0]
X.insert(X.shape[1], 'intercept', np.ones(N))
y = df_scaled['lpsa']
Xtrain = X[istrain]
ytrain = y[istrain]
Xtest = X[np.logical_not(istrain)]
ytest = y[np.logical_not(istrain)]
Xm_train = Xtrain.as_matrix()
ym_train = ytrain.as_matrix()
Xm_test = Xtest.as_matrix()
ym_test = ytest.as_matrix()
names_regressors = ["lcavol", "lweight",
"age", "lbph", "svi", "lcp",
"gleason", "pgg45"]
selected, points_training, points_test = fss(Xm_train, ym_train, names_regressors, (Xm_test, ym_test))
print selected
plot(points_training, points_test, desc=False)
removed, points_training, points_test = bss(Xm_train, ym_train, names_regressors, (Xm_test, ym_test))
print removed
plot(points_training, points_test)