I'm trying to apply the Simple Linear regression with gradient descent but I got stuck here
I get an error ValueError
ValueError: Unable to coerce to Series, length must be 1: given 506 can anyone help me what it the wrong in this code
X_train_std: standarding value of X, X.shape = (506, 13) X have int values y.shape = (506,1)
class LinearRegressionGD(object):
def __init__(self, eta = 0.001, n_iter = 20):
self.eta = eta
self.n_iter = n_iter
def fit(self, X_train_std, y):
self.w_ = np.zeros(1+X_train_std.shape[1])
self.cost = []
for i in range(self.n_iter):
output = self.net_input(X_train_std)
errors = (y - output)
self.w_[1:] += self.eta * X_train_std.T.dot(errors)
self.w_[0] += self.eta * errors.sum()
cost = (errors**2).sum() /2.0
self.cost_.append(cost)
return self
def net_input(self,X_train_std):
return np.dot(X_train_std, self.w_[1:]) + self.w_[0]
def predict(self, X_train_std):
return self.net_input(X_train_std)
lr = LinearRegressionGD()
lr.fit(X_train_std, y)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-24-29cc349fb131> in <module>()
1 lr = LinearRegressionGD()
----> 2 lr.fit(X_train_std, y)
3 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/ops/__init__.py in to_series(right)
637 if len(left.columns) != len(right):
638 raise ValueError(
--> 639 msg.format(req_len=len(left.columns), given_len=len(right))
640 )
641 right = left._constructor_sliced(right, index=left.columns)
output
is one dimensional while y
is two dimensional. That's why you have to reshape the output
.
output = self.net_input(X_train_std).reshape(-1, 1)
Also I get no convergence with your weights updates. If you replace it with
self.w_[1:] += self.eta * np.sum(errors * X_train_std, axis=0)
and probably tweak learning rate it should work fine.