Skip to content

Instantly share code, notes, and snippets.

import arperiodogram as arp
ts = arp.TimeSeries(store_df[['visitors']].values,'1',split=0.7)
ts.season_num = 2
ts.lag_num = 2
ts.create_seasons()
ts.phase_correlation()
# ts.set_top_lags([1,2,3])
print("lag values:",ts.top_lags)
print("seasonal periods:",ts.periods)
from fbprophet import Prophet
fbprophet_df = big_df[['visit_date','visitors']].groupby('visit_date').sum().reset_index()
fbprophet_df.columns = ['ds','y']
train = fbprophet_df[:int(len(fbprophet_df)*0.7)]
valid = fbprophet_df[int(len(fbprophet_df)*0.7):]
m = Prophet(daily_seasonality=True,yearly_seasonality=True)
m.fit(train);
future = m.make_future_dataframe(periods=len(valid))
forecast = m.predict(future)
import scipy.optimize as opt
def poly_least_sqs_loss(x,y,w):
hypothesis = w[0]*x[:,0:1] + (x[:,1:2])*w[1] + w[2]*(x[:,1:2])**w[3]
loss = hypothesis-y
return np.sum(loss**2)/len(y)
def poly_function(x, *args_):
w = np.array(args_).T
def poly_least_sqs_loss(x,y,w):
hypothesis = w[0]*x[:,0:1] + w[1]*(x[:,1:2]) + w[2]*(x[:,1:2])**w[3]
loss = hypothesis-y
return np.sum(loss**2)/len(y)
start_time = time.time()
X = train[['sqft_living']].values
y = train[["price"]].values
model = kernel_optimizer(X,y,poly_least_sqs_loss,num_param=4)
model.add_intercept()
X = train[['bedrooms','bathrooms']].values
y = (train['sqft_living'] > np.mean(train['sqft_living'])).reshape(len(train),1)
model = linear_model.LogisticRegression()
model.fit(X, y)
X = train[['bedrooms','bathrooms']].values
start_time = time.time()
model = kernel_optimizer(X,y,liklihood_loss,num_param=3)
model.add_intercept()
model.adjust_bias_parameters(n_parameter_updates=100,random_sample_num=100)
def liklihood_loss(x,y,w):
hypothesis = x.dot(w)
hypothesis = 1/(1+np.exp(-1*hypothesis))
hypothesis[hypothesis<=0.00001] = 0.00001
loss = -1*((1-y).T.dot(np.log(1-hypothesis)) + y.T.dot(np.log(hypothesis)))/len(y)
return loss.flatten()[0]
def sin_least_sqs_loss(x,y,w):
hypothesis = w[0]*x[:,0:1] + np.cos(x[:,1:2]*w[1]-w[2])*w[3]
loss = hypothesis-y
convergence = (best_parameter-np.mean(param_by_iter[-10:,:],axis=0))/(np.std(param_by_iter[-10:,:],axis=0))
if np.all(np.abs(convergence)<1):
print(‘converged’)
break
from ipyparallel import Client
rc = Client(profile='default')
dview = rc[:]
dview.block = True
with dview.sync_imports():
#for some reason, aliases cannot be use
import numpy
import scipy
from scipy import stats
class NNShapeHelper():
def __init__(self,layer_shape,num_inputs,num_outputs):
self.N_inputs = num_inputs
self.N_outputs = num_outputs
self.layer_shape = layer_shape
self.N_layers = len(layer_shape)
def reshape_vector(w):
reshape_w = []
indx = 0
for shape,num in zip([30, 30, 1], [300, 300, 30]):
x = w[indx:num+indx]
if x.size!=num:
continue
x = x.reshape(shape,int(num/shape))
reshape_w.append(x)
indx = indx+num