forked from pms/uczenie-maszynowe
313 lines
9.0 KiB
Plaintext
313 lines
9.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"slideshow": {
|
|
"slide_type": "-"
|
|
}
|
|
},
|
|
"source": [
|
|
"### Uczenie maszynowe — laboratoria\n",
|
|
"# 5. Ewaluacja"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Do wykonania zadań wykorzystaj wiedzę z wykładów."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 5.1. Korzystanie z gotowych implementacji algorytmów na przykładzie pakietu *scikit-learn*"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"[Scikit-learn](https://scikit-learn.org) jest otwartoźródłową biblioteką programistyczną dla języka Python wspomagającą uczenie maszynowe. Zawiera implementacje wielu algorytmów uczenia maszynowego."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Poniżej przykład, jak stworzyć klasyfikator regresji liniowej wielu zmiennych z użyciem `scikit-learn`.\n",
|
|
"\n",
|
|
"Na podobnej zasadzie można korzystać z innych modeli dostępnych w bibliotece."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[[332187.32537534]\n",
|
|
" [369587.77676738]\n",
|
|
" [488428.70420785]\n",
|
|
" [300013.00301966]\n",
|
|
" [412118.79730411]\n",
|
|
" [283333.7605634 ]\n",
|
|
" [275209.84706017]\n",
|
|
" [361970.50784352]\n",
|
|
" [272402.36116539]\n",
|
|
" [328635.55642844]]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"import pandas as pd\n",
|
|
"\n",
|
|
"from sklearn.linear_model import LinearRegression\n",
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
"\n",
|
|
"\n",
|
|
"FEATURES = [\n",
|
|
" \"Powierzchnia w m2\",\n",
|
|
" \"Liczba pokoi\",\n",
|
|
" \"Liczba pięter w budynku\",\n",
|
|
" \"Piętro\",\n",
|
|
" \"Rok budowy\",\n",
|
|
"]\n",
|
|
"\n",
|
|
"\n",
|
|
"def preprocess(data):\n",
|
|
" \"\"\"Wstępne przetworzenie danych\"\"\"\n",
|
|
" data = data.replace({\"parter\": 0, \"poddasze\": 0}, regex=True)\n",
|
|
" data = data.applymap(np.nan_to_num) # Zamienia \"NaN\" na liczby\n",
|
|
" return data\n",
|
|
"\n",
|
|
"\n",
|
|
"# Nazwy plików\n",
|
|
"dataset_filename = \"flats.tsv\"\n",
|
|
"\n",
|
|
"# Wczytanie danych\n",
|
|
"data = pd.read_csv(dataset_filename, header=0, sep=\"\\t\")\n",
|
|
"columns = data.columns[1:] # wszystkie kolumny oprócz pierwszej (\"cena\")\n",
|
|
"data = data[FEATURES + [\"cena\"]] # wybór cech\n",
|
|
"data = preprocess(data) # wstępne przetworzenie danych\n",
|
|
"\n",
|
|
"# Podział danych na zbiory uczący i testowy\n",
|
|
"data_train, data_test = train_test_split(data, test_size=0.2)\n",
|
|
"\n",
|
|
"# Uczenie modelu\n",
|
|
"y_train = pd.DataFrame(data_train[\"cena\"])\n",
|
|
"x_train = pd.DataFrame(data_train[FEATURES])\n",
|
|
"model = LinearRegression() # definicja modelu\n",
|
|
"model.fit(x_train, y_train) # dopasowanie modelu\n",
|
|
"\n",
|
|
"# Predykcja wyników dla danych testowych\n",
|
|
"y_expected = pd.DataFrame(data_test[\"cena\"])\n",
|
|
"x_test = pd.DataFrame(data_test[FEATURES])\n",
|
|
"y_predicted = model.predict(x_test) # predykcja wyników na podstawie modelu\n",
|
|
"\n",
|
|
"print(y_predicted[:10]) # Pierwsze 10 wyników\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Biblioteka *scikit-learn* dostarcza również narzędzi do wstępnego przetwarzania danych, np. skalowania i normalizacji: https://scikit-learn.org/stable/modules/preprocessing.html"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 5.2. Metody ewaluacji"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Bilioteka *scikit-learn* dostarcza również narzędzi do ewaluacji algorytmów zaimplementowanych z wykorzystaniem jej metod.\n",
|
|
"\n",
|
|
"Te narzędzia znajdują się w module [`sklearn.metrics`](https://scikit-learn.org/stable/modules/classes.html#module-sklearn.metrics)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Ewaluacja regresji "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"Do ewaluacji regresji z powyższego przykładu możemy np. użyć metryki [`mean_squared_error`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html#sklearn.metrics.mean_squared_error):"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Błąd średniokwadratowy wynosi 1179760250402.185\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from sklearn.metrics import mean_squared_error\n",
|
|
"\n",
|
|
"error = mean_squared_error(y_expected, y_predicted)\n",
|
|
"\n",
|
|
"print(f\"Błąd średniokwadratowy wynosi {error}\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Większość modeli posiada też metodę `score`, która zwraca wartość metryki tak skonstruowanej, żeby jej wartość wynosiła `1.0`, jeżeli `y_predicted` jest równe `y_expected`. Im mniejsza wartość `score`, tym gorszy wynik."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"-10.712011261173265\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(model.score(x_test, y_expected))\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Ewaluacja klasyfikacji"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Dla ewaluacji algorytmów klasyfikacji możemy użyć metody [`precision_recall_fscore_support`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_recall_fscore_support.html), która oblicza wartości metryk precyzji, pokrycia i F-score. Przydatna może być też metoda [`classification_report`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Precision: 1.0\n",
|
|
"Recall: 1.0\n",
|
|
"F-score: 1.0\n",
|
|
"Model score: 1.0\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"/home/pawel/.local/lib/python3.10/site-packages/sklearn/utils/validation.py:1111: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
|
|
" y = column_or_1d(y, warn=True)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"\n",
|
|
"from sklearn.linear_model import LogisticRegression\n",
|
|
"from sklearn.metrics import precision_recall_fscore_support\n",
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
"\n",
|
|
"FEATURES = [\"pl\", \"pw\", \"sl\", \"sw\"]\n",
|
|
"\n",
|
|
"# Wczytanie danych\n",
|
|
"data_iris = pd.read_csv(\"../wyk/iris.csv\")\n",
|
|
"data_iris[\"Iris setosa?\"] = data_iris[\"Gatunek\"].apply(\n",
|
|
" lambda x: 1 if x == \"Iris-setosa\" else 0\n",
|
|
")\n",
|
|
"\n",
|
|
"# Podział danych na zbiór uczący i zbiór testowy\n",
|
|
"data_train, data_test = train_test_split(data_iris, test_size=0.2)\n",
|
|
"\n",
|
|
"# Uczenie modelu\n",
|
|
"y_train = pd.DataFrame(data_train[\"Iris setosa?\"])\n",
|
|
"x_train = pd.DataFrame(data_train[FEATURES])\n",
|
|
"model = LogisticRegression() # definicja modelu\n",
|
|
"model.fit(x_train, y_train) # dopasowanie modelu\n",
|
|
"\n",
|
|
"# Predykcja wyników\n",
|
|
"y_expected = pd.DataFrame(data_test[\"Iris setosa?\"])\n",
|
|
"x_test = pd.DataFrame(data_test[FEATURES])\n",
|
|
"y_predicted = model.predict(x_test) # predykcja wyników na podstawie modelu\n",
|
|
"\n",
|
|
"precision, recall, fscore, support = precision_recall_fscore_support(\n",
|
|
" y_expected, y_predicted, average=\"micro\"\n",
|
|
")\n",
|
|
"\n",
|
|
"print(f\"Precision: {precision}\")\n",
|
|
"print(f\"Recall: {recall}\")\n",
|
|
"print(f\"F-score: {fscore}\")\n",
|
|
"\n",
|
|
"score = model.score(x_test, y_expected)\n",
|
|
"\n",
|
|
"print(f\"Model score: {score}\")\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"celltoolbar": "Slideshow",
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.12"
|
|
},
|
|
"livereveal": {
|
|
"start_slideshow_at": "selected",
|
|
"theme": "amu"
|
|
},
|
|
"vscode": {
|
|
"interpreter": {
|
|
"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
|
|
}
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|