Skip to content

Commit c641de0

Browse files
authored
A series of fix for notebooks that were bugging (#545)
* up * correction * spacy download * solve intro problem * up * pd should be defined
1 parent 33d9ba7 commit c641de0

File tree

8 files changed

+33
-16
lines changed

8 files changed

+33
-16
lines changed

content/NLP/01_intro.qmd

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,11 @@ Les chapitres suivants seront utiles au cours de ce chapitre:
3737

3838
```{python}
3939
#| echo: true
40-
4140
!pip install pywaffle
4241
!pip install spacy
4342
!pip install plotnine
4443
!pip install great_tables
4544
!pip install wordcloud
46-
!pip install cleantext
47-
4845
```
4946

5047

@@ -307,6 +304,7 @@ la ligne de commande:
307304

308305
```{python}
309306
#| output: false
307+
#| echo: true
310308
!python -m spacy download fr_core_news_sm
311309
!python -m spacy download en_core_web_sm
312310
```

content/NLP/04_word2vec.qmd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ print_badges("content/NLP/04_word2vec.qmd", ssp_cloud_service="pytorch")
4242
:::
4343

4444

45+
::: {.warning}
46+
Ce chapitre va évoluer prochainement.
47+
:::
48+
49+
4550

4651
Cette page approfondit certains aspects présentés dans la
4752
[partie introductive](/content/NLP/02_exoclean.qmd). Après avoir travaillé sur le
@@ -710,18 +715,22 @@ Ces *embeddings* sont produits par des architectures très complexes,
710715
de type Transformer (`BERT`, etc.).
711716

712717

718+
_TODO: approfondir le sujet_
719+
713720
```{python}
714721
#| output: hide
715722
model = SentenceTransformer('all-mpnet-base-v2')
716723
```
717724

718725

719726
```{python}
727+
#| echo: false
720728
X_train_vectors = model.encode(X_train)
721729
```
722730

723731

724732
```{python}
733+
#| echo: false
725734
scores = cross_val_score(clf, X_train_vectors, y_train,
726735
cv=4, scoring='f1_micro', n_jobs=4)
727736
@@ -730,6 +739,7 @@ print(f"Mean F1 {np.mean(scores)}")
730739
```
731740

732741
```{python}
742+
#| echo: false
733743
perfs = pd.concat(
734744
[perfs,
735745
pd.DataFrame(

content/manipulation/03_geopandas_intro.qmd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,4 +1410,11 @@ base = (
14101410
)
14111411
ctx.add_basemap(base, source = ctx.providers.OpenStreetMap.Mapnik)
14121412
base.set_axis_off()
1413-
```
1413+
```
1414+
1415+
1416+
::: {.content-visible when-profile="fr"}
1417+
# Références
1418+
:::::: {.content-visible when-profile="fr"}
1419+
# References
1420+
:::

content/manipulation/03_geopandas_intro/_exo2.qmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#| output: false
33
#| echo: true
44
#| label: load-france-data
5+
from cartiflette import carti_download
56
france = carti_download(
67
values = ["France"],
78
crs = 4326,

content/manipulation/04_webscraping/_exo1_solution.qmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ dico_participants
155155

156156
```{python}
157157
#| echo: true
158+
import pandas as pd
158159
data_participants = pd.DataFrame.from_dict(dico_participants,orient='index')
159160
data_participants.head()
160161
```

content/modelisation/0_preprocessing.qmd

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ par comté x année avec en colonnes les résultats de chaque candidat dans cet
169169

170170
Si vous ne faites pas l'exercice 1, pensez à charger les données en executant la fonction `get_data.py` :
171171

172-
::: {.python}
173172
```{python}
174173
#| echo: true
175174
#| output: false
@@ -183,7 +182,6 @@ open('getdata.py', 'wb').write(r.content)
183182
import getdata
184183
votes = getdata.create_votes_dataframes()
185184
```
186-
:::
187185

188186

189187
Ce code introduit une base nommée `votes` dans l'environnement. Il s'agit d'une
@@ -700,6 +698,7 @@ onehot_enc.transform(df)
700698
Pour faire vos premiers pas en modélisation, notamment sur le preprocessing de données, vous pouvez également consulter le sujet 3 d'un hackathon organisé par l'Insee en 2023, *Explorer les habitudes alimentaires de nos compatriotes*, sur le [SSP Cloud](https://www.sspcloud.fr/formation?search=funath&path=%5B%22Funathon%202023%22%5D) ou sur [Github](https://github.com/InseeFrLab/funathon2023_sujet3/).
701699

702700
Le but du sujet est de travailler sur les données de consommations et habitudes alimentaires de l'étude INCA 3. Vous y travaillerez plusieurs thèmes :
701+
703702
- Analyse exploratoire de données et visualisations
704703
- Clustering d'individus : du preprocessing jusqu'aux méthodes classiques d'apprentissage non supervisé (ACP, K-moyennes, Clustering Ascendant Hiérarchique)
705704
- Prédiction de l'IMC : Premiers pas vers les méthodes d'apprentissage supervisé et les _preprocessings_ associés

content/modelisation/4_featureselection.qmd

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,22 @@ En adoptant le principe d'une fonction objectif pénalisée,
106106
le LASSO permet de fixer un certain nombre de coefficients à 0.
107107
Les variables dont la norme est non nulle passent ainsi le test de sélection.
108108

109-
::: {.cell .markdown}
110-
```{=html}
111-
<div class="alert alert-warning" role="alert">
112-
<h3 class="alert-heading"><i class="fa-solid fa-pencil"></i> Hint</h3>
113-
```
109+
::: {.tip}
114110
Le LASSO est un programme d'optimisation sous contrainte. On cherche à trouver l'estimateur $\beta$ qui minimise l'erreur quadratique (régression linéaire) sous une contrainte additionnelle régularisant les paramètres:
115111
$$
116112
\min_{\beta} \frac{1}{2}\mathbb{E}\bigg( \big( X\beta - y \big)^2 \bigg) \\
117113
\text{s.t. } \sum_{j=1}^p |\beta_j| \leq t
118114
$$
115+
119116
Ce programme se reformule grâce au Lagrangien est permet ainsi d'obtenir un programme de minimisation plus maniable :
117+
120118
$$
121119
\beta^{\text{LASSO}} = \arg \min_{\beta} \frac{1}{2}\mathbb{E}\bigg( \big( X\beta - y \big)^2 \bigg) + \alpha \sum_{j=1}^p |\beta_j| = \arg \min_{\beta} ||y-X\beta||_{2}^{2} + \lambda ||\beta||_1
122120
$$
121+
123122
où $\lambda$ est une réécriture de la régularisation précédente qui dépend de $\alpha$. La force de la pénalité appliquée aux modèles non parcimonieux dépend de ce paramètre.
124123

125-
```{=html}
126-
</div>
127-
```
124+
128125
:::
129126

130127
## Première régression LASSO
@@ -455,7 +452,6 @@ rmse_nonparci, rsq_nonparci = print_regression_metrics(
455452
Le modèle parcimonieux est (légèrement) plus performant:
456453

457454
```{python}
458-
#| output: asis
459455
pd.DataFrame({
460456
"parcimonieux": [rmse_parci, rsq_parci, len(features_selected)],
461457
"non parcimonieux": [rmse_nonparci, rsq_nonparci, ols_pipeline[-1].coef_.shape[1] + 1]},

content/modelisation/6_pipeline.qmd

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,8 @@ Reprendre les codes précédents et reconstruire notre _pipeline_ sur
838838
la nouvelle base en mettant en oeuvre une méthode de _boosting_
839839
plutôt qu'une forêt aléatoire.
840840

841+
_La correction de cet exercice est apparente pour simplifier les prochaines étapes mais essayez de faire celui-ci de vous-même_.
842+
841843
```{=html}
842844
</div>
843845
```
@@ -849,6 +851,7 @@ de celles-ci :
849851

850852
```{python}
851853
#| label: estim-model-paris
854+
#| echo: true
852855
from sklearn.ensemble import GradientBoostingRegressor
853856
854857
mutations_paris = mutations.drop(
@@ -908,6 +911,7 @@ la profondeur de l'arbre mais c'était un choix au doigt mouillé.
908911
❓️ Quels sont les hyperparamètres qu'on peut essayer d'optimiser ?
909912

910913
```{python}
914+
#| echo: true
911915
pipe['boosting'].get_params()
912916
```
913917

@@ -917,6 +921,7 @@ absurde de jouer sur le paramètre `random_state` qui est la racine du générat
917921
pseudo-aléatoire.
918922

919923
```{python}
924+
#| echo: true
920925
X = pd.concat((X_train, X_test), axis=0)
921926
Y = np.concatenate([y_train,y_test])
922927
```
@@ -977,7 +982,7 @@ param_grid = {
977982
"boosting__max_depth": [2,4]
978983
}
979984
grid_search = GridSearchCV(pipe, param_grid=param_grid)
980-
grid_search.fit(X, Y)
985+
grid_search.fit(X_train, y_train)
981986
982987
end_time = time.time()
983988

0 commit comments

Comments
 (0)