0

I need to solve a large system of linear equations using a least squares method. So far I have found the answers whilst unconstrained but would like to limit my answers to be non-negative. The code I have been using is shown below. 'Sheet 1' holds a matrix of size 30x135 and 'sheet 2' a matrix of size 30x1.

 import pandas as pd
 import numpy as np
 df = pd.read_excel("C:\\example\price_analysis.xlsx", sheet_name = "Sheet1")
 print(df)
 dg = pd.read_excel("C:\\example\price_analysis.xlsx", sheet_name = "Sheet2")
 print(dg)
 z = np.linalg.lstsq(df,dg, rcond=None)
 print(z)

I tried using the answer from this post but couldn't work out how to find the least squares result where all values are positive, rather than simply changing all negative values to be 0.

  • That's a linear programming problem, and NumPy alone may not be able to solve this. `scipy`, on the other hand, should have methods to do it. – ForceBru Jan 04 '19 at 11:49
  • This is not an LP as the objective is nonlinear. The natural scipy-func would be [nnls](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.nnls.html). But you could also use some of the `scipy.optimize.minimize` solvers as outlined in your link, but in this case: pass gradients (and don't use numerical differentiation as done in the link). The former will be faster and more simple for small / dense instances. For large / sparse instances, L-BFGS-B within `minimize` will be much much faster. – sascha Jan 04 '19 at 11:56
  • Check out this thread as well: https://datascience.stackexchange.com/questions/18258/how-to-force-weights-to-be-non-negative-in-linear-regression – psarka Jan 04 '19 at 12:23
  • Thanks all, the nnls method seems to be a good way to solve my problem. I've got it to work for an example data set but can't get it to work when I'm pulling my data in from excel: Thanks you again – Ben Bishop Jan 04 '19 at 14:03

0 Answers0