# Multiple Features


```python
import pandas as pd
import numpy as np
```


```python
size =[2104,1416,1534,852]
nbr_bedrooms = [5,3,3,2]
nbr_floors = [1,2,2,1]
age = [45,40,30,36]
price = [460,232,315,178]
```


```python
d = {'size':size,'nbr_bedrooms':nbr_bedrooms,'nbr_floors':nbr_floors,'age':age,'price':price}
```


```python
df = pd.DataFrame(d)
```

## 1. Definitions


```python
df
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>size</th>
      <th>nbr_bedrooms</th>
      <th>nbr_floors</th>
      <th>age</th>
      <th>price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>2104</td>
      <td>5</td>
      <td>1</td>
      <td>45</td>
      <td>460</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1416</td>
      <td>3</td>
      <td>2</td>
      <td>40</td>
      <td>232</td>
    </tr>
    <tr>
      <th>2</th>
      <td>1534</td>
      <td>3</td>
      <td>2</td>
      <td>30</td>
      <td>315</td>
    </tr>
    <tr>
      <th>3</th>
      <td>852</td>
      <td>2</td>
      <td>1</td>
      <td>36</td>
      <td>178</td>
    </tr>
  </tbody>
</table>
</div>



$y$ : the element to predict here is the **price**  

$x_n$ : the features used to predict y **(size, nbr of bedrooms, nbr of floors, age )**  

$n$ : number of features. here $n = 4$ 

$x^{(i)} : $ input of $i^{th}$ training example  

* **ex:** $x^{(2)}$, with $x^{(2)}$ being a vector


```python
#(pandas is indexed at 0)
df.loc[1,['size','nbr_bedrooms','nbr_floors','age']]
```




    size            1416
    nbr_bedrooms       3
    nbr_floors         2
    age               40
    Name: 1, dtype: int64



$x^{(i)}_j$ : value of feature $j$ in $i^{th}$ training example  
* **ex:** $x^{(2)}_3$


```python
#(pandas is indexed at 0)
df.iloc[2][2]
```




    2



## 2. Hypothesis

**General rule**

$h_\theta(x) = \theta_0 + \theta_1x_1 + \theta_2x_2 + ... + \theta_nx_n$

**In our case**

$h_\theta(x) = \theta_0 + \theta_1x_1 + \theta_2x_2 + \theta_3x_3 + \theta_4x_4$

### Writing the hypothesis formula in term on Matrices

with $x_0 = 1$. which gives $\theta_0x_0 = \theta_0$

Features vector as $x$  

Parameters vector as $\theta$

$ x = \begin{bmatrix}x_0 \\x_1 \\x_2 \\x_3 \\x_4 \end{bmatrix}$     $\space \space \space$ $\theta = \begin{bmatrix} \theta_0 \\ \theta_1 \\\theta_2 \\ \theta_3 \\ \theta_4 \end{bmatrix}$


To end up with our hypothesis formula, we can think of it as the product of 2 vectors. $x$ and the transpose of $\theta$

$ x = \begin{bmatrix}x_0 \\x_1 \\x_2 \\x_3 \\x_4 \end{bmatrix}$     $\space \space \space$ $\theta^T = \begin{bmatrix} \theta_0 & \theta_1 &\theta_2 & \theta_3 & \theta_4 \end{bmatrix}$


So we can resume the formula to :
$h_\theta(x) = h_\theta(x) = \theta_0 + \theta_1x_1 + \theta_2x_2 + ... + \theta_nx_n = \theta^Tx$

### Hypothesis computing in Python


```python
from sympy import *
```


```python
X = Matrix(['x0','x1','x2','x3','x4'])
X
```




$\displaystyle \left[\begin{matrix}x_{0}\\x_{1}\\x_{2}\\x_{3}\\x_{4}\end{matrix}\right]$




```python
T = Matrix([['T0','T1','T2','T3','T4']])
T
```




$\displaystyle \left[\begin{matrix}T_{0} & T_{1} & T_{2} & T_{3} & T_{4}\end{matrix}\right]$




```python
H = T.multiply(X)
H
```




$\displaystyle \left[\begin{matrix}T_{0} x_{0} + T_{1} x_{1} + T_{2} x_{2} + T_{3} x_{3} + T_{4} x_{4}\end{matrix}\right]$




```python

```

69dd45969f4f1b15e1b1693ab130ee5779b4db5b
ipynb
Jupyter Notebook
Andrew Ng - Coursera/Week 2/Multiple Features Regression.ipynb
chikoungoun/Machine-Learning
18625cacec264b612c4ec69cdc82cf7db46e6785
MIT
Andrew Ng - Coursera/Week 2/Multiple Features Regression.ipynb
chikoungoun/Machine-Learning
18625cacec264b612c4ec69cdc82cf7db46e6785
MIT
Andrew Ng - Coursera/Week 2/Multiple Features Regression.ipynb
chikoungoun/Machine-Learning
18625cacec264b612c4ec69cdc82cf7db46e6785
MIT
Qwen/Qwen-72B
1. YES
2. YES