scikit-learn/benchmarks/bench_glm.py

59 lines
1.5 KiB
Python
Raw Normal View History

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__':
import pylab as pl
n_iter = 40
2010-09-03 22:30:17 +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
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()
ridge = linear_model.Ridge(alpha=1.)
ridge.fit(X, Y)
time_ridge[i] = total_seconds(datetime.now() - start)
2010-09-03 22:30:17 +08:00
start = datetime.now()
ols = linear_model.LinearRegression()
ols.fit(X, Y)
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()
lasso.fit(X, Y)
time_lasso[i] = total_seconds(datetime.now() - start)
2010-09-03 22:30:17 +08:00
pl.figure('scikit-learn GLM benchmark results')
pl.xlabel('Dimensions')
pl.ylabel('Time (s)')
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
pl.legend(['Ridge', 'OLS', 'LassoLars'], loc='upper left')
pl.axis('tight')
2010-09-03 22:30:17 +08:00
pl.show()