def decision_function(self, X): """Decision function of the linear model Parameters ---------- X : numpy array of shape [n_samples, n_features] Returns ------- C : array, shape = [n_samples] Returns predicted values. """ X = safe_asarray(X) return safe_sparse_dot(X, self.coef_.T) + self.intercept_
def fit(self, X, y_clf, y_regression): """ Fit the multiclass model. Parameters ---------- X : numpy array of shape [n_samples,n_features] Training data y_clf : numpy array of shape [n_samples] Target classes for classification model y_regression: numpy array of shape [n_samples] Target values for regression model Returns ------- self : returns an instance of self. """ X = safe_asarray(X) y_clf = np.asarray(y_clf) y_regression = np.asarray(y_regression) self.clf_model = self.clf.fit(X, y_clf) classes = set(y_clf) regr = self.regr def _generator(): for class_ in classes: examples = y_clf == class_ yield class_, X[examples], y_regression[examples], regr out = Parallel(self.n_jobs, self.verbose, self.pre_dispatch)(\ delayed(_fit_helper)(*params) for params in _generator()) self.regression_models = {} for class_, regr_model in out: self.regression_models[class_] = regr_model return self
def predict(self, X, return_class_prediction=False): """ Predict using the muticlass regression model Parameters ---------- X : numpy array of shape [n_samples, n_features] Returns ------- C : array, shape = [n_samples] Returns predicted values. """ X = safe_asarray(X) y_clf_predicted = np.asarray(self.clf_model.predict(X)) classes = set(y_clf_predicted) def _generator(): for class_ in classes: examples = y_clf_predicted == class_ yield examples, X[examples], self.regression_models[class_] out = Parallel(self.n_jobs, self.verbose, self.pre_dispatch)(\ delayed(_predict_helper)(*params) for params in _generator()) y_regr_predicted = None for examples, predicted in out: if y_regr_predicted is None: y_regr_predicted = np.zeros(X.shape[0], predicted.dtype) y_regr_predicted[examples] = predicted if return_class_prediction: return y_clf_predicted, y_regr_predicted else: return y_regr_predicted
def fit(self, X, y): X = safe_asarray(X) y = np.asarray(y) X = (X.T / y).T return super(RSELinearRegression, self).fit(X, y / y)