# Quiz 1
# Machine Learning 2018-1

A logistic regression model is a statistical classification method that uses a generalized linear regression model to estimate $P(C=1 | \mathbf{x})$, the probability of the sample $\mathbf{x}\in\mathbb{R}^2$ belonging to class $C_1$. 

\begin{equation}
y=P(C=1|\mathbf{x},\mathbf{w})=\sigma(w_0+w_1x_0 + w_2x_1)
\end{equation}
where 
\begin{equation}
\sigma(x)=\frac{1}{1+e^{-x}}
\end{equation}

### 1. 
Write a function that implements a logistic regression model.


```python
import numpy as np

def ro(x):
    return  1 / (1 + np.exp(-x))

def f_1(w, x):
    ''' 
     w: weight vector with shape (3,)
     x: input sample with shape (1,2)
     returns: P(C=1|x,w)
    '''
    ### Your code here
    
    y = ro(w[0] + w[1]*x[0] + w[2]*x[1])
    
    
    return  y

print(f_1(np.array([0, 1, 2]), np.array([1, 2])))

lst = []
for x1 in np.linspace(-2, 1.5, 4):
    for x2 in np.linspace(-2, 1.5, 4):
        lst.append([x1, x2])
X = np.array(lst)

print(X[0, :])
```

    0.9933071490757153
    [-2. -2.]



```python

```

### 2. 
Assume that the cost of a false positive (predicting class $C_1$ when the real class is $C_0$) is $L_0$ and the cost of a false negative is $L_1$. Write a function that calculates the risk of classifying a sample $\mathbf{x}$ in class $y \in \{0,1\}$.



```python
def f_2(w, L, x, y):
    ''' 
     w: weight vector with shape (3,)
     L: loss vector with shape (2,)
     x: input sample with shape (2,1)
     y: class value {0, 1}
     returns: R(y|x,w)
    '''
    ### Your code here
    
    p = f_1(w, x)
    
    if y == 0:
        R =  L[1]*p
    else:
        R =  L[0]*(1-p)
    
    return  R
W1 = np.array([0, 1, 2])
L1 = np.array([0.9, 2, 0.3])
X = np.array([-2, -2])
y = 0

f_2(W1, L1, X, y)
```




    0.004945246313269549



### 3. 
Write a function that implements a classifier that makes the prediction that minimizes the risk.


```python
def f_3(w, L, x):
    ''' 
     w: weight vector with shape (3,)
     L: loss vector with shape (2,)
     x: input sample with shape (2,1)
     returns: predicted class {0, 1} 
    '''
    ### Your code here
    
    r0 = f_2(w, L, x, 0)
    
    r1 = f_2(w, L, x, 1)
    
    # Choose the minimum risk
    
    if r0 <= r1 :
        return 0
    else:
        return 1
    
    return  0
```

### 4. 
Write a function that implements a classifier that makes the prediction that minimizes the risk, but that can also reject the sample. The cost of rejection is $L_2$.


```python
def f_4(w, L, x):
    ''' 
     w: weight vector with shape (3,)
     L: loss vector with shape (3,)
     x: input sample with shape (2,1)
     returns: predicted class {0, 1, 2}. Rejection is 2.
    '''
    ### Your code here
    
    rejectRisk = L[2]
    
    risk0 = f_2(w, L, x, 0)
    
    risk1 = f_2(w, L, x, 1)
    
    r = [risk0, risk1, rejectRisk]
    
    # Choose the minimum risk
    
    if np.argmin(r) == 0 : # risk of 0
        return 0
    elif np.argmin(r) == 1: # risk of 1
        return 1
    elif np.argmin(r) == 2: # risk of reject
        return 2
    
    return -1

# Test the function
C2= [ 0.,  0.,  2.,  1.,  0.,  0.,  2.,  1.,  0.,  2.,  1.,  1.,  0.,  2.,  1.,  1.]

for i in range(len(lst)):
    print(C2[i], "=", f_4(W1, L1, X[i, :]))
```

    0.0 = 0
    0.0 = 0
    2.0 = 2
    1.0 = 1
    0.0 = 0
    0.0 = 0
    2.0 = 2
    1.0 = 1
    0.0 = 0
    2.0 = 2
    1.0 = 1
    1.0 = 1
    0.0 = 0
    2.0 = 2
    1.0 = 1
    1.0 = 1


### Grader

Run the following cell to grade your quiz.


```python
def compare(val1, val2, error):
    if abs(val1 - val2) > error:
        return False
    return True

lst = []
for x1 in np.linspace(-2, 1.5, 4):
    for x2 in np.linspace(-2, 1.5, 4):
        lst.append([x1, x2])
X = np.array(lst)

W1 = np.array([0, 1, 2])
L1 = np.array([0.9, 2, 0.3])
W2 = np.array([-0.3, 1, -0.5])
Y1= [ 0.00247262,  0.02492443, 0.20860853,  0.73105858,  0.00789708,  0.07585818,
  0.45842952,  0.89721598,  0.02492443,  0.20860853,  0.73105858,  0.9655548,
  0.07585818,  0.45842952,  0.89721598,  0.98901306]
R10= [ 0.00494525,  0.04984885,  0.41721705,  1.46211716,  0.01579417,  0.15171636,
  0.91685903,  1.79443195,  0.04984885,  0.41721705,  1.46211716,  1.93110961,
  0.15171636,  0.91685903,  1.79443195,  1.97802611]
R11= [ 0.89777464,  0.87756802,  0.71225233,  0.24204728,  0.89289262,  0.83172764,
  0.48741343,  0.09250562,  0.87756802,  0.71225233,  0.24204728,  0.03100068,
  0.83172764,  0.48741343,  0.09250562,  0.00988825]
C1= [ 0.,  0.,  0.,  1.,  0.,  0.,  1.,  1.,  0.,  0.,  1.,  1.,  0.,  1.,  1.,  1.]
C2= [ 0.,  0.,  2.,  1.,  0.,  0.,  2.,  1.,  0.,  2.,  1.,  1.,  0.,  2.,  1.,  1.]
Y2= [ 0.21416502,  0.13200647,  0.07822826,  0.04521747,  0.46671596,  0.32812743,
  0.21416502,  0.13200647,  0.73756162,  0.61063923,  0.46671596,  0.32812743,
  0.90024951,  0.83433491,  0.73756162,  0.61063923]

def test1():
    for i in range(len(lst)):
        if not compare(Y1[i], f_1(W1, X[i, :]), 0.0001):
            return False
        if not compare(Y2[i], f_1(W2, X[i, :]), 0.0001):
            return False
    return True

def test2():
    for i in range(len(lst)):
        if not compare(R10[i], f_2(W1, L1, X[i, :], 0), 0.0001):
            return False
        if not compare(R11[i], f_2(W1, L1, X[i, :], 1), 0.0001):
            return False
    return True

def test3():
    for i in range(len(lst)):
        if not compare(C1[i], f_3(W1, L1[:2], X[i, :]), 0.0001):
            return False
    return True

def test4():
    for i in range(len(lst)):
        if not compare(C2[i], f_4(W1, L1, X[i, :]), 0.0001):
            return False
    return True

def evaluate():
    score = 0 
    for test in [test1, test2, test3, test4]:
        if test():
            score += 1
    return score

print ("Score: ", evaluate(), "/ 4")
```

    Score:  4 / 4



```python

```

cf488e63024efda50e49f69c13e32fee3048b438
ipynb
Jupyter Notebook
quizzes/quiz1.ipynb
ingJSNA/machineLearning2018
63102dceb06c97d62b0ebcaee19363b72d3fdf13
MIT
quizzes/quiz1.ipynb
ingJSNA/machineLearning2018
63102dceb06c97d62b0ebcaee19363b72d3fdf13
MIT
quizzes/quiz1.ipynb
ingJSNA/machineLearning2018
63102dceb06c97d62b0ebcaee19363b72d3fdf13
MIT
Qwen/Qwen-72B
1. YES
2. YES