"""Logistic regression model.""" import numpy as np class Logistic: def __init__(self, lr: float, epochs: int): """Initialize a new classifier. Parameters: lr: the learning rate epochs: the number of epochs to train for """ self.w = None # TODO: change this self.lr = lr self.epochs = epochs self.threshold = 0.5 def sigmoid(self, z: np.ndarray) -> np.ndarray: """Sigmoid function. Parameters: z: the input Returns: the sigmoid of the input """ # TODO: implement me sigmoid=1/(1+np.exp(-np.asarray(z))) #print(sigmoid) return sigmoid def train(self, X_train: np.ndarray, y_train: np.ndarray): """Train the classifier. Use the logistic regression update rule as introduced in lecture. Parameters: X_train: a numpy array of shape (N, D) containing training data; N examples with D dimensions y_train: a numpy array of shape (N,) containing training labels """ bias=np.ones((X_train.shape[1])) X_train[0,:]=bias self.w = np.random.randn(X_train.shape[1])*0.001 for i in range(self.epochs): for dat in range(X_train.shape[0]): wt=X_train[dat].dot(self.w) z=self.sigmoid(wt) sigma_wt=self.sigmoid(-y_train[dat]*wt)*y_train[dat]*X_train[dat] #print(sigma_wt) self.w+=self.lr*sigma_wt self.w/=X_train.shape[0] def predict(self, X_test: np.ndarray) -> np.ndarray: """Use the trained weights to predict labels for test data points. Parameters: X_test: a numpy array of shape (N, D) containing testing data; N examples with D dimensions Returns: predicted labels for the data in X_test; a 1-dimensional array of length N, where each element is an integer giving the predicted class. """ # TODO: implement me #y_pred = np.zeros(X_test.shape[0]) y_pred = (np.dot(X_test, self.w)) #print(y_pred) for x in range(0,len(y_pred)): if y_pred[x] <0: y_pred[x]=-1 else: y_pred[x]=1 return y_pred