sklearn-docs/Lasso-dense-sparse-data
3
1import gradio as gr2from time import time3from scipy import sparse4from scipy import linalg5 6from sklearn.datasets import make_regression7from sklearn.linear_model import Lasso8 9 10def load_dataset():11 X, y = make_regression(n_samples=200, n_features=5000, random_state=0)12 # create a copy of X in sparse format13 X_sp = sparse.coo_matrix(X)14 return X,X_sp,y15 16def compare_lasso_dense():17 alpha_dense = 118 alpha_sparse = 0.119 sparse_lasso = Lasso(alpha= alpha_sparse, fit_intercept=False, max_iter=1000)20 dense_lasso = Lasso(alpha=alpha_dense, fit_intercept=False, max_iter=1000)21 22 t0 = time()23 sparse_lasso.fit(X_sp, y)24 # print(f"Sparse Lasso done in {(time() - t0):.3f}s")25 elapse1 = time() - t026 27 t1 = time()28 dense_lasso.fit(X, y)29 # print(f"Dense Lasso done in {(time() - t0):.3f}s")30 elapse2 = time() - t131 32 # compare the regression coefficients33 coeff_diff = linalg.norm(sparse_lasso.coef_ - dense_lasso.coef_)34 # print(f"Distance between coefficients : {coeff_diff:.2e}")35 return f"Sparse Lasso done in {(elapse1):.3f}s\t\n" + f"Dense Lasso done in {(elapse2):.3f}s\t\n" + f"Distance between coefficients : {coeff_diff:.2e}\t\n"36 37def compare_lasso_sparse():38 # make a copy of the previous data39 Xs = X.copy()40 # make Xs sparse by replacing the values lower than 2.5 with 0s41 Xs[Xs < 2.5] = 0.042 # create a copy of Xs in sparse format43 Xs_sp = sparse.coo_matrix(Xs)44 Xs_sp = Xs_sp.tocsc()45 46 # compute the proportion of non-zero coefficient in the data matrix47 print(f"Matrix density : {(Xs_sp.nnz / float(X.size) * 100):.3f}%")48 matrix_density = Xs_sp.nnz / float(X.size) * 100 49 50 alpha_dense = 151 alpha_sparse = 0.152 sparse_lasso = Lasso(alpha= alpha_sparse, fit_intercept=False, max_iter=1000)53 dense_lasso = Lasso(alpha=alpha_dense, fit_intercept=False, max_iter=1000)54 55 t0 = time()56 sparse_lasso.fit(Xs_sp, y)57 print(f"Sparse Lasso done in {(time() - t0):.3f}s")58 elapses1 = time() - t059 60 t1 = time()61 dense_lasso.fit(Xs, y)62 print(f"Dense Lasso done in {(time() - t1):.3f}s")63 elapses2 = time() - t164 65 # compare the regression coefficients66 coeff_diff = linalg.norm(sparse_lasso.coef_ - dense_lasso.coef_)67 print(f"Distance between coefficients : {coeff_diff:.2e}")68 return f"Matrix density : {(Xs_sp.nnz / float(X.size) * 100):.3f}%\t\n"+ f"Sparse Lasso done in {(elapses1):.3f}s\t\n" + f"Dense Lasso done in {(elapses2):.3f}s\t\n" + f"Distance between coefficients : {coeff_diff:.2e}\t\n" 69 70 71X,X_sp,y = load_dataset()72# compare_lasso_dense(X,X_sp,y)73# compare_lasso_sparse(X,X_sp,y)74 75 76 77title = " Lasso on Dense and Sparse data "78info = '''**Comparing the two Lasso implementations on Dense data**79We create a linear regression problem that is suitable for the Lasso, that is to say, with more features than samples. 80We then store the data matrix in both dense (the usual) and sparse format, and train a Lasso on each. We compute the 81runtime of both and check that they learned the same model by 82computing the Euclidean norm of the difference between the coefficients they learned. 83Because the data is dense, we expect better runtime with a dense data format.84'''85 86info2='''***Comparing the two Lasso implementations on Sparse data***87We make the previous problem sparse by replacing all small values with 0 88and run the same comparisons as above. Because the data is now sparse, 89we expect the implementation that uses the sparse data format to be faster.90'''91 92conclusion = '''**Conclusion**93We show that linear_model.Lasso provides the same results for dense and sparse data and that in the case of sparse data the speed is improved**.94'''95with gr.Blocks() as demo:96 gr.Markdown(f"# {title}")97 gr.Markdown(info)98 99 txt_3 = gr.Textbox(value="", label="Dense Lasso comparison")100 btn = gr.Button(value="Dense Lasso comparison")101 btn.click(compare_lasso_dense, outputs=[txt_3])102 103 gr.Markdown(info2)104 105 txt_4 = gr.Textbox(value="", label="Sparse Lasso comparison")106 btn = gr.Button(value="Sparse Lasso comparison")107 btn.click(compare_lasso_sparse, outputs=[txt_4])108 109 gr.Markdown(conclusion)110 111 112if __name__ == "__main__":113 demo.launch()114 