Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Imputing Missing Values | Section
Machine Learning Foundations with Scikit-Learn
Abschnitt 1. Kapitel 9
single

single

bookChallenge: Imputing Missing Values

Swipe um das Menü anzuzeigen

The SimpleImputer class replaces missing values automatically.

from sklearn.impute import SimpleImputer
imputer = SimpleImputer()

Its key parameters:

  • missing_value: placeholder treated as missing (default np.nan);
  • strategy: method for filling gaps ('mean' by default);
  • fill_value: used when strategy='constant'.

As a transformer, it provides methods such as .fit(), .transform(), and .fit_transform().

Choosing how to fill missing data is essential. A common approach:

  • numerical features → mean;
  • categorical features → most frequent value.

strategy options:

  • 'mean' — fill with mean;
  • 'median' — fill with median;
  • 'most_frequent' — fill with mode;
  • 'constant' — fill with a specified value via fill_value.

missing_values defines which values are treated as missing (default NaN, but may be '' or another marker).

Note
Note

SimpleImputer expects a DataFrame, not a Series. A single-column DataFrame must be selected using double brackets:

imputer.fit_transform(df[['column']])

fit_transform() returns a 2D array, but assigning back to a DataFrame column requires a 1D array. Flatten the result using .ravel():

df['column'] = imputer.fit_transform(df[['column']]).ravel()
Aufgabe

Wischen, um mit dem Codieren zu beginnen

You are given a DataFrame df containing penguin data. The 'sex' column has missing values. Fill them using the most frequent category.

  1. Import SimpleImputer;
  2. Create an imputer with strategy='most_frequent';
  3. Apply it to df[['sex']];
  4. Assign the imputed values back to df['sex'].

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 9
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt