Skip to content

Instantly share code, notes, and snippets.

@regivm
Created April 22, 2020 03:27
Show Gist options
  • Save regivm/18d643b879d127aff0677b339e85c9e3 to your computer and use it in GitHub Desktop.
Save regivm/18d643b879d127aff0677b339e85c9e3 to your computer and use it in GitHub Desktop.
boston = datasets.load_boston()
y = boston.target
X = pd.DataFrame(boston.data, columns = boston.feature_names)
ss = StandardScaler()
Xs = ss.fit_transform(X)
Xs = pd.DataFrame(Xs, columns=boston.feature_names)
Xs['const'] = 1
# calculate y
def calc_y(weights):
yc = 0
for i in range(Xs.shape[1]):
yc = yc + weights[i]*Xs.iloc[:,i]
return yc
# define objective
def objective(weights):
return np.mean(((calc_y(weights)-y))**2)
# initial guesses
x0 = np.zeros(Xs.shape[1])
# show initial objective
print('Initial SSE Objective: ' + str(objective(x0)))
# optimize
# bounds on variables
b = (-100, 100)
bnds = [(b)]*Xs.shape[1]
solution = minimize(objective,x0,method='SLSQP',bounds=bnds)
weights = solution.x
y_pred = calc_y(weights)
# show final objective
print('Final SSE Objective: ' + str(objective(weights)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment