def _ompcv(*, train, test, x_predict=None, metrics, copy=True, fit_intercept=True, normalize=True, max_iter=None, cv=None, n_jobs=None, verbose=False): """For more info visit : https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.OrthogonalMatchingPursuitCV.html#sklearn.linear_model.OrthogonalMatchingPursuitCV """ model = OrthogonalMatchingPursuitCV(fit_intercept=fit_intercept, copy=copy, normalize=normalize, max_iter=max_iter, cv=cv, n_jobs=n_jobs, verbose=verbose) model.fit(train[0], train[1]) model_name = 'OrthogonalMatchingPursuitCV' y_hat = model.predict(test[0]) if metrics == 'mse': accuracy = _mse(test[1], y_hat) if metrics == 'rmse': accuracy = _rmse(test[1], y_hat) if metrics == 'mae': accuracy = _mae(test[1], y_hat) if x_predict is None: return (model_name, accuracy, None) y_predict = model.predict(x_predict) return (model_name, accuracy, y_predict)
class _OrthogonalMatchingPursuitCVImpl: def __init__(self, **hyperparams): self._hyperparams = hyperparams self._wrapped_model = Op(**self._hyperparams) def fit(self, X, y=None): if y is not None: self._wrapped_model.fit(X, y) else: self._wrapped_model.fit(X) return self def predict(self, X): return self._wrapped_model.predict(X)
def predict(self): """ trains the scikit-learn python machine learning algorithm library function https://scikit-learn.org then passes the trained algorithm the features set and returns the predicted y test values form, the function then compares the y_test values from scikit-learn predicted to y_test values passed in then returns the accuracy """ n_nonzero_coefs = 17 algorithm = OrthogonalMatchingPursuitCV() algorithm.fit(self.X_train, self.y_train) y_pred = list(algorithm.predict(self.X_test)) self.acc = OneHotPredictor.get_accuracy(y_pred, self.y_test) return self.acc
tss, rss, ess, r2 = xss(Y, omp.predict(X)) print "TSS(Total Sum of Squares): ", tss print "RSS(Residual Sum of Squares): ", rss print "ESS(Explained Sum of Squares): ", ess print "R^2: ", r2 print "\n**********测试OrthogonalMatchingPursuitCV类**********" ompCV = OrthogonalMatchingPursuitCV(cv=5) # 拟合训练集 ompCV.fit(train_X, train_Y.values.ravel()) # 打印最好的n_nonzero_coefs值 print "最好的n_nonzero_coefs值: ", ompCV.n_nonzero_coefs_ # 打印模型的系数 print "系数:", ompCV.coef_ print "截距:", ompCV.intercept_ print '训练集R2: ', r2_score(train_Y, ompCV.predict(train_X)) # 对于线性回归模型, 一般使用均方误差(Mean Squared Error,MSE)或者 # 均方根误差(Root Mean Squared Error,RMSE)在测试集上的表现来评该价模型的好坏. test_Y_pred = ompCV.predict(test_X) print "测试集得分:", ompCV.score(test_X, test_Y) print "测试集MSE:", mean_squared_error(test_Y, test_Y_pred) print "测试集RMSE:", np.sqrt(mean_squared_error(test_Y, test_Y_pred)) print "测试集R2:", r2_score(test_Y, test_Y_pred) tss, rss, ess, r2 = xss(Y, ompCV.predict(X)) print "TSS(Total Sum of Squares): ", tss print "RSS(Residual Sum of Squares): ", rss print "ESS(Explained Sum of Squares): ", ess print "R^2: ", r2