<jupyter_start><jupyter_text>scikit-learnStrona biblioteki: [https://scikit-learn.org](https://scikit-learn.org)  Dokumentacja/User Guide: [https://scikit-learn.org/stable/user_guide.html](https://scikit-learn.org/stable/user_guide.html)Podstawowa biblioteka do uczenia maszynowego w języku Python.Aby zainstalować bibliotekę scikit-learn, użyj polecenia poniżej:```!pip install scikit-learn```Aby zaktualizować do najnowszej wersji bibliotekę scikit-learn, użyj polecenia poniżej:```!pip install --upgrade scikit-learn```Kurs stworzony w oparciu o wersję `0.22.1` Preprocessing danych:1. [Import bibliotek](0)2. [Wygenerowanie danych](1)3. [Utworzenie kopii danych](2)4. [Zmiana typu danych i wstępna eksploracja](3)5. [LabelEncoder](4)6. [OneHotEncoder](5)7. [Pandas *get_dummies()*](6)8. [Standaryzacja - StandardScaler](7)9. [Przygotowanie danych do modelu](8)   Import bibliotek<jupyter_code>import numpy as np
import pandas as pd
import sklearn

sklearn.__version__<jupyter_output><empty_output><jupyter_text>Wygenerowanie danych<jupyter_code>data = {
    'size': ['XL', 'L', 'M', 'L', 'M'],
    'color': ['red', 'green', 'blue', 'green', 'red'],
    'gender': ['female', 'male', 'male', 'female', 'female'],
    'price': [199.0, 89.0, 99.0, 129.0, 79.0],
    'weight': [500, 450, 300, 380, 410],
    'bought': ['yes', 'no', 'yes', 'no', 'yes']
}

df_raw = pd.DataFrame(data=data)
df_raw<jupyter_output><empty_output><jupyter_text>Utworzenie kopii danych<jupyter_code>df = df_raw.copy()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 6 columns):
size      5 non-null object
color     5 non-null object
gender    5 non-null object
price     5 non-null float64
weight    5 non-null int64
bought    5 non-null object
dtypes: float64(1), int64(1), object(4)
memory usage: 368.0+ bytes<jupyter_text>Zmiana typu danych i wstępna eksploracja<jupyter_code>for col in ['size', 'color', 'gender', 'bought']:
    df[col] = df[col].astype('category')

df['weight'] = df['weight'].astype('float')

df.info()
df.describe()
df.describe().T
df.describe(include=['category']).T
df<jupyter_output><empty_output><jupyter_text>LabelEncoder<jupyter_code>from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()
le.fit(df['bought'])
le.transform(df['bought'])
le.fit_transform(df['bought'])
le.classes_
df['bought'] = le.fit_transform(df['bought'])
df
le.inverse_transform(df['bought'])
df['bought'] = le.inverse_transform(df['bought'])
df<jupyter_output><empty_output><jupyter_text>OneHotEncoder<jupyter_code>from sklearn.preprocessing import OneHotEncoder

encoder = OneHotEncoder(sparse=False)
encoder.fit(df[['size']])
encoder.transform(df[['size']])
encoder.categories_
encoder = OneHotEncoder(drop='first', sparse=False)
encoder.fit(df[['size']])
encoder.transform(df[['size']])
df<jupyter_output><empty_output><jupyter_text>Pandas *get_dummies()*<jupyter_code>pd.get_dummies(data=df)
pd.get_dummies(data=df, drop_first=True)
pd.get_dummies(data=df, drop_first=True, prefix='new')
pd.get_dummies(data=df, drop_first=True, prefix_sep='-')
pd.get_dummies(data=df, drop_first=True, columns=['size'])<jupyter_output><empty_output><jupyter_text>Standaryzacja - StandardScalerstd() - pandas nieobciążony  std() - numpy obciążony<jupyter_code>print(f"{df['price']}\n")
print(f"Średnia: {df['price'].mean()}")
print(f"Odchylenie standardowe: {df['price'].std():.4f}")
(df['price'] - df['price'].mean()) / df['price'].std()
def standardize(x):
    return (x - x.mean()) / x.std()

standardize(df['price'])
from sklearn.preprocessing import scale

scale(df['price'])
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
scaler.fit(df[['price']])
scaler.transform(df[['price']])
scaler = StandardScaler()
df[['price', 'weight']] = scaler.fit_transform(df[['price', 'weight']])
df<jupyter_output><empty_output><jupyter_text>Przygotowanie danych do modelu<jupyter_code>df = df_raw.copy()
df
le = LabelEncoder()

df['bought'] = le.fit_transform(df['bought'])

scaler = StandardScaler()
df[['price', 'weight']] = scaler.fit_transform(df[['price', 'weight']])

df = pd.get_dummies(data=df, drop_first=True)
df<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup