2022-05-17 17:30:50 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "TQqrOdkY6nsy"
},
"source": [
"# **Klasyfikacja za pomocą naiwnej metody bayesowskiej z rozkładem normalnym**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SSaJsYOhz8h8"
},
"source": [
"![rozklady.jpg](data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gMTAwCv/bAEMAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/bAEMBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAucC6QMBEQACEQEDEQH/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+fr/xAAfAQADAQEBAQEBAQEBAAAAAAAAAQIDBAUGBwgJCgv/xAC1EQACAQIEBAMEBwUEBAABAncAAQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYnKCkqNTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqCg4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2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AoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKAPkX5/9n9aAPrqgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKAPkX5/wDZ/WgD66oAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgD5F+f/AGf1oA+uqACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoA+Rfn/2f1oA+uqACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoA+Rfn/2f1oA+uqACgAoAKACgAoAKACgAoAKACg
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AlcfRFCPSXIj"
},
"source": [
"# **Twierdzenie Bayesa**\n",
"![bayes.svg](data:image/svg+xml;base64,PHN2ZyB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vc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pienia klasy A (tj. prawdopodobieństwo, że dowolny przykład należy do klasy A)\n",
"\n",
"P(B|A) -- oznacza prawdopodobieństwo a-posteriori, że B należy do \n",
"klasy A\n",
"\n",
"P(B) -- znacza prawdopodobieństwo a-priori wystąpienia przykładu B "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Yabcm4Rei2ue"
},
"source": [
"![GaussianNB.png](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8AAAAKACAYAAABT+RFDAAABhWlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9TpVUqDnYQdchQHcSCqIijVqEIFUKt0KqDyaVf0KQhSXFxFFwLDn4sVh1cnHV1cBUEwQ8QJ0cnRRcp8X9JoUWsB8f9eHfvcfcOEGolplkd44Cm22YyHhPTmVUx8IogBHRhEKMys4w5SUqg7fi6h4+vd1Ge1f7cn6NHzVoM8InEs8wwbeIN4ulN2+C8TxxmBVklPiceM+mCxI9cVzx+45x3WeCZYTOVnCcOE4v5FlZamBVMjXiKOKJqOuULaY9VzluctVKFNe7JXxjK6ivLXKc5hDgWsQQJIhRUUEQJNqK06qRYSNJ+rI1/wPVL5FLIVQQjxwLK0CC7fvA/+N2tlZuc8JJCMaDzxXE+hoHALlCvOs73sePUTwD/M3ClN/3lGjDzSXq1qUWOgN5t4OK6qSl7wOUO0P9kyKbsSn6aQi4HvJ/RN2WAvluge83rrbGP0wcgRV0lboCDQ2AkT9nrbd4dbO3t3zON/n4ARb1ylXHM+TcAAAAGYktHRAD0APwA/9UrKNsAAAAJcEhZcwAACxMAAAsTAQCanBgAAAAHdElNRQfkCAIJHTANHBdBAAAAGXRFWHRDb21tZW50AENyZWF0ZWQgd2l0aCBHSU1QV4EOFwAAIABJREFUeNrs3Xd4lfX9//HXnZzskDCSkEASVgxLZIugAkqrIEgrw4KihCVVsa2jitZRrP1+7VeLP5xQxdAUBC1EGnEUWTKkypAhIAEkJoEMdgbZ5/z+SLlznwxIQgJJzvNxXbmu3Ofc5z73f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7Nmz5jcunTp10saNG9WlSxfNmjVL3t7elZb3CcAAGrq31h/WK/+uvLtRgLdN658Yqlb+XtwoAECdhoHEjGzd9trGRnt9qx8drOjWzS77OAcPHtTkyZP1zTffVHiuV69emjlzpmbMmGHewwMHDqh79+4KCwvTDz/8oGbNKp7DyJEj9dlnn+mTTz7RqFGjqh2Ao6KidOTIEb3++ut65JFHnJ5zOBzq1auX9uzZoz//+c965plnJEkPP/yw3n77bcXHx+uuu+667PsxaNAgbd26Vfv27VO3bt0IwBa1ngV6wYIFWrBggaKjo/Xyyy8rNjZWkjR9+nQlJCRc8vXNmjXTe++9p1/+8peKiopSamqqnnvuOf7KAWh0jp/N05vryiYBbOZtU2RLX3M7K79Yr65mLA4AAPWlc+fO+s9//qNvvvlGzz//vG6//Xazurpr1y49+OCDGj58uAoLCyVJn332mRwOh0aMGFFp+JVKC36S9PXXX1f7PFJTU3XkyBFJ0uTJkys8bxiGpkyZIklav369+fj1118vSZo9e7bi4+OrPeTz8OHDevPNN/W73/1O06ZNMwuUF8ZFMxa4ItvlfMi2bt1a4fH33nuv0v3bt29vVn8vGD16tEaPHk0rAGjU/uezA8orKjG3F025Xn3btdDMf2zXv/eV/g9o2bYUTbw+UteFN+eGAQBQT66//nozTDocDn333Xd65ZVXtGzZMq1Zs0bz5s3T73//e/3444+SpIULF2rhwoUXPeaJEyeq/f7Hjh2TJLVq1UoBAQGV7nNhedgL+0rSfffdpy+//FJLlizR2LFj5e7urm7duummm27SuHHjdOuttzodo6SkRLNmzdKCBQt0sQ69WVlZfCjqKgADAKStR05p1Z40c3tMn7bq266FJOnZkd204eAJFRTb5XBILyTs04pfD5Kbm8GNAwDUiciWvlr96OBGff71xTAM9enTR0
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dsf6FnlgjiOL"
},
"source": [
"# Funkcja gęstości prawdopodobieństwa rozkładu normalnego \n",
"![gestosc.svg](data:image/svg+xml;base64,PHN2ZyB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53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IDM1N1Q0NzYgMzU4SDQxNkw0MjEgMzQ4UTQzOSAzMTAgNDM5IDI2M1E0MzkgMTUzIDM1OSA3MVQxODQgLTExWk0zNjEgMjc4UTM2MSAzNTggMjc2IDM1OFExNTIgMzU4IDExNSAxODRRMTE0IDE4MCAxMTQgMTc4UTEwNiAxNDEgMTA2IDExN1ExMDYgNjcgMTMxIDQ3VDE4OCAyNlEyNDIgMjYgMjg3IDczUTMxNiAxMDMgMzM0IDE1M1QzNTYgMjMzVDM2MSAyNzhaIj48L3BhdGg+CjxwYXRoIHN0cm9rZS13aWR0aD0iMSIgaWQ9IkUxLU1KTUFJTi0yOCIgZD0iTTk0IDI1MFE5NCAzMTkgMTA0IDM4MVQxMjcgNDg4VDE2NCA1NzZUMjAyIDY0M1QyNDQgNjk1VDI3NyA3MjlUMzAyIDc1MEgzMTVIMzE5UTMzMyA3NTAgMzMzIDc0MVEzMzMgNzM4IDMxNiA3MjBUMjc1IDY2N1QyMjYgNTgxVDE4NCA0NDNUMTY3IDI1MFQxODQgNThUMjI1IC04MVQyNzQgLTE2N1QzMTYgLTIyMFQzMzMgLTI0MVEzMzMgLTI1MCAzMTggLTI1MEgzMTVIMzAyTDI3NCAtMjI2UTE4MCAtMTQxIDEzNyAtMTRUOTQgMjUwWiI+PC9wYXRoPgo8cGF0aCBzdHJva2Utd2lkdGg9IjEiIGlkPSJFMS1NSk1BVEhJLTc4IiBkPSJNNTIgMjg5UTU5IDMzMSAxMDYgMzg2VDIyMiA0NDJRMjU3IDQ0MiAyODYgNDI0VDMyOSAzNzlRMzcxIDQ0MiA0MzAgNDQyUTQ2NyA0NDIgNDk0IDQyMFQ1MjIgMzYxUTUyMiAzMzIgNTA4IDMxNFQ0ODEgMjkyVDQ1OCAyO
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "v0oeHebytjNp"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import scipy.stats as stats\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"from sklearn.model_selection import train_test_split\n",
"sns.set(style=\"whitegrid\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "fOYTA3VVtjNw"
},
"outputs": [],
"source": [
"class NaiveBayesClassifier():\n",
" def calc_prior(self, features, target):\n",
" '''\n",
" Wyliczenie prawdopodobieństwa a priori\n",
" '''\n",
" self.prior = (features.groupby(target).apply(lambda x: len(x)) / self.rows).to_numpy()\n",
"\n",
" return self.prior\n",
" \n",
" def calc_statistics(self, features, target):\n",
" '''\n",
" Wyliczenie średnich i wariancji dla danych\n",
" ''' \n",
" self.mean = features.groupby(target).apply(np.mean).to_numpy()\n",
" self.var = features.groupby(target).apply(np.var).to_numpy()\n",
" \n",
" return self.mean, self.var\n",
" \n",
" def gaussian_density(self, class_idx, x): \n",
" '''\n",
" Wyliczenie prawdopodobieństwa z rozkładu normalnego \n",
" (1/√2pi*σ ) * exp((-1/2)*((x-μ)^2)/(2*σ²))\n",
" μ -średnia\n",
" σ² - wariancja\n",
" σ - odchylenie standardowe\n",
" '''\n",
" mean = self.mean[class_idx]\n",
" var = self.var[class_idx]\n",
2022-05-17 18:19:47 +02:00
" \n",
" numerator = np.exp((-1/2)*((x-mean)**2) / (2 * var)) # Licznik wzoru na gęstość rozkładu normalnego \n",
" denominator = np.sqrt(2 * np.pi * var) # Mianownik wzoru na gęstość rozkładu normalnego \n",
2022-05-17 17:30:50 +02:00
" prob = numerator / denominator\n",
2022-05-17 18:19:47 +02:00
" \n",
2022-05-17 17:30:50 +02:00
" return prob\n",
" \n",
2022-05-17 18:19:47 +02:00
" def classify(self, x):\n",
2022-05-17 17:30:50 +02:00
" '''\n",
" Wyliczenie prawdopodobieństwa a posteriori i zwrócenie klasy, dla której prawdopodobieństwo jest najwyższe\n",
" '''\n",
" posteriors = []\n",
" posteriors_no_log = []\n",
"\n",
" # calculate posterior probability for each class\n",
" for i in range(self.count):\n",
" prior = np.log(self.prior[i]) # Do predykcji używane jest prawodopodobieństwo logarytmiczne\n",
" prior_no_log = self.prior[i] # Zwykłe prawdopodobieństwo liczymy, żeby zwrócić je z predykcjami\n",
"\n",
" conditional = np.sum(np.log(self.gaussian_density(i, x))) \n",
" conditional_no_log = np.prod(self.gaussian_density(i, x))\n",
"\n",
" posterior = prior + conditional\n",
" posterior_no_log = prior_no_log * conditional_no_log\n",
"\n",
" posteriors.append(posterior)\n",
" posteriors_no_log.append(posterior_no_log)\n",
"\n",
" # Zwracamy klasę o największym prawdopodobieństwie\n",
" return self.classes[np.argmax(posteriors)], np.max(posteriors_no_log)\n",
"\n",
" def fit(self, features, target):\n",
" '''\n",
" Główna metoda trenująca model\n",
" '''\n",
" self.classes = np.unique(target)\n",
" self.count = len(self.classes)\n",
" self.feature_nums = features.shape[1]\n",
" self.rows = features.shape[0]\n",
" \n",
" self.calc_statistics(features, target)\n",
" self.calc_prior(features, target)\n",
" \n",
" def predict(self, features):\n",
" '''\n",
" Predykcja wartości dla każdego wiersza\n",
" '''\n",
2022-05-17 18:19:47 +02:00
" preds = [self.classify(f) for f in features.to_numpy()]\n",
2022-05-17 17:30:50 +02:00
" return preds\n",
"\n",
" def accuracy(self, y_test, y_pred):\n",
" '''\n",
" Wyliczenie accuracy modelu\n",
" '''\n",
" accuracy = np.sum(y_test == y_pred) / len(y_test)\n",
" return accuracy\n",
"\n",
" def visualize(self, y_true, y_pred, target):\n",
" '''\n",
" Narysowanie wykresu porównującego rozkład klas prawdziwych i przewidzianych\n",
" '''\n",
" tr = pd.DataFrame(data=y_true, columns=[target])\n",
" pr = pd.DataFrame(data=y_pred, columns=[target])\n",
" \n",
" \n",
" fig, ax = plt.subplots(1, 2, sharex='col', sharey='row', figsize=(15,6))\n",
" \n",
2022-05-17 18:19:47 +02:00
" sns.countplot(x=target, data=tr, ax=ax[0], alpha=0.7, hue=target, dodge=False)\n",
" sns.countplot(x=target, data=pr, ax=ax[1], alpha=0.7, hue=target, dodge=False)\n",
2022-05-17 17:30:50 +02:00
" \n",
" ax[0].tick_params(labelsize=12)\n",
" ax[1].tick_params(labelsize=12)\n",
" ax[0].set_title(\"Prawdziwe wartości\", fontsize=18)\n",
2022-05-17 18:19:47 +02:00
" ax[1].set_title(\"Predykcje\", fontsize=18)\n",
2022-05-17 17:30:50 +02:00
" plt.show()\n"
]
},
2022-05-17 18:54:16 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pitność wody"
]
},
2022-05-17 17:30:50 +02:00
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 382
},
"id": "5-riUAGntjN2",
"outputId": "f87f047d-bc71-41ef-a43a-17b6f7cf84c3"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2948, 9) (2948,)\n",
"(328, 9) (328,)\n"
]
}
],
"source": [
"# Preprocessing danych\n",
"\n",
"# Uzupełnienie pustych wartości w kolumnach\n",
"def fill_nan(df):\n",
" for index, column in enumerate(df.columns[:9]):\n",
" df[column] = df[column].fillna(df.groupby('Potability')[column].transform('mean'))\n",
" return df\n",
"\n",
"# Wczytywanie danych\n",
"df = pd.read_csv(\"water_potability.csv\")\n",
"\n",
"df = fill_nan(df)\n",
"\n",
"# Zrandomizowanie kolejności danych w datasecie\n",
"df = df.sample(frac=1, random_state=10).reset_index(drop=True)\n",
"\n",
"# Podział na atrybuty i przewidywane wartości\n",
"X, y = df.iloc[:, :-1], df.iloc[:, -1]\n",
"\n",
"# Normalizacja i skalowanie danych\n",
"from sklearn.preprocessing import StandardScaler\n",
"sc = StandardScaler()\n",
"X = sc.fit_transform(X.to_numpy())\n",
"X = pd.DataFrame(X, columns=df.columns.values.tolist()[:-1])\n",
"\n",
"# Podział na dane trenujące i testowe, z uwzględnieniem równego rozłożenia danych\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, stratify=y, random_state=1)\n",
"\n",
"print(X_train.shape, y_train.shape)\n",
"print(X_test.shape, y_test.shape)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "O82SGzK6tjN5"
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>ph</th>\n",
" <th>Hardness</th>\n",
" <th>Solids</th>\n",
" <th>Chloramines</th>\n",
" <th>Sulfate</th>\n",
" <th>Conductivity</th>\n",
" <th>Organic_carbon</th>\n",
" <th>Trihalomethanes</th>\n",
" <th>Turbidity</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1022</th>\n",
" <td>0.003078</td>\n",
" <td>0.688791</td>\n",
" <td>0.846257</td>\n",
" <td>1.428934</td>\n",
" <td>-0.858263</td>\n",
" <td>0.002792</td>\n",
" <td>0.913790</td>\n",
" <td>0.232417</td>\n",
" <td>2.319505</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3191</th>\n",
" <td>-0.587365</td>\n",
" <td>0.223203</td>\n",
" <td>-0.731867</td>\n",
" <td>0.397503</td>\n",
" <td>0.759893</td>\n",
" <td>0.330607</td>\n",
" <td>0.094379</td>\n",
" <td>0.282563</td>\n",
" <td>0.235024</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>0.003078</td>\n",
" <td>-0.241037</td>\n",
" <td>0.773051</td>\n",
" <td>0.580019</td>\n",
" <td>1.334369</td>\n",
" <td>-0.049130</td>\n",
" <td>-1.121422</td>\n",
" <td>-0.200432</td>\n",
" <td>-0.946356</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2068</th>\n",
" <td>-2.176058</td>\n",
" <td>1.443006</td>\n",
" <td>-1.626771</td>\n",
" <td>-4.164610</td>\n",
" <td>-0.033706</td>\n",
" <td>-1.050763</td>\n",
" <td>-0.391328</td>\n",
" <td>-0.398649</td>\n",
" <td>-0.298341</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1484</th>\n",
" <td>0.213047</td>\n",
" <td>0.403036</td>\n",
" <td>-0.464729</td>\n",
" <td>0.070417</td>\n",
" <td>0.021560</td>\n",
" <td>-0.952776</td>\n",
" <td>-0.213330</td>\n",
" <td>0.111419</td>\n",
" <td>-0.235893</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>691</th>\n",
" <td>0.003078</td>\n",
" <td>1.199106</td>\n",
" <td>-0.003483</td>\n",
" <td>-0.670308</td>\n",
" <td>-0.069513</td>\n",
" <td>0.185754</td>\n",
" <td>-0.466010</td>\n",
" <td>0.031975</td>\n",
" <td>0.676276</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1283</th>\n",
" <td>-2.034004</td>\n",
" <td>-1.508135</td>\n",
" <td>0.255310</td>\n",
" <td>0.083839</td>\n",
" <td>-1.413707</td>\n",
" <td>0.694074</td>\n",
" <td>-1.110579</td>\n",
" <td>0.232996</td>\n",
" <td>2.544703</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2818</th>\n",
" <td>-0.702987</td>\n",
" <td>-0.575677</td>\n",
" <td>0.755056</td>\n",
" <td>0.664695</td>\n",
" <td>0.021560</td>\n",
" <td>-0.489334</td>\n",
" <td>0.371852</td>\n",
" <td>-2.272990</td>\n",
" <td>-1.764684</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1330</th>\n",
" <td>1.525943</td>\n",
" <td>0.497074</td>\n",
" <td>-0.714355</td>\n",
" <td>-1.024237</td>\n",
" <td>-1.022037</td>\n",
" <td>-0.327074</td>\n",
" <td>-1.107341</td>\n",
" <td>0.517432</td>\n",
" <td>-1.230528</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1926</th>\n",
" <td>-0.043558</td>\n",
" <td>-0.882359</td>\n",
" <td>-0.456141</td>\n",
" <td>-0.770271</td>\n",
" <td>0.795189</td>\n",
" <td>0.560306</td>\n",
" <td>-1.086081</td>\n",
" <td>-1.356820</td>\n",
" <td>0.172521</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>2948 rows × 9 columns</p>\n",
"</div>"
],
"text/plain": [
" ph Hardness Solids Chloramines Sulfate Conductivity \\\n",
"1022 0.003078 0.688791 0.846257 1.428934 -0.858263 0.002792 \n",
"3191 -0.587365 0.223203 -0.731867 0.397503 0.759893 0.330607 \n",
"13 0.003078 -0.241037 0.773051 0.580019 1.334369 -0.049130 \n",
"2068 -2.176058 1.443006 -1.626771 -4.164610 -0.033706 -1.050763 \n",
"1484 0.213047 0.403036 -0.464729 0.070417 0.021560 -0.952776 \n",
"... ... ... ... ... ... ... \n",
"691 0.003078 1.199106 -0.003483 -0.670308 -0.069513 0.185754 \n",
"1283 -2.034004 -1.508135 0.255310 0.083839 -1.413707 0.694074 \n",
"2818 -0.702987 -0.575677 0.755056 0.664695 0.021560 -0.489334 \n",
"1330 1.525943 0.497074 -0.714355 -1.024237 -1.022037 -0.327074 \n",
"1926 -0.043558 -0.882359 -0.456141 -0.770271 0.795189 0.560306 \n",
"\n",
" Organic_carbon Trihalomethanes Turbidity \n",
"1022 0.913790 0.232417 2.319505 \n",
"3191 0.094379 0.282563 0.235024 \n",
"13 -1.121422 -0.200432 -0.946356 \n",
"2068 -0.391328 -0.398649 -0.298341 \n",
"1484 -0.213330 0.111419 -0.235893 \n",
"... ... ... ... \n",
"691 -0.466010 0.031975 0.676276 \n",
"1283 -1.110579 0.232996 2.544703 \n",
"2818 0.371852 -2.272990 -1.764684 \n",
"1330 -1.107341 0.517432 -1.230528 \n",
"1926 -1.086081 -1.356820 0.172521 \n",
"\n",
"[2948 rows x 9 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X_train"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "a3jkTMFLtjN6"
},
"outputs": [],
"source": [
"# Trenowanie modelu klasyfikatora\n",
"x = NaiveBayesClassifier()\n",
"x.fit(X_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "CoC22aNgtjN9"
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Predykcja wartości dla danych testowych\n",
"predictions = x.predict(X_test)\n",
"\n",
"# Prawdopodobieństwa kolejnych predykcji\n",
"probabilities = [p[1] for p in predictions]\n",
"\n",
"# Przewidziana wartość\n",
"predictions = [p[0] for p in predictions]\n",
"predictions[0]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "JR06zodmtjN9"
},
"outputs": [
{
"data": {
"text/plain": [
"0.6280487804878049"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Wyliczenie accuracy modelu\n",
"x.accuracy(y_test, predictions)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "1jW0QPootjN_"
},
"outputs": [
{
"data": {
"text/plain": [
"0.14084507042253522"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.metrics import f1_score\n",
"\n",
"f1_score(y_test, predictions)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "vEVogTmAtjOA"
},
"outputs": [
{
"data": {
"text/plain": [
"0 0.609756\n",
"1 0.390244\n",
"Name: Potability, dtype: float64"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y_test.value_counts(normalize=True)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"id": "jCVOdBZytjOB"
},
"outputs": [
{
"data": {
2022-05-17 18:19:47 +02:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA4oAAAGQCAYAAADoX54sAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3XtYlHX+//EXIKcBRPFcahggWHhAAfFE4SE1c9XMsjXbykpTUXNd11Or61qutqlFhu3qama5pkWeSlNzPSJmlq4pqZSiZpEiGIwNp/n94Zf5eQcq6MBweD6uy+uCz316z5jz7jX3575vJ6vVahUAAAAAAP/H2dEFAAAAAAAqFoIiAAAAAMCAoAgAAAAAMCAoAgAAAAAMCIoAAAAAAAOCIoBq5b333tM777yjgoICR5cCAABQYREUATuYNGmSgoODS71dUlKSgoOD9dFHH5VBVfitpUuX6h//+Idat24tZ+fSf/x17dpVQ4cOLYPKAAD29NFHHyk4OFhJSUm3tL29P+/j4uIUHByss2fP2m2fQFmr4egCUH0lJSXpySefNIyZTCY1a9ZM/fr10xNPPCEXFxcHVQdHOnv2rBISEtS9e3e1aNHCLvtMTEzUG2+8oUWLFqlNmzZ22ScA4P+jrwNVC0ERDvfQQw8pOjpaVqtVaWlpSkhI0CuvvKKTJ0/qb3/7m6PLK1MRERE6fPiwatTgn+K1zp07pzfffFN33nmn3YLid999p0WLFql9+/a3vI9NmzbZpRYAqMqqc1+/nhdeeEHPP/+83NzcHF0KUGL83ykc7p577lG/fv1sv//+979X7969tXr1ao0dO1Z169Ytdrvc3FwVFBTI3d29vEq1O2dn50pdv71lZWXJ29u7TPY9ZMiQ294HDR4Abu5W+npV6Ok3UqNGDb4URqXDNYqocLy9vRUWFiar1aozZ85I+v9z+0+cOKHZs2crOjparVq10tdffy1J+uSTTzRixAjdf//9Cg0NVfv27TVy5EglJycb9j1p0iS1bNlSFovFNnbw4EEFBwcrMjLScIOTHTt2KDg4WJ988oltzGKxaM6cOercubNatWqlRx55RLt37y7yGgqvjbjen8JrJn57jaLFYlGrVq00adIkw/5eeuklBQcH6+WXXzaMjxs3Tm3btlVeXp5t7JdfftGrr76qHj16KDQ0VFFRURo/frztvbyR4q7pyM3NVVhYmIKDg3Xs2DHbeFZWlu69917NmDHDNrZ7926NGzdO3bp1U6tWrRQeHq5nnnlG+/fvL3KsoUOHqmvXrjpz5ozGjBmjyMhItWvXTh999JFt6tLkyZNt79m114qYzWa99tpr6t69u0JDQ9WpUydNnDhR586dMxzDarVq2bJl6tu3r8LCwtS2bVv17NlTU6ZMUW5urmHdo0ePasyYMerYsaNCQ0N13333afz48UpNTbWtwzWKAFB6v+3rN+vpkrR3714988wzCg8PV8uWLdW3b1+tXLmy2P2vXr1avXr1UmhoqHr06KF33nlHVqvVsM7SpUsVHBysvXv3Ftk+JydHkZGR+sMf/nDD13HmzBn17NlTnTt3Nvz/RVZWlubPn6/evXurZcuWat++vR5//HFt3LjRts71rlG8nZ4NlDW+2kCFY7Vadfr0aUlS7dq1DcsmTJggDw8PPfPMM5KkevXqSZJWrFghX19fPfroo6pXr55SU1P1wQcf6PHHH1dCQoL8/f0lSVFRUUpISNDBgwfVoUMHSdK+ffvk7OyszMxMHT16VKGhobZxJycnRUVF2Y4/fvx4bd26VTExMerSpYtSU1MVGxurxo0bG+qMiIjQ3Llzi7yu+fPnKy0trcjrKuTu7q42bdpo3759hvHCGq8dt1qt2r9/vyIiImzfUv7yyy8aPHiwfvjhBw0cOFBBQUH6+eef9f7772vQoEH68MMPdeedd173vS98TxITE21TNA8dOiSz2SxnZ2clJibapoIeOHBAeXl5hvcnISFBmZmZ6t+/vxo2bKiffvpJq1ev1lNPPaXly5crPDzccLzs7Gw98cQTatu2rcaNG6f09HRFRERoxIgRWrRokR577DG1a9dOkmzfQOfl5WnYsGE6ePCgevbsqaefflqnT5/WypUrtWfPHn344Ydq2LChJOmtt97SG2+8oZiYGA0ePFguLi46e/asPv/8c+X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I0bNgww7G7d++uZs2a3fA1rl69WkuWLNHgwYP11FNP3XDdwmMW1pKYmCgXFxfFxsbKycnJNl44PbRwfavVqvXr1ysiIkL169dXenq67Y+np6fatGlT7DTZ4o5/5MgRZWdn247Tvn17dezYUQcOHLBN2SycNlsYpCUZQmJ2drYuXbokZ2dntW7d2vD3cK3fvp83s2XLFjk7O2v48OGG8fvvv18tWrTQtm3bbM3Z29tbP/30kw4cOHDd/e3evVuXLl3S008/bQiJhW7lMRoAUJ2VtK8X19M3b96snJwcPfLII4Y+lp6erq5du6qgoECJiYmSrn5+X7lyRUOGDLGFRElq2LCh+vbtW6SuRx99VDk5OVq/fr1t7OzZs0pMTFTfvn2LvT5y7dq1Gj58uNq3b69ly5YZQmJBQYE++eQTBQQE6NFHHy2y7Y36h716NlCWOKMIh3vsscfUq1cvOTk5ydPTU/7+/oZv+q712zOMhY4eParXX39d+/fvl9lsNiz77bTQ9u3b68MPP1RWVpb+97//yWKxKCoqSrm5uVqwYIFycnK0f/9+FRQUGILimTNn5OzsXGwNAQEB+v7774utLTExUTNmzFDnzp310ksv3eCduKpVq1YymUzat2+fBg8erH379ik0NFRNmzZV8+bNtW/fPj300EPat2+fatWqZfsWMj09XRkZGdq9e7ftbOlvlST0REVF6YMPPtCBAwfUvn17ff3115oyZYpat26tOXPm6PDhwwoMDFRycnK
2022-05-17 17:30:50 +02:00
"text/plain": [
"<Figure size 1080x432 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x.visualize(y_test, predictions, 'Potability')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"id": "aw8Tefprhjnn"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/lib/python3/dist-packages/matplotlib/cbook/__init__.py:1377: FutureWarning: Support for multi-dimensional indexing (e.g. `obj[:, None]`) is deprecated and will be removed in a future version. Convert to a numpy array before indexing instead.\n",
" x[:, None]\n",
"/usr/lib/python3/dist-packages/matplotlib/axes/_base.py:237: FutureWarning: Support for multi-dimensional indexing (e.g. `obj[:, None]`) is deprecated and will be removed in a future version. Convert to a numpy array before indexing instead.\n",
" x = x[:, np.newaxis]\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXMAAAELCAYAAAAry2Y+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3XtcFPX+P/DX7rLLVYUQFNPykhiKpqJHMz3eUxTFjMRU8FJ6vKX57WYPTa3ItHrksbxlP29odkozvJKWmpdKT5KZpB4M8QYIKKjIIiy78/uDdmNhL7MXdpfp9Xw8fMjufOYz75nPe947O7s7IxMEQQAREdVpcncHQEREjmMxJyKSABZzIiIJYDEnIpIAFnMiIglgMScikgAWcyJyqoSEBGzbts3weNmyZejWrRueeOIJN0YlfSzmHuTkyZP45z//WeP56jsHkSucOnUKo0ePRlRUFP7xj39g9OjR+O2332zqIzc3Fxs2bMC+ffvwww8/WG0/d+5cLFu2zN6Q/9a83B0AEXmee/fuYerUqVi0aBGio6Oh0Whw6tQpqFQqm/rJzs5GYGAggoODaylS0uORuRv069cPn3zyCYYMGYKuXbvi9ddfR1lZmbvDIjLIysoCAMTExEChUMDHxwc9e/bEo48+io8//hgvv/yyoe3169fRpk0bVFRUGPXx448/YtKkScjPz0enTp0wd+5cAMCsWbPwxBNPICoqCmPHjsXFixcBAF988QV2796NdevWoVOnTpg6dSoAIC8vDy+88AK6d++Ofv36ITk52RWboM5hMXcTfdJ+++23yMrKwqpVq9wdEpFBixYtoFAo8Nprr+HIkSO4c+eOzX306NEDn376KUJDQ3H69GksWbIEAPDPf/4T+/fvx08//YS2bdsaXhji4+MxbNgwPPfcczh9+jTWrFkDnU6HadOmoU2bNjh69Cg2bdqETZs24dixY05dXylgMXeTsWPHIiws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TkQkASzmREQSwGJORCQBLOZERBLAYk5EJAEs5kREEsBiTkQkASzmREQSwGJORCQBLOZERBLAYk5EJAEs5kREEsBiTkQkASzmREQSwGJORCQBLOZERBLAYk5EJAEs5kREEsBiTkQkASzmREQSIKqYZ2VlIT4+HoMGDUJ8fDwuX75cy2ERuQZzm6TCS0yjhQsXYsyYMYiNjcXOnTuxYMECJCcn273Q79OuITn1PG4WlSLATwkAuKfWoGGQLxKjI9AnqpnJedamnEWxWmP0vLdSDpVSUeN5j7L1ursjMAgJ8oW2QovC4nKj5+v5KY3GoJ6VfqqOoblxE9PG0WU4ytm57anM7T8AIJMBguCGoKrzoP1Er56fElNGtAcAJKeeR0FRKeRyGXQ6AQ38FHge15yek/ayemR+69YtnDt3DjExMQCAmJgYnDt3DoWFhXYt8Pu0a1ix7QwKikohAChWa1Cs1kAAUFBUihXbzuD7tGtG8/yWVYLlX5w2mYhlGp1nF3IPU1BUWqOQA6gxBr9llZjto/oYmho3MW0scXR+MZyd257q+7Rr+Pd/TO8/gIcUcg9VrNbgw62/YPkXp1FQVAoA0OkqN9gdtdbpOekIq8U8NzcXjRo1gkKhAAAoFAqEhoYiNzfXrgUmp55HmUZrdnqZRovk1PNGzx08cxcVWmacq5RptDh45q7Z6abGsPq4iWljiaPzi+Hs3PZUyannodVx/7GXAJitP87OSUeIOs3iqPT0dMPf+lc3SwqKSpGWlmZ4fEdtvvjXdRp1IbIOLUHrIe9CJle4OxyDO2qt0RhUZW4Mq46bmDaWODq/K1TNa09TdRuJ2eccdf3kOtRr8hgaNOtS68syJ2PPq2je91Wo/Bu6dLmekpNWi3l
"text/plain": [
"<Figure size 432x288 with 6 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"ph_val = X_test[\"ph\"]\n",
"sulfate_val = X_test[\"Sulfate\"]\n",
"hard_val = X_test[\"Hardness\"]\n",
"carb_val = X_test[\"Organic_carbon\"]\n",
"turb_val = X_test[\"Turbidity\"]\n",
"ch_val = X_test[\"Chloramines\"]\n",
"\n",
"\n",
"figure, axes = plt.subplots(nrows=3, ncols=2)\n",
"\n",
"axes[0, 0].plot(ph_val, predictions, 'bo')\n",
"axes[0, 0].set_title(\"pH\")\n",
"\n",
"axes[0, 1].plot(sulfate_val, predictions, 'bo')\n",
"axes[0, 1].set_title(\"Sulfate\")\n",
"\n",
"axes[1, 0].plot(hard_val, predictions, 'bo')\n",
"axes[1, 0].set_title(\"Hardness\")\n",
"\n",
"axes[1, 1].plot(carb_val, predictions, 'bo')\n",
"axes[1, 1].set_title(\"Organic carbon\")\n",
"\n",
"axes[2, 0].plot(turb_val, predictions, 'bo')\n",
"axes[2, 0].set_title(\"Turbidity\")\n",
"\n",
"axes[2, 1].plot(ch_val, predictions, 'bo')\n",
"axes[2, 1].set_title(\"Chloramines\")\n",
"\n",
"plt.show()"
]
2022-05-17 18:37:29 +02:00
},
{
"cell_type": "code",
2022-05-17 18:54:16 +02:00
"execution_count": 12,
2022-05-17 18:37:29 +02:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2022-05-17 18:54:16 +02:00
"<matplotlib.collections.PathCollection at 0x7fbbadd08048>"
2022-05-17 18:37:29 +02:00
]
},
2022-05-17 18:54:16 +02:00
"execution_count": 12,
2022-05-17 18:37:29 +02:00
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAEBCAYAAAB7Wx7VAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzsnWdgFFUXhp/ZnoQQIIRepUmR3gSlowgoTarSLIAiTQX96CAqgoiKINKkCCgIKEV6L9JCDb2FkoQAIYVsts98PxYiIbubTYEEcp8/ypQ7Zza779w59xRJURQFgUAgEDzzqDLbAIFAIBA8GYTgCwQCQTZ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ek5yVs4D5UbVnjiZTpqv1aNw5uOYY5PPstXFKjcsAK3jTefqE2CjEPM8J8SAgvm5t1pXciVLwAff+cMUO+jo8KLZRm7amiax/1nzlbio42JYv8Ac4KF3yf+hTEuIb2mC7xEkiSKVChIs7cbULVxpUypydS460vkCPBL5l7SGbRUa1KJYs8XfuI2CTIOMcN/iiheuTC/h/3CyV1niLkVS8nKxSlevki6xtz1534sJqvLfRqdmlN7z1H7tWrpukZWw2F3oFKrRD16Fxh89fz471dM7P4jZw5cQKvTYLPaadz1JQb89G5mmydIJ0LwnzLUajVVG1fKuPFS6L37LPXm3bf6ENOHzOV26F3UWjUN3qxLn8k9CCzoXRvO7EJQkUCmbB9HVEQ00ZExFCyZ74muIwgeH8/Or1mQJpp3b5isrv8DZFmh0kvPP2GLHg+bFu7gq27fc+tKFIqiYLfa2blsHx/WGEZc1L1kx98JiyLiSmS2jlgKLJib0lV
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from sklearn.decomposition import PCA\n",
"\n",
"pca = PCA(n_components=2)\n",
"pca.fit(X_test)\n",
"X_pca = pca.transform(X_test)\n",
"\n",
"plt.scatter(X_pca[:, 0], X_pca[:, 1], c=predictions, s=50, cmap='viridis')"
]
},
{
"cell_type": "code",
2022-05-17 18:54:16 +02:00
"execution_count": 13,
2022-05-17 18:37:29 +02:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2022-05-17 18:54:16 +02:00
"<matplotlib.collections.PathCollection at 0x7fbbad8e7358>"
2022-05-17 18:37:29 +02:00
]
},
2022-05-17 18:54:16 +02:00
"execution_count": 13,
2022-05-17 18:37:29 +02:00
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAEBCAYAAAB7Wx7VAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGx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KsEiR8URWHEkg8oX7MMBrMBS4gZS6iZqNJFGLn8Q4LDg2+aLtbk9IADC03TsKVd3mNWUpCQPvwCwgMv3cf0L+aAM2v2psGop3qjypSvWfaa5AotFRHfDbR44IJsLR5SRyBEEkrIK9epuSQnFI2O4sdtI5nz63yClTCKRkdSp3nNm16mo/H99dm0eBv2NN9RvhBQp3lN4qxnb6pOktxDjvALCFGlivDst48QUTwcS6h3BGiyGKnZtCofzhpwzXJF+jTQUsgw9hnYIO1HhJZ2XXpLco6iKJSpWYo2T9xLvZa186QmU8tH7yYkPNjHvWQ0G6jfqjblqkffdJ0kuYcc4RcgyteJZurpH9m5ai9J55KpWKc85WuUuT6h9oVAgGm6YgDXZjDdWmV8hXADOlmP3g/mIBPf/DucT5/8hr3rD2Iw6nE53bR89G76fvdsXqsnuU6kwS9g6HQ66rWsnXsClSuFct46PxFhX0r14sMQsTGAHmFujxL6ForON/O1MFOsTBSjlg8h/kwiibFJlKpY/KauI0huHNKlU9gxdwUsAXZqYMy/vUGvBi19FiLpdSyG04AAXGBfgIjvitASfY4XnrMI98lCHbEUVaoIletVlMb+FkIa/EKKy+nClmYDy4OgiwaMlx1hhtB3UBRzXqiXqwjhgtTh+LquPKAlI6y
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y_test, s=50, cmap='viridis')"
]
2022-05-17 18:54:16 +02:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Irysy"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(105, 4) (105,)\n",
"(45, 4) (45,)\n",
"0.9333333333333333\n"
]
}
],
"source": [
"# Preprocessing danych\n",
"\n",
"\n",
"# Wczytywanie danych\n",
"df = pd.read_csv(\"iris.csv\")\n",
"\n",
"# Zrandomizowanie kolejności danych w datasecie\n",
"df = df.sample(frac=1, random_state=10).reset_index(drop=True)\n",
"\n",
"# Podział na atrybuty i przewidywane wartości\n",
"X, y = df.iloc[:, :-1], df.iloc[:, -1]\n",
"\n",
"# Normalizacja i skalowanie danych\n",
"from sklearn.preprocessing import StandardScaler\n",
"sc = StandardScaler()\n",
"X = sc.fit_transform(X.to_numpy())\n",
"X = pd.DataFrame(X, columns=df.columns.values.tolist()[:-1])\n",
"\n",
"# Podział na dane trenujące i testowe, z uwzględnieniem równego rozłożenia danych\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y, random_state=1)\n",
"\n",
"print(X_train.shape, y_train.shape)\n",
"print(X_test.shape, y_test.shape)\n",
"\n",
"\n",
"# Trenowanie modelu klasyfikatora\n",
"x = NaiveBayesClassifier()\n",
"x.fit(X_train, y_train)\n",
"\n",
"\n",
"# Predykcja wartości dla danych testowych\n",
"predictions = x.predict(X_test)\n",
"\n",
"# Prawdopodobieństwa kolejnych predykcji\n",
"probabilities = [p[1] for p in predictions]\n",
"\n",
"# Przewidziana wartość\n",
"predictions = [p[0] for p in predictions]\n",
"\n",
"\n",
"# Wyliczenie accuracy modelu\n",
"print(x.accuracy(y_test, predictions))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9326599326599326"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f1_score(y_test, predictions, average=\"macro\")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x7fbbad7b1358>"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAEBCAYAAAB7Wx7VAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4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gZfvP0VyjA49ZKTefKr8axYsIqSXaX88M0GFs5cTDh06EggM2Tywzeb2LqmCJsN0kla9g98R2swGlVe6UejsqHxGyhXu8TVJYQNbF67lRv7j6WirKIq0Oc8NY+PX13IkwUTaN62KbcNvL9a2B8Q9AfZ8PWPMCqRVacv2w/LVEqhcu+j5t9tIZQuSlRJQtjG5Jueo7zUd0igh4Nh9hWX8ezd/wbA4625W+jHlTVcjImYsn3gA6iMM8FVw1o/OgjBbxNXkBA2EKgIsGx+YcTRNmbY5LM3vgCgQ5+a1+Px7avNgAoRC/bv0jnAnQ/Br4BQhIMuMBomuiJhc/sDAWavWcWqXTtpnZPLhXndaZaVZXVZMRMOhWs8HgpWHs//ZVdcbifBQPWfPcNh0KSN/OwlStoEvvJehC57mciBb4Ln7ESXJGxsxY4iRr/5OiHTxBcK4nE4eOLLzxl/9iCGdu5qdXkx4c320qpDC35cszXi8QMTtvqe3ZPM3ExKdpVWe47T7aT/RSfEtU7xs7To0gFQzg6Q/Qcgg59H7Tgqv24wAWVkW1ecsJVgOMyVs95gX8CPL1S5zIA/HKYiHOLOee+xZV/14EtV10+8Ao+3+twVj9fNNY+PBsDhdPDwnLvIzPXiyax8rtPlxJ3h4vqJV9C8fZOE1pzO0uYKH8D
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pca = PCA(n_components=2)\n",
"pca.fit(X_test)\n",
"X_pca = pca.transform(X_test)\n",
"\n",
"df_pred = pd.DataFrame(predictions).replace({'Virginica': 0, 'Versicolor': 1, \"Setosa\": 2}, regex=True)\n",
"df_pred = np.array(df_pred).reshape(1, -1)\n",
"plt.scatter(X_pca[:, 0], X_pca[:, 1], c=df_pred[0], s=50, cmap='viridis')"
]
2022-05-17 17:30:50 +02:00
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "naive_bayes.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}