```python
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import scipy
from scipy.stats import pearsonr
print("Pandas version: {}".format(pd.__version__))
print("Numpy version: {}".format(np.__version__))
print("Seaborn version: {}".format(sns.__version__))
print("Scipy version: {}".format(scipy.__version__))
print("Matplotlib version: {}".format(matplotlib.__version__))
```
Pandas version: 2.2.2
Numpy version: 1.26.4
Seaborn version: 0.13.2
Scipy version: 1.13.1
Matplotlib version: 3.9.2
```python
df = sns.load_dataset('tips')
df.head()
```
<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>total_bill</th>
<th>tip</th>
<th>sex</th>
<th>smoker</th>
<th>day</th>
<th>time</th>
<th>size</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>16.99</td>
<td>1.01</td>
<td>Female</td>
<td>No</td>
<td>Sun</td>
<td>Dinner</td>
<td>2</td>
</tr>
<tr>
<th>1</th>
<td>10.34</td>
<td>1.66</td>
<td>Male</td>
<td>No</td>
<td>Sun</td>
<td>Dinner</td>
<td>3</td>
</tr>
<tr>
<th>2</th>
<td>21.01</td>
<td>3.50</td>
<td>Male</td>
<td>No</td>
<td>Sun</td>
<td>Dinner</td>
<td>3</td>
</tr>
<tr>
<th>3</th>
<td>23.68</td>
<td>3.31</td>
<td>Male</td>
<td>No</td>
<td>Sun</td>
<td>Dinner</td>
<td>2</td>
</tr>
<tr>
<th>4</th>
<td>24.59</td>
<td>3.61</td>
<td>Female</td>
<td>No</td>
<td>Sun</td>
<td>Dinner</td>
<td>4</td>
</tr>
</tbody>
</table>
</div>
# Pearson
HO : "le montant du pourboire n'est pas corrélé au montant de l'addition"
```python
sns.scatterplot(x='total_bill', y='tip', data=df)
```
<Axes: xlabel='total_bill', ylabel='tip'>

```python
pearsonr(df['total_bill'], df['tip'])
```
PearsonRResult(statistic=0.6757341092113645, pvalue=6.692470646863819e-34)
On obtient une P_Value très faible --> il y a bien une corrélation clairement visible
```python
# En filtrant une partie des données
filtered_df = df.query("`sex`=='Female' & `day`=='Fri'")
sns.scatterplot(x='total_bill', y='tip', data=filtered_df)
```
<Axes: xlabel='total_bill', ylabel='tip'>

Avec aussi peu de point, il gaut être prudent avant de conclure qu'il existe une corélation --> le teste de Pearson peut être utile
```python
pearsonr(filtered_df['total_bill'], filtered_df['tip'])
```
PearsonRResult(statistic=0.7176682991060548, pvalue=0.02948261189502927)
# En résumé
```python
print(f"H0 :\"le montant du pourboire n'est pas corrélé au montant de l'addition\"")
print()
alpha = 0.02
p_value = pearsonr(filtered_df['total_bill'], filtered_df['tip']).pvalue
if p_value < alpha:
print("Nous avons suffisamment d'éléments pour rejeter H0")
else:
print("Nous n'avons pas suffisamment d'éléments pour rejeter H0")
```
H0 :"le montant du pourboire n'est pas corrélé au montant de l'addition"
Nous n'avons pas suffisamment d'éléments pour rejeter H0