2010-09-03 22:30:17 +08:00
|
|
|
"""
|
|
|
|
|
A comparison of different methods in GLM
|
|
|
|
|
|
|
|
|
|
Data comes from a random square matrix.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
import numpy as np
|
2011-09-03 18:57:53 +08:00
|
|
|
from sklearn import linear_model
|
|
|
|
|
from sklearn.utils.bench import total_seconds
|
2010-09-03 22:30:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
2010-09-07 14:50:25 +08:00
|
|
|
import pylab as pl
|
|
|
|
|
|
2010-11-05 21:54:09 +08:00
|
|
|
n_iter = 40
|
2010-09-03 22:30:17 +08:00
|
|
|
|
2010-12-12 11:07:20 +08:00
|
|
|
time_ridge = np.empty(n_iter)
|
|
|
|
|
time_ols = np.empty(n_iter)
|
|
|
|
|
time_lasso = np.empty(n_iter)
|
2010-09-03 22:30:17 +08:00
|
|
|
|
2011-12-17 03:18:40 +08:00
|
|
|
dimensions = 500 * np.arange(1, n_iter + 1)
|
2010-09-03 22:30:17 +08:00
|
|
|
|
|
|
|
|
for i in range(n_iter):
|
|
|
|
|
|
2013-02-12 06:11:57 +08:00
|
|
|
print('Iteration %s of %s' % (i, n_iter))
|
2010-09-03 22:30:17 +08:00
|
|
|
|
2011-12-17 03:18:40 +08:00
|
|
|
n_samples, n_features = 10 * i + 3, 10 * i + 3
|
2010-09-03 22:30:17 +08:00
|
|
|
|
2010-12-12 11:07:20 +08:00
|
|
|
X = np.random.randn(n_samples, n_features)
|
|
|
|
|
Y = np.random.randn(n_samples)
|
2010-09-03 22:30:17 +08:00
|
|
|
|
|
|
|
|
start = datetime.now()
|
2010-12-12 11:07:20 +08:00
|
|
|
ridge = linear_model.Ridge(alpha=1.)
|
|
|
|
|
ridge.fit(X, Y)
|
2010-09-06 16:13:11 +08:00
|
|
|
time_ridge[i] = total_seconds(datetime.now() - start)
|
2010-09-03 22:30:17 +08:00
|
|
|
|
|
|
|
|
start = datetime.now()
|
2010-11-25 21:53:55 +08:00
|
|
|
ols = linear_model.LinearRegression()
|
2010-12-12 11:07:20 +08:00
|
|
|
ols.fit(X, Y)
|
2010-09-06 16:13:11 +08:00
|
|
|
time_ols[i] = total_seconds(datetime.now() - start)
|
2010-09-03 22:30:17 +08:00
|
|
|
|
|
|
|
|
start = datetime.now()
|
2011-07-26 09:41:48 +08:00
|
|
|
lasso = linear_model.LassoLars()
|
2010-12-12 11:07:20 +08:00
|
|
|
lasso.fit(X, Y)
|
2010-09-06 16:13:11 +08:00
|
|
|
time_lasso[i] = total_seconds(datetime.now() - start)
|
2010-09-03 22:30:17 +08:00
|
|
|
|
2013-05-28 14:27:20 +08:00
|
|
|
pl.figure('scikit-learn GLM benchmark results')
|
|
|
|
|
pl.xlabel('Dimensions')
|
|
|
|
|
pl.ylabel('Time (s)')
|
2010-12-12 11:07:20 +08:00
|
|
|
pl.plot(dimensions, time_ridge, color='r')
|
|
|
|
|
pl.plot(dimensions, time_ols, color='g')
|
|
|
|
|
pl.plot(dimensions, time_lasso, color='b')
|
2010-09-03 22:30:17 +08:00
|
|
|
|
2013-05-28 14:27:20 +08:00
|
|
|
pl.legend(['Ridge', 'OLS', 'LassoLars'], loc='upper left')
|
2010-12-12 11:07:20 +08:00
|
|
|
pl.axis('tight')
|
2010-09-03 22:30:17 +08:00
|
|
|
pl.show()
|