|
11 | 11 |
|
12 | 12 | The l1 Solvers
|
13 | 13 | --------------
|
14 |
| -The solvers are slower than standard Newton, and sometimes have |
15 |
| - convergence issues Nonetheless, the final solution makes sense and |
16 |
| - is often better than the ML solution. |
17 | 14 | The standard l1 solver is fmin_slsqp and is included with scipy. It
|
18 | 15 | sometimes has trouble verifying convergence when the data size is
|
19 | 16 | large.
|
|
36 | 33 | logit_mod = sm.Logit(spector_data.endog, spector_data.exog)
|
37 | 34 | ## Standard logistic regression
|
38 | 35 | logit_res = logit_mod.fit()
|
| 36 | + |
39 | 37 | ## Regularized regression
|
| 38 | + |
40 | 39 | # Set the reularization parameter to something reasonable
|
41 | 40 | alpha = 0.05 * N * np.ones(K)
|
| 41 | + |
42 | 42 | # Use l1, which solves via a built-in (scipy.optimize) solver
|
43 | 43 | logit_l1_res = logit_mod.fit_regularized(method='l1', alpha=alpha, acc=1e-6)
|
| 44 | + |
44 | 45 | # Use l1_cvxopt_cp, which solves with a CVXOPT solver
|
45 | 46 | logit_l1_cvxopt_res = logit_mod.fit_regularized(
|
46 | 47 | method='l1_cvxopt_cp', alpha=alpha)
|
| 48 | + |
47 | 49 | ## Print results
|
48 | 50 | print "============ Results for Logit ================="
|
49 | 51 | print "ML results"
|
|
58 | 60 | anes_exog = sm.add_constant(anes_exog, prepend=False)
|
59 | 61 | mlogit_mod = sm.MNLogit(anes_data.endog, anes_exog)
|
60 | 62 | mlogit_res = mlogit_mod.fit()
|
| 63 | + |
61 | 64 | ## Set the regularization parameter.
|
62 | 65 | alpha = 10 * np.ones((mlogit_mod.J - 1, mlogit_mod.K))
|
| 66 | + |
63 | 67 | # Don't regularize the constant
|
64 | 68 | alpha[-1,:] = 0
|
65 | 69 | mlogit_l1_res = mlogit_mod.fit_regularized(method='l1', alpha=alpha)
|
66 | 70 | print mlogit_l1_res.params
|
| 71 | + |
67 | 72 | #mlogit_l1_res = mlogit_mod.fit_regularized(
|
68 | 73 | # method='l1_cvxopt_cp', alpha=alpha, abstol=1e-10, trim_tol=1e-6)
|
69 | 74 | #print mlogit_l1_res.params
|
| 75 | + |
70 | 76 | ## Print results
|
71 | 77 | print "============ Results for MNLogit ================="
|
72 | 78 | print "ML results"
|
73 | 79 | print mlogit_res.summary()
|
74 | 80 | print "l1 results"
|
75 | 81 | print mlogit_l1_res.summary()
|
76 | 82 | #
|
| 83 | +# |
77 | 84 | #### Logit example with many params, sweeping alpha
|
78 | 85 | spector_data = sm.datasets.spector.load()
|
79 | 86 | X = spector_data.exog
|
80 | 87 | Y = spector_data.endog
|
| 88 | + |
81 | 89 | ## Fit
|
82 | 90 | N = 50 # number of points to solve at
|
83 | 91 | K = X.shape[1]
|
84 | 92 | logit_mod = sm.Logit(Y, X)
|
85 | 93 | coeff = np.zeros((N, K)) # Holds the coefficients
|
86 | 94 | alphas = 1 / np.logspace(-0.5, 2, N)
|
| 95 | + |
87 | 96 | ## Sweep alpha and store the coefficients
|
88 | 97 | # QC check doesn't always pass with the default options.
|
89 | 98 | # Use the options QC_verbose=True and disp=True
|
|
94 | 103 | method='l1', alpha=alpha, trim_mode='off', QC_tol=0.1, disp=False,
|
95 | 104 | QC_verbose=True, acc=1e-15)
|
96 | 105 | coeff[n,:] = logit_res.params
|
| 106 | + |
97 | 107 | ## Plot
|
98 | 108 | plt.figure(1);plt.clf();plt.grid()
|
99 | 109 | plt.title('Regularization Path');
|
|
0 commit comments