Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
dynamicslab
GitHub Repository: dynamicslab/databook_python
Path: blob/master/CH04/CH04_SEC04_1_CompareRegression.ipynb
597 views
Kernel: Python 3
import numpy as np import matplotlib.pyplot as plt from matplotlib import rcParams from sklearn import linear_model rcParams.update({'font.size': 18})
n = 100 L = 4 x = np.linspace(0,L,n) f = np.power(x,2) # Parabola with 100 data points M = 20 # Polynomial degree phi = np.zeros((n,M)) for j in range(M): phi[:,j] = np.power(x,j) # Build matrix A plt.figure() plt.plot(x,f,color='k') fig,axs = plt.subplots(2,2) axs = axs.reshape(-1) for j in range(4): fn = np.power(x,2) + 0.1*np.random.randn(*x.shape) an = np.linalg.pinv(phi) @ fn # Least-square fit fna = phi @ an En = np.linalg.norm(f-fna,ord=2)/np.linalg.norm(f,ord=2) axs[j].bar(range(len(an)),an) plt.show()
Image in a Jupyter notebookImage in a Jupyter notebook
## Different regressions plt.figure() plt.plot(x,f,color='k') lam = 0.1 phi2 = phi[:,1:] E1 = np.zeros(100) E2 = np.zeros(100) E3 = np.zeros(100) E4 = np.zeros(100) E5 = np.zeros(100) E6 = np.zeros(100) A1 = np.zeros((M,100)) A2 = np.zeros((M,100)) A3 = np.zeros((M,100)) A4 = np.zeros((M,100)) A5 = np.zeros((M,100)) A6 = np.zeros((M,100)) for jj in range(100): # for jj in range(10): f = np.power(x,2)+0.2*np.random.randn(n) a1 = np.linalg.pinv(phi) @ f f1 = phi @ a1 E1[jj] = np.linalg.norm(f-f1,ord=2)/np.linalg.norm(f,ord=2) a2 = np.linalg.lstsq(phi,f,rcond=None)[0] f2 = phi @ a2 E2[jj] = np.linalg.norm(f-f2,ord=2)/np.linalg.norm(f,ord=2) regr3 = linear_model.ElasticNet(alpha=1.0, copy_X=True, l1_ratio=lam, max_iter=10**5,random_state=0) regr3.fit(phi, f) a3 = regr3.coef_ f3 = phi @ a3 E3[jj] = np.linalg.norm(f-f3,ord=2)/np.linalg.norm(f,ord=2) regr4 = linear_model.ElasticNet(alpha=0.8, copy_X=True, l1_ratio=lam, max_iter=10**5,random_state=0) regr4.fit(phi, f) a4 = regr4.coef_ f4 = phi @ a4 E4[jj] = np.linalg.norm(f-f4,ord=2)/np.linalg.norm(f,ord=2) huber = linear_model.HuberRegressor().fit(phi, f) # matlab's robustfit() does not have an exact sklearn analogue a5 = huber.coef_ f5 = phi @ a5 E5[jj] = np.linalg.norm(f-f5,ord=2)/np.linalg.norm(f,ord=2) ridge = linear_model.Ridge(alpha=1.0).fit(phi,f) a6 = ridge.coef_ f6 = phi @ a6 E6[jj] = np.linalg.norm(f-f6,ord=2)/np.linalg.norm(f,ord=2) A1[:,jj] = a1 A2[:,jj] = a2 A3[:,jj] = a3 A4[:,jj] = a4 A5[:,jj] = a5 A6[:,jj] = a6 plt.plot(x,f) Err = np.column_stack((E1,E2,E3,E4,E5,E6)) Err2 = np.column_stack((E1,E2,E3,E4,E5))
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.96313364616438, tolerance: 0.23262143211570602 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.980496909240491, tolerance: 0.23009443396543713 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.834149648064171, tolerance: 0.23159754285130368 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.736628142726698, tolerance: 0.23387411099959637 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.39790009634818, tolerance: 0.23255369746526378 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.395653753387025, tolerance: 0.23159019548178722 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.166154772989183, tolerance: 0.23301442617758059 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.65621759089967, tolerance: 0.23448716843418635 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.110071804763539, tolerance: 0.23336026457132766 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 11.120440744271942, tolerance: 0.23309521320713023 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.339007827311736, tolerance: 0.23417443828074017 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.462602157968517, tolerance: 0.23404950846647243 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.576078152319708, tolerance: 0.2310098923652076 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.101770488766984, tolerance: 0.23088345006432393 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.292858115875047, tolerance: 0.2342122413558959 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.518545276322222, tolerance: 0.2327164984683579 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.481442823171584, tolerance: 0.23168418757340514 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 11.108061524782494, tolerance: 0.23334965980023964 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.36703812378985, tolerance: 0.23513322989408936 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.927545258493016, tolerance: 0.23233470858687116 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.338989317020477, tolerance: 0.23099109546081883 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.745997946294779, tolerance: 0.23306484913525866 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.693846474113858, tolerance: 0.23584984042630486 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.029047970127138, tolerance: 0.23475830403634632 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.530399941739756, tolerance: 0.23253795654933065 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.630449994040076, tolerance: 0.2343166882501852 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.687452841552897, tolerance: 0.23562913844391825 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.29449036940857, tolerance: 0.23271418434480617 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.177019262790829, tolerance: 0.23177888676317546 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.381013296879575, tolerance: 0.2349845350158184 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.854369954133638, tolerance: 0.23316913814669107 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.563012266253029, tolerance: 0.23583523131668782 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.761618396081548, tolerance: 0.2308814856199967 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.345718833239838, tolerance: 0.23185210928517164 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.233291202472271, tolerance: 0.23116907266402434 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.51554329696776, tolerance: 0.23536383389210788 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.634434248453559, tolerance: 0.2297638224518892 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.616738442202, tolerance: 0.23303849380790234 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 11.023430280831263, tolerance: 0.23311606036526902 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.75696287773189, tolerance: 0.23386175875562085 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.65106393323172, tolerance: 0.2332251620258169 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.99592587084579, tolerance: 0.23375130373600336 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.793615436299199, tolerance: 0.23275978681469248 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.65014176753386, tolerance: 0.23637887308067312 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.031741244011068, tolerance: 0.23570553706218625 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.506133594885469, tolerance: 0.23445136972942124 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.946972545990764, tolerance: 0.23405850182576834 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.74271695361919, tolerance: 0.23314507675622756 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.870631820693257, tolerance: 0.232785106247468 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.120630825070405, tolerance: 0.23565084496813327 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.517077434364763, tolerance: 0.23491151102835733 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.45271426930172, tolerance: 0.23343573579710256 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.647166495154964, tolerance: 0.2328607053900123 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.688268906121591, tolerance: 0.23099541388747818 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.159106288015579, tolerance: 0.23014685238363236 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.624207934176216, tolerance: 0.2360765393204739 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.094540973416663, tolerance: 0.23398750550274708 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.635871966104654, tolerance: 0.2342609772697318 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 11.350269072569237, tolerance: 0.23234474011727918 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.231872084266943, tolerance: 0.23142356416579724 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.334469436527279, tolerance: 0.23494604051281026 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.658802557325586, tolerance: 0.23286399038884348 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.848175641718974, tolerance: 0.2303320965111695 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.747253483442849, tolerance: 0.23192194419778916 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.94594311197503, tolerance: 0.23379791274243827 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.365719249828949, tolerance: 0.2329030494636531 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.922592131449534, tolerance: 0.23169455076710957 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.38597103707277, tolerance: 0.23406193281180565 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.806016423318525, tolerance: 0.23127604562124598 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.28754781083886, tolerance: 0.23182593113809905 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.002488490500944, tolerance: 0.23129735510924768 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.804025429492564, tolerance: 0.2347516641386673 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.746815368036373, tolerance: 0.23109837027947053 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.783097278409773, tolerance: 0.22967289808849192 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.863890373507061, tolerance: 0.23212351780600768 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 8.835518115196432, tolerance: 0.23167416413637162 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 11.291410886994049, tolerance: 0.2302965517089786 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.033826818609349, tolerance: 0.236142777988498 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.420315341976716, tolerance: 0.23050739291136507 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.208073630023563, tolerance: 0.23468844580486467 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.234145677724662, tolerance: 0.2322157041608075 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.729464155375156, tolerance: 0.23026163203806752 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.990280089487385, tolerance: 0.233561376047733 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.163300421874325, tolerance: 0.23303879870700123 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.046985958973469, tolerance: 0.23472385078327043 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.463047729059378, tolerance: 0.2342473936038503 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.63445386996984, tolerance: 0.23363012961189067 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.081494297780164, tolerance: 0.23426769290217486 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.608094150948627, tolerance: 0.23065559615761586 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 8.793244984153372, tolerance: 0.23108010589455513 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.207267721833992, tolerance: 0.23442396248026845 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.155821028731497, tolerance: 0.2328034897257374 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.18866161243616, tolerance: 0.23644802069438747 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.446372135018155, tolerance: 0.2299930878420481 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.0539397163259, tolerance: 0.23509858656967408 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.986888284121775, tolerance: 0.2327807847166748 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.179148725808037, tolerance: 0.23481740677888976 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.609089964284479, tolerance: 0.2335215068361321 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 10.039793635312677, tolerance: 0.23324187394205448 positive) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\coordinate_descent.py:475: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Duality gap: 9.537660696068201, tolerance: 0.2306513197868324 positive)
Image in a Jupyter notebook
plt.rcParams['figure.figsize'] = [12, 18] rcParams.update({'font.size': 14}) fig,axs = plt.subplots(3,2) axs = axs.reshape(-1) axs[0].boxplot(A1.T) axs[0].set_title('pinv') axs[1].boxplot(A2.T) axs[1].set_title('lstsq') axs[2].boxplot(A3.T) axs[2].set_title('elastic (alpha=1)') axs[3].boxplot(A4.T) axs[3].set_title('elastic (alpha=0.8)') axs[4].boxplot(A5.T) axs[4].set_title('huber') axs[5].boxplot(A6.T) axs[5].set_title('ridge') for ax in axs: ax.set_xlim(0,M) plt.rcParams['figure.figsize'] = [8, 8] plt.figure() plt.boxplot(Err) plt.show()
Image in a Jupyter notebookImage in a Jupyter notebook
M = 10 En = np.zeros((100,M)) phi = np.zeros((len(x),M)) for jj in range(M): for j in range(jj): phi[:,j] = np.power(x,j) f = np.power(x,2) for j in range(100): fn = np.power(x,2) + 0.1*np.random.randn(n) an = np.linalg.pinv(phi) @ fn fna = phi @ an En[j,jj] = np.linalg.norm(f-fna,ord=2)/np.linalg.norm(f,ord=2) plt.boxplot(En) plt.show()
Image in a Jupyter notebook