Skip to content

Commit d56f6e9

Browse files
committed
Un petit coup de neuf sur les consignes et corrections pandas related
1 parent 6e70b5a commit d56f6e9

File tree

6 files changed

+12
-18
lines changed

6 files changed

+12
-18
lines changed

content/modelisation/01_preprocessing/_exo5.qmd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
*Note : Le résultat du label encoding est relativement intuitif, notamment quand on le met en relation avec le vecteur initial.*
99

1010
3. Regarder la *dummy expansion* de `state_name`
11-
4. Appliquer un `OrdinalEncoder` à `df[['state_name', 'county_name']]`
11+
4. Appliquer un `OrdinalEncoder` à `df.loc[: , ['state_name', 'county_name']]`
1212
*Note : Le résultat du _ordinal encoding_ est cohérent avec celui du label encoding*
1313

14-
5. Appliquer un `OneHotEncoder` à `df[['state_name', 'county_name']]`
14+
5. Appliquer un `OneHotEncoder` à `df.loc[:, ['state_name', 'county_name']]`
1515

1616
*Note : `scikit` optimise l'objet nécessaire pour stocker le résultat d'un modèle de transformation. Par exemple, le résultat de l'encoding One Hot est un objet très volumineux. Dans ce cas, `scikit` utilise une matrice Sparse.*
1717

@@ -28,10 +28,10 @@
2828
*Note: The label encoding result is relatively intuitive, especially when related to the initial vector.*
2929

3030
3. Observe the dummy expansion of `state_name`
31-
4. Apply an `OrdinalEncoder` to `df[['state_name', 'county_name']]`
31+
4. Apply an `OrdinalEncoder` to `df.loc[:, ['state_name', 'county_name']]`
3232
*Note: The ordinal encoding result is consistent with the label encoding*
3333

34-
5. Apply a `OneHotEncoder` to `df[['state_name', 'county_name']]`
34+
1. Apply a `OneHotEncoder` to `df.loc[:, ['state_name', 'county_name']]`
3535

3636
*Note: `scikit` optimizes the object required to store the result of a transformation model. For example, the One Hot encoding result is a very large object. In this case, `scikit` uses a Sparse matrix.*
3737

content/modelisation/0_preprocessing.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ centroids["winner"] = np.where(centroids['votes_gop'] > centroids['votes_dem'],
339339
340340
centroids['lon'] = centroids['geometry'].x
341341
centroids['lat'] = centroids['geometry'].y
342-
centroids = pd.DataFrame(centroids[["county_name",'lon','lat','winner', 'CENSUS_2020_POP',"state_name"]])
342+
centroids = pd.DataFrame(centroids.loc[: , "county_name",'lon','lat','winner', 'CENSUS_2020_POP',"state_name"])
343343
groups = centroids.groupby('winner')
344344
345345
df = centroids.copy()

content/modelisation/3_regression.qmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ df2 = df2.dropna().astype(np.float64)
271271
272272
X_train, X_test, y_train, y_test = train_test_split(
273273
df2.drop(["Median_Household_Income_2021","per_gop"], axis = 1),
274-
100*df2[['per_gop']].values.ravel(), test_size=0.2, random_state=0
274+
100*df2['per_gop'], test_size=0.2, random_state=0
275275
)
276276
277277
ols = LinearRegression().fit(X_train, y_train)
@@ -628,7 +628,7 @@ df2['y'] = (df2['per_gop']>0.5).astype(int)
628628
629629
X_train, X_test, y_train, y_test = train_test_split(
630630
df2.drop(["Median_Household_Income_2021","y"], axis = 1),
631-
1*df2[['y']].values.ravel(), test_size=0.2, random_state=0
631+
1*df2['y'], test_size=0.2, random_state=0
632632
)
633633
634634
clf = LogisticRegression().fit(X_train, y_train)

content/modelisation/4_featureselection.qmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ df2.replace([np.inf, -np.inf], np.nan, inplace=True)
421421
#2. Echantillon d'entraînement et échantillon test
422422
X_train, X_test, y_train, y_test = train_test_split(
423423
df2.drop(["per_gop"], axis = 1),
424-
100*df2[['per_gop']], test_size=0.2, random_state=0
424+
100*df2['per_gop'], test_size=0.2, random_state=0
425425
)
426426
```
427427

@@ -688,7 +688,7 @@ from sklearn.preprocessing import StandardScaler
688688
df2.replace([np.inf, -np.inf], np.nan, inplace=True)
689689
X_train, X_test, y_train, y_test = train_test_split(
690690
df2.drop(["per_gop"], axis = 1),
691-
100*df2[['per_gop']], test_size=0.2, random_state=0
691+
100*df2['per_gop'], test_size=0.2, random_state=0
692692
)
693693
694694
numerical_features = X_train.select_dtypes(include='number').columns.tolist()

content/modelisation/6_pipeline.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ mutations2 = mutations2.groupby('dep').sample(frac = 0.1, random_state = 123)
520520
X_train, X_test, y_train, y_test = train_test_split(
521521
mutations2.drop("Valeur_fonciere", axis = 1),
522522
mutations2["Valeur_fonciere"],
523-
test_size = 0.2, random_state = 123, stratify=mutations2[['dep']]
523+
test_size = 0.2, random_state = 123, stratify=mutations2['dep']
524524
)
525525
```
526526

content/modelisation/_import_data_ml.qmd

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
Ce chapitre utilise toujours le même jeu de données, présenté dans l'[introduction
2-
de cette partie](index.qmd) : les données de vote aux élections présidentielles américaines
3-
croisées à des variables sociodémographiques.
4-
Le code
5-
est disponible [sur Github](https://github.com/linogaliana/python-datascientist/blob/main/content/modelisation/get_data.py).
1+
L'ensemble de la partie _machine learning_ utilise le même jeu de données, présenté dans l'[introduction de cette partie](index.qmd) : les données de vote aux élections présidentielles américaines croisées à des variables sociodémographiques. Le code est disponible [sur Github](https://github.com/linogaliana/python-datascientist/blob/main/content/modelisation/get_data.py).
62

73

84
```{python}
95
#| eval: false
106
#| echo: true
11-
!pip install --upgrade xlrd #colab bug verson xlrd
12-
!pip install geopandas
13-
!pip install openpyxl
7+
!pip install geopandas openpyxl plotnine plotly
148
```
159

1610
```{python}

0 commit comments

Comments
 (0)