def runLarsRegressor(self): lm = Lars(fit_intercept=True, normalize=True) print("Lars Regressor\n") lm.fit(self.m_X_train, self.m_y_train) predictY = lm.predict(self.m_X_test) score = lm.score(self.m_X_test, self.m_y_test) predictTraingY = lm.predict(self.m_X_train) self.displayPredictPlot(predictY) self.displayResidualPlot(predictY, predictTraingY) self.dispalyModelResult(lm, predictY, score)
#LarsCV: fit_intercept, verbose, normalize, cv from sklearn.linear_model import LarsCV, Lars from sklearn.datasets import make_regression import matplotlib.pyplot as plt X, y = make_regression(n_samples=200, noise=4.0, random_state=0) reg = LarsCV(cv=5).fit(X, y) reg.score(X, y) reg.alpha_ pred = reg.predict(X[:, ]) plt.scatter(X[:, 0], y, color='black') plt.scatter(X[:, 0], pred, color='red') plt.show() reg2 = Lars().fit(X, y) reg2.score(X, y) reg2.alpha_ pred = reg2.predict(X[:, ]) #%% LassoLars: alpha, fit_intercept, normalize #LassoLarsCV: alpha, fit_intercept, normalize, cv from sklearn import linear_model reg = linear_model.LassoLars(alpha=0.01) reg.fit([[-1, 1], [0, 0], [1, 1]], [-1, 0, -1]) print(reg.coef_) reg2 = linear_model.LassoLarsCV() reg2.fit([[-1, 1], [0, 0], [1, 1]], [-1, 0, -1])
# LARS Regression import numpy as np from sklearn import datasets from sklearn.linear_model import Lars # load the diabetes datasets dataset = datasets.load_diabetes() # fit a LARS model to the data model = Lars() model.fit(dataset.data, dataset.target) print(model) # make predictions expected = dataset.target predicted = model.predict(dataset.data) # summarize the fit of the model mse = np.mean((predicted-expected)**2) print(mse) print(model.score(dataset.data, dataset.target))
# LARS Regression # The Least Angle Regression (LARS) method is a computationally efficient algorithm for fitting a regression model. # It is useful for highdimensional data and is commonly used in conjunction with regularization (such as LASSO). import numpy as np from sklearn import datasets from sklearn.linear_model import Lars # load the diabetes datasets dataset = datasets.load_diabetes() # fit a LARS model to the data model = Lars() model.fit(dataset.data, dataset.target) print(model) # make predictions expected = dataset.target predicted = model.predict(dataset.data) # summarize the fit of the model mse = np.mean((predicted-expected)**2) print(mse) print(model.score(dataset.data, dataset.target))
# PCA + Elastic Net elasticnet = ElasticNet(l1_ratio=0.5) elasticnet.fit(reduced_training_features, training_labels) preds = elasticnet.predict(reduced_testing_features) score = elasticnet.score(reduced_testing_features,testing_labels) print 'PCA + ElasticNet Results:' print 'R2 score:', score print 'MAE:', mean_absolute_error(testing_labels,preds) # Least-Angle Regression (LARS) from sklearn.linear_model import Lars lars = Lars() lars.fit(training_features, training_labels) preds = lars.predict(testing_features) score = lars.score(testing_features,testing_labels) print 'LARS Results:' print 'R2 score:', score print 'MAE:', mean_absolute_error(testing_labels,preds), '\n' # PCA + LARS lars = Lars() lars.fit(reduced_training_features, training_labels) preds = lars.predict(reduced_testing_features) score = lars.score(reduced_testing_features,testing_labels) print 'PCA + LARS Results:' print 'R2 score:', score print 'MAE:', mean_absolute_error(testing_labels,preds) # Orthogonal Matching Pursuit from sklearn.linear_model import OrthogonalMatchingPursuit
可以快速改造成lasso 缺点: 因为模型是对残差进行迭代设计,所以对噪声敏感 ''' rg = Lars(fit_intercept=True, verbose=False, normalize=True, precompute='auto', n_nonzero_coefs=500, eps=2.2204460492503131e-16, copy_X=True, fit_path=True, positive=False) rg.fit(X_train, Y_train) Y_pre = rg.predict(X_test) rg.score(X_test, Y_test) rg.coef_ rg.intercept_ ''' fit_intercept 是否训练截距 verbose 冗长度 normalize 归一化否 precompute 是否使用Gram矩阵来加速 n_nonzero_coefs 非零系数的目标数 eps 精确度,计算某个值时用到 copy_X 是否覆盖模型中的X fit_path 不太理解,暂时应该也用不到 positive 设置强制系数为正的嘛? '''
predictions = linreg.predict(x_test) # Plot predictions and y_test plt.figure() plt.plot(predictions, label='Predictions') plt.plot(pd.Series(predictions).rolling(5).mean(), label='rolling predictions') plt.plot(y_test.values, label='Shifted Currencies ( y_test values', color='grey') plt.plot(cu.loc[test_index, currency].values, label='UNSHIFTED') plt.legend() plt.show() # Print Score and summary score = linreg.score(x_test, y_test) print('SCORE: {:.2f}'.format(score)) print('Explained Variance: {}'\ .format(explained_variance_score(y_test, predictions))) ############################################################################### # Use two currency predictions to predict instrument ############################################################################### if 0: # Get Parameters if False: cur1 = predictions cur2 = predictions # Choose Instrument
from sklearn.linear_model import Lars from sklearn.model_selection import train_test_split lars = Lars(fit_intercept=False, normalize=False, n_nonzero_coefs=100, verbose=True) x_train, x_test, y_train, y_test = train_test_split(x_scaled, y_scaled, test_size=0.2, random_state=42) lars.fit(x_train, y_train) #print(x_test[0]) lars.predict(x_test)[0] lars.score(x_test, y_test) lars.get_params() beta = generate_random_points(n=10, p=10) scaler.fit(beta) scaler.fit_transform(beta) beta = scaler.fit_transform(beta) epsilons = generate_random_points(n=100, p=10) #print(epsilons) y = [[] for _ in range(10)] for k in range(10): y[k] = np.matmul(beta, np.asarray(x[k])) + epsilons[k] x_scaled = scale(x)