# Information Retrieval in High Dimensional Data
### Lab \#10, 25.01.2018

## CVXOPT

a) Go to <a href="http://www.cvxopt.org">cvxopt.org</a> and follow the installation instructions for your distribution.  
For conda, you need to run

conda install -c conda-forge cvxopt

b) Skim through the <b>Examples</b> section on cvxopt.org to get an overview of the functionality of the different solvers of CVXOPT.

c) Implement a function minsq which expects a NumPy array A of shape (m, n) and a NumPy array y of shape (m,) as its arguments and returns a NumPy array x of shape (n,) that solves the following problem

\begin{equation} \min\limits_x \| Ax-y\|. \end{equation}

Test your function by feeding it with appropriate inputs an comparing the results with the one syou get by using np.linalg.pinv. Experiment by adding white Gaussian noise to y.


```python
import numpy as np
from cvxopt import matrix, solvers
```


```python
def minsq(A,y):
    m, n = A.shape
    P = matrix(np.eye(n).astype(dtype=np.float_))
    q = matrix(np.zeros(n).astype(dtype=np.float_))
    G = matrix(np.zeros((m, n)))
    h = matrix(np.zeros((n, 1)))
    A = matrix(A.astype(dtype=np.float_))
    y = np.expand_dims(y, axis = 1)
    b = matrix(y.astype(dtype=np.float_))
    print(y.shape)
    sol = solvers.qp(P,q, G, h, A=A, b=b, solver='MOSEK')
    return sol['x']    
```


```python
A = np.array([[1, 2, 4], [4, 9, 6], [7, 8, 4]], dtype='float64')
y = np.array([3, 5, 9], dtype='float64')
```


```python
A_inv = np.linalg.pinv(A)
```


```python
x = np.dot(A_inv, y)
x
```




    array([ 1.57142857, -0.57142857,  0.64285714])




```python
x = minsq(A, y)
print(x)
```

    (3, 1)
         pcost       dcost       gap    pres   dres
     0:  1.6046e+00  1.6046e+00  3e+00  2e+00  0e+00
     1:  1.6046e+00  1.6046e+00  3e-02  2e-02  2e-16
     2:  1.6046e+00  1.6046e+00  3e-04  2e-04  5e-16
     3:  1.6046e+00  1.6046e+00  3e-06  2e-06  1e-15
     4:  1.6046e+00  1.6046e+00  3e-08  2e-08  1e-15
    Optimal solution found.
    [ 1.57e+00]
    [-5.71e-01]
    [ 6.43e-01]
    



```python

```

9fee0e04c8028148a315990c000b489a02c405a6
ipynb
Jupyter Notebook
information-retrieval-exercises/lab10.ipynb
achmart/inforet
3596ff971207728a42b335e71608b0b96e241228
Apache-2.0
information-retrieval-exercises/lab10.ipynb
achmart/inforet
3596ff971207728a42b335e71608b0b96e241228
Apache-2.0
information-retrieval-exercises/lab10.ipynb
achmart/inforet
3596ff971207728a42b335e71608b0b96e241228
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES