python 3.x - numpy.dot() gives TypeError: can't multiply sequence by non-int of type 'float' -
i have begun learning machine learning using python. have written following class gives error:
typeerror: can't multiply sequence non-int of type 'float'
class perceptron(object): def __init__(self, eta=0.01, n_iter=10): self.eta = eta # learning rate self.n_iter = n_iter # number of iteration on training dataset def fit(self, x, y): self.w_ = np.zeros(1 + x.shape[1]) # initialize weights 0 # x = {array-like} : shape[no_of_samples, no_of_features] self.errors_ = [] # no errors in beginning of computation _ in range(self.n_iter): errors = 0 xi, target in zip(x, y): update = self.eta * (target - self.predict(xi)) self.w_[1:] += update * xi self.w_[0] += update errors += int(update != 0.0) self.errors_.append(errors) return self def net_input(self, x): return np.dot(x, self.w_[1:]) + self.w_[0] def predict(self, x): return np.where(self.net_input(x) >= 0.0, 1, -1)
i getting error in net_input() method @ np.dot(). using following dataset : https://raw.githubusercontent.com/uiuc-cse/data-fa14/gh-pages/data/iris.csv
following changes help.
def fit(self, x, y): ... xi, target in zip(x, y): update = self.eta * (target - self.predict(xi.reshape(1, x.shape[1])) ... # here if want implement perceptron, use matmul not dot product def net_input(self, x): return np.matmul(x, self.w_[1:]) + self.w_[0]
Comments
Post a Comment