Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
585 B
2
Indexable
Never
def fit(X, y, lr=0.1, num_iter=iter, fit_intercept=True):
  if fit_intercept:
    X=intercept_add(X)
  if X.shape[0] != y.shape[0]:
    raise ValueError("X and y must have the same number of rows")
  num_classes = len(np.unique(y))
  if X.shape[1] != num_classes:
    raise ValueError("Value array must have the same number of columns as the number of unique elements in y")
  value=np.zeros((X.shape[1], num_classes)) 
  for i in range(num_iter):
    z=np.dot(X,value)
    h=softmax(z)
    gradient=np.dot(X.T,(h-y))/len(y)
    value=value-(lr*gradient)
  return value