コード例 #1
0
 def predict(self, X: spa.csr_matrix):
     ''' Predict class of X'''
     if X.ndim == 1:
         X = X.reshape(1, -1)
     N, D = X.shape
     class_probs = LR.logisticClassProbability(X, self.b, self.W)
     y = class_probs.argmax(axis=1)
     return self.encoder.inverse_transform(y)
コード例 #2
0
 def fit(self,
         X: spa.csr_matrix,
         Y: spa.csr_matrix,
         X_val=None,
         Y_val=None):
     ''' Fit model to data. X_val and Y_val are only used to report accuracy
     during optimization they do not affect the fitted W,b parameters'''
     if X.ndim == 1:
         X = X.reshape(1, -1)
     N, D = X.shape
     self.encoder = LabelEncoder()
     y = self.encoder.fit_transform(Y)
     K = len(self.encoder.classes_)
     Z = np.zeros((N, K), dtype=int)
     Z[np.arange(N), y] = 1
     if not (X_val is None):
         N_val = len(X_val)
         y_val = self.encoder.transform(Y_val)
         Z_val = np.zeros((N_val, K), dtype=int)
         Z_val[np.arange(N_val), y_val] = 1
     else:
         Z_val = None
     b_guess = np.zeros(K)
     W_guess = np.random.normal(0, 1, (K, D)) / np.sqrt(D)
     self.b, self.W = LR.optimize_logistic_weights(
         X,
         Z,
         b_guess,
         W_guess,
         X_val=X_val,
         Z_val=Z_val,
         penalty=self.penalty,
         learning_rate=self.learning_rate,
         batch_size=self.batch_size,
         tol=self.tol,
         max_iter=self.max_iter,
         verbose=self.verbose)