In [1]:
from sklearn import datasets, linear_model, metrics
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures
import math, scipy, numpy as np
from scipy import linalg
In [2]:
data_set = datasets.load_diabetes()
In [3]:
x_trn,x_tst,y_trn,y_tst = train_test_split(data_set.data,data_set.target,test_size=0.2)
In [5]:
x_trn.shape,x_tst.shape,y_trn.shape,y_tst.shape
Out[5]:
In [7]:
feature_names=['age', 'sex', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6']
In [6]:
lr = linear_model.LinearRegression()
In [8]:
def regr_metrics(act, pred):
return (math.sqrt(metrics.mean_squared_error(act, pred)),
metrics.mean_absolute_error(act, pred))
In [9]:
%timeit lr.fit(x_trn,y_trn)
In [10]:
pred = lr.predict(x_tst)
In [11]:
regr_metrics(y_tst,pred)
Out[11]:
In [12]:
poly = PolynomialFeatures(include_bias=False)
trn_feat = poly.fit_transform(x_trn)
In [13]:
', '.join(poly.get_feature_names(feature_names))
Out[13]:
In [14]:
trn_feat.shape
Out[14]:
In [15]:
lr.fit(trn_feat,y_trn)
Out[15]:
In [17]:
regr_metrics(y_tst,lr.predict(poly.fit_transform(x_tst)))
Out[17]:
In [25]:
%timeit poly.fit_transform(x_trn)
In [34]:
from numba import jit, vectorize, guvectorize, cuda, float32, void, float64
In [26]:
import math, numpy as np, matplotlib.pyplot as plt
from pandas_summary import DataFrameSummary
from scipy import ndimage
In [27]:
# Untype and Unvectorized
def proc_python(xx,yy):
zz = np.zeros(nobs, dtype='float32')
for j in range(nobs):
x, y = xx[j], yy[j]
x = x*2 - ( y * 55 )
y = x + y*2
z = x + y + 99
z = z * ( z - .88 )
zz[j] = z
return zz
In [28]:
nobs = 10000
x = np.random.randn(nobs).astype('float32')
y = np.random.randn(nobs).astype('float32')
In [29]:
%timeit proc_python(x,y) # Untyped and unvectorized
In [30]:
def proc_numpy(x,y):
z = np.zeros(nobs,dtype='float32')
x = x*2-(y*55)
y = x+y*2
z = x+y+99
z = z*(z-.88)
return z
In [31]:
np.allclose(proc_numpy(x,y),proc_python(x,y),atol=1e-4)
Out[31]:
In [32]:
%timeit proc_numpy(x,y)
In [35]:
@jit()
def proc_numba(xx,yy,zz):
for j in range(nobs):
x, y = xx[j], yy[j]
x = x*2 - ( y * 55 )
y = x + y*2
z = x + y + 99
z = z * ( z - .88 )
zz[j] = z
return zz
In [36]:
z = np.zeros(nobs).astype('float32')
np.allclose( proc_numpy(x,y), proc_numba(x,y,z), atol=1e-4 )
Out[36]:
In [37]:
%timeit proc_numba(x,y,z)
In [38]:
@vectorize
def vec_numba(x,y):
x = x*2 - ( y * 55 )
y = x + y*2
z = x + y + 99
return z * ( z - .88 )
In [39]:
np.allclose(vec_numba(x,y), proc_numba(x,y,z), atol=1e-4 )
Out[39]:
In [40]:
%timeit vec_numba(x,y)
In [ ]: