Submit
Path:
~
/
/
opt
/
alt
/
python35
/
share
/
doc
/
alt-python35-scikit-learn-0.18.1
/
examples
/
linear_model
/
File Content:
plot_ard.py
""" ================================================== Automatic Relevance Determination Regression (ARD) ================================================== Fit regression model with Bayesian Ridge Regression. See :ref:`bayesian_ridge_regression` for more information on the regressor. Compared to the OLS (ordinary least squares) estimator, the coefficient weights are slightly shifted toward zeros, which stabilises them. The histogram of the estimated weights is very peaked, as a sparsity-inducing prior is implied on the weights. The estimation of the model is done by iteratively maximizing the marginal log-likelihood of the observations. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from scipy import stats from sklearn.linear_model import ARDRegression, LinearRegression ############################################################################### # Generating simulated data with Gaussian weights # Parameters of the example np.random.seed(0) n_samples, n_features = 100, 100 # Create Gaussian data X = np.random.randn(n_samples, n_features) # Create weights with a precision lambda_ of 4. lambda_ = 4. w = np.zeros(n_features) # Only keep 10 weights of interest relevant_features = np.random.randint(0, n_features, 10) for i in relevant_features: w[i] = stats.norm.rvs(loc=0, scale=1. / np.sqrt(lambda_)) # Create noise with a precision alpha of 50. alpha_ = 50. noise = stats.norm.rvs(loc=0, scale=1. / np.sqrt(alpha_), size=n_samples) # Create the target y = np.dot(X, w) + noise ############################################################################### # Fit the ARD Regression clf = ARDRegression(compute_score=True) clf.fit(X, y) ols = LinearRegression() ols.fit(X, y) ############################################################################### # Plot the true weights, the estimated weights and the histogram of the # weights plt.figure(figsize=(6, 5)) plt.title("Weights of the model") plt.plot(clf.coef_, color='darkblue', linestyle='-', linewidth=2, label="ARD estimate") plt.plot(ols.coef_, color='yellowgreen', linestyle=':', linewidth=2, label="OLS estimate") plt.plot(w, color='orange', linestyle='-', linewidth=2, label="Ground truth") plt.xlabel("Features") plt.ylabel("Values of the weights") plt.legend(loc=1) plt.figure(figsize=(6, 5)) plt.title("Histogram of the weights") plt.hist(clf.coef_, bins=n_features, color='navy', log=True) plt.scatter(clf.coef_[relevant_features], 5 * np.ones(len(relevant_features)), color='gold', marker='o', label="Relevant features") plt.ylabel("Features") plt.xlabel("Values of the weights") plt.legend(loc=1) plt.figure(figsize=(6, 5)) plt.title("Marginal log-likelihood") plt.plot(clf.scores_, color='navy', linewidth=2) plt.ylabel("Score") plt.xlabel("Iterations") plt.show()
Submit
FILE
FOLDER
Name
Size
Permission
Action
README.txt
135 bytes
0644
lasso_dense_vs_sparse_data.py
1862 bytes
0644
plot_ard.py
2828 bytes
0644
plot_bayesian_ridge.py
2733 bytes
0644
plot_huber_vs_ridge.py
2206 bytes
0644
plot_iris_logistic.py
1679 bytes
0644
plot_lasso_and_elasticnet.py
2074 bytes
0644
plot_lasso_coordinate_descent_path.py
2945 bytes
0644
plot_lasso_lars.py
1080 bytes
0644
plot_lasso_model_selection.py
5431 bytes
0644
plot_logistic.py
1568 bytes
0644
plot_logistic_l1_l2_sparsity.py
2601 bytes
0644
plot_logistic_multinomial.py
2480 bytes
0644
plot_logistic_path.py
1195 bytes
0644
plot_multi_task_lasso_support.py
2319 bytes
0644
plot_ols.py
1936 bytes
0644
plot_ols_3d.py
2040 bytes
0644
plot_ols_ridge_variance.py
2060 bytes
0644
plot_omp.py
2263 bytes
0644
plot_polynomial_interpolation.py
2088 bytes
0644
plot_ransac.py
1859 bytes
0644
plot_ridge_coeffs.py
2785 bytes
0644
plot_ridge_path.py
2138 bytes
0644
plot_robust_fit.py
3050 bytes
0644
plot_sgd_comparison.py
1819 bytes
0644
plot_sgd_iris.py
2202 bytes
0644
plot_sgd_loss_functions.py
1232 bytes
0644
plot_sgd_penalties.py
1877 bytes
0644
plot_sgd_separating_hyperplane.py
1221 bytes
0644
plot_sgd_weighted_samples.py
1458 bytes
0644
plot_sparse_recovery.py
7486 bytes
0644
plot_theilsen.py
3846 bytes
0644
N4ST4R_ID | Naxtarrr