Last active
June 24, 2021 01:18
-
-
Save H4ad/e4c5f79849eebdec32b655d7bba6be0a to your computer and use it in GitHub Desktop.
Sub - Inteligencia Computacional
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
precipitacao | temperatura | umidade | vento | jogar | |
---|---|---|---|---|---|
90 | 30 | 85 | 30 | 0 | |
80 | 21 | 80 | 12 | 0 | |
40 | 20 | 75 | 7 | 1 | |
30 | 18 | 70 | 8 | 0 | |
20 | 32 | 60 | 8 | 1 | |
60 | 34 | 60 | 15 | 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Sub - Inteligência Computacional.ipynb", | |
"provenance": [], | |
"collapsed_sections": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "yWRGaEUvlrTz" | |
}, | |
"source": [ | |
"# Sub - Inteligência Computacional\n", | |
"\n", | |
"Vinícius Lourenço Claro Cardoso - 180618" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "CqhefAQolsm6" | |
}, | |
"source": [ | |
"# KNN" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "pP_VVmmtcbJB" | |
}, | |
"source": [ | |
"#Leitura dos módulos\n", | |
"from __future__ import print_function\n", | |
"\n", | |
"import pandas as pd\n", | |
"from pandas import Series,DataFrame\n", | |
"\n", | |
"from sklearn import svm\n", | |
"from sklearn.model_selection import train_test_split\n", | |
"from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\n", | |
"\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"\n", | |
"np.random.seed(1000)\n", | |
"\n", | |
"import seaborn as sns\n", | |
"import matplotlib.pyplot as plt\n", | |
"\n", | |
"import keras\n", | |
"from keras.datasets import fashion_mnist\n", | |
"from keras.models import Sequential\n", | |
"from keras.layers import Dense, Dropout\n", | |
"from keras.optimizers import RMSprop\n", | |
"from keras.optimizers import Adam\n", | |
"\n", | |
"from sklearn.neighbors import KNeighborsClassifier" | |
], | |
"execution_count": 74, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "tau7PnaccZmj" | |
}, | |
"source": [ | |
"data_set = pd.read_csv(\"https://gist.githubusercontent.com/H4ad/e4c5f79849eebdec32b655d7bba6be0a/raw/c721e3a88157743576f9ade9f3cfa15ea86164e0/sub.csv\",sep=\",\",header=0)" | |
], | |
"execution_count": 75, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 484 | |
}, | |
"id": "e9FwpNRdcz0B", | |
"outputId": "c8c5021e-eadd-424f-ec3c-996c860fab72" | |
}, | |
"source": [ | |
"display(data_set.head(n=5))\n", | |
"display(data_set.describe())" | |
], | |
"execution_count": 76, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"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>a1</th>\n", | |
" <th>a2</th>\n", | |
" <th>a3</th>\n", | |
" <th>a4</th>\n", | |
" <th>classe</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" <td>2</td>\n", | |
" <td>9</td>\n", | |
" <td>A</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" <td>3</td>\n", | |
" <td>0</td>\n", | |
" <td>B</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1</td>\n", | |
" <td>1</td>\n", | |
" <td>4</td>\n", | |
" <td>9</td>\n", | |
" <td>C</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>0</td>\n", | |
" <td>2</td>\n", | |
" <td>2</td>\n", | |
" <td>0</td>\n", | |
" <td>B</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>1</td>\n", | |
" <td>2</td>\n", | |
" <td>3</td>\n", | |
" <td>9</td>\n", | |
" <td>C</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" a1 a2 a3 a4 classe\n", | |
"0 1 0 2 9 A\n", | |
"1 0 1 3 0 B\n", | |
"2 1 1 4 9 C\n", | |
"3 0 2 2 0 B\n", | |
"4 1 2 3 9 C" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
} | |
}, | |
{ | |
"output_type": "display_data", | |
"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>a1</th>\n", | |
" <th>a2</th>\n", | |
" <th>a3</th>\n", | |
" <th>a4</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>count</th>\n", | |
" <td>5.000000</td>\n", | |
" <td>5.00000</td>\n", | |
" <td>5.00000</td>\n", | |
" <td>5.000000</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>mean</th>\n", | |
" <td>0.600000</td>\n", | |
" <td>1.20000</td>\n", | |
" <td>2.80000</td>\n", | |
" <td>5.400000</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>std</th>\n", | |
" <td>0.547723</td>\n", | |
" <td>0.83666</td>\n", | |
" <td>0.83666</td>\n", | |
" <td>4.929503</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>min</th>\n", | |
" <td>0.000000</td>\n", | |
" <td>0.00000</td>\n", | |
" <td>2.00000</td>\n", | |
" <td>0.000000</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25%</th>\n", | |
" <td>0.000000</td>\n", | |
" <td>1.00000</td>\n", | |
" <td>2.00000</td>\n", | |
" <td>0.000000</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>50%</th>\n", | |
" <td>1.000000</td>\n", | |
" <td>1.00000</td>\n", | |
" <td>3.00000</td>\n", | |
" <td>9.000000</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>75%</th>\n", | |
" <td>1.000000</td>\n", | |
" <td>2.00000</td>\n", | |
" <td>3.00000</td>\n", | |
" <td>9.000000</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>max</th>\n", | |
" <td>1.000000</td>\n", | |
" <td>2.00000</td>\n", | |
" <td>4.00000</td>\n", | |
" <td>9.000000</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" a1 a2 a3 a4\n", | |
"count 5.000000 5.00000 5.00000 5.000000\n", | |
"mean 0.600000 1.20000 2.80000 5.400000\n", | |
"std 0.547723 0.83666 0.83666 4.929503\n", | |
"min 0.000000 0.00000 2.00000 0.000000\n", | |
"25% 0.000000 1.00000 2.00000 0.000000\n", | |
"50% 1.000000 1.00000 3.00000 9.000000\n", | |
"75% 1.000000 2.00000 3.00000 9.000000\n", | |
"max 1.000000 2.00000 4.00000 9.000000" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "UNMRXadccTzs" | |
}, | |
"source": [ | |
"x = DataFrame(data_set.drop(\"classe\",axis=1))\n", | |
"y = DataFrame(data_set[\"classe\"])" | |
], | |
"execution_count": 77, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "dbjY7Rjfb1a1", | |
"outputId": "61856671-6c52-4237-e5db-69654fd72411" | |
}, | |
"source": [ | |
"knn = KNeighborsClassifier(n_neighbors=2)\n", | |
"knn.fit(x, y)\n", | |
"# Como há pouco dado, eu basicamente pulei a parte de dados de treino e teste,\n", | |
"# e passei direto para testar com o valor que foi pedido na sub." | |
], | |
"execution_count": 78, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:2: 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", | |
" \n" | |
], | |
"name": "stderr" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n", | |
" metric_params=None, n_jobs=None, n_neighbors=2, p=2,\n", | |
" weights='uniform')" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 78 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "eVWDEoCheye8", | |
"outputId": "db3dd290-47ef-46e2-bd0c-9c63664cfb3d" | |
}, | |
"source": [ | |
"# Cria um dataset para a sub\n", | |
"x_sub_test = [[1,2.5,3,8]]\n", | |
"sub_dataset = pd.DataFrame(data, columns = ['a1', 'a2', 'a3', 'a4'])\n", | |
"\n", | |
"print(f\"A classe para os valores x = [1 2,5 3 8]: {knn.predict(df)}\\n\")\n" | |
], | |
"execution_count": 79, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"A classe para os valores x = [1 2,5 3 8]: ['C']\n", | |
"\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "pBcqGYRPluEB" | |
}, | |
"source": [ | |
"# Naive Bayes" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 204 | |
}, | |
"id": "6N90QdYwhnVJ", | |
"outputId": "fd9eb6f8-a833-40d0-88d7-2bac91967669" | |
}, | |
"source": [ | |
"bayes_set = pd.read_csv(\"https://gist.githubusercontent.com/H4ad/e4c5f79849eebdec32b655d7bba6be0a/raw/822964d28ce98bcc555f50ee0f0626b4b5409b8a/bayes_data.csv\",sep=\",\",header=0)\n", | |
"\n", | |
"bayes_set.head(5)" | |
], | |
"execution_count": 80, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"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>precipitacao</th>\n", | |
" <th>temperatura</th>\n", | |
" <th>umidade</th>\n", | |
" <th>vento</th>\n", | |
" <th>jogar</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>90</td>\n", | |
" <td>30</td>\n", | |
" <td>85</td>\n", | |
" <td>30</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>80</td>\n", | |
" <td>21</td>\n", | |
" <td>80</td>\n", | |
" <td>12</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>40</td>\n", | |
" <td>20</td>\n", | |
" <td>75</td>\n", | |
" <td>7</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>30</td>\n", | |
" <td>18</td>\n", | |
" <td>70</td>\n", | |
" <td>8</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>20</td>\n", | |
" <td>32</td>\n", | |
" <td>60</td>\n", | |
" <td>8</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" precipitacao temperatura umidade vento jogar\n", | |
"0 90 30 85 30 0\n", | |
"1 80 21 80 12 0\n", | |
"2 40 20 75 7 1\n", | |
"3 30 18 70 8 0\n", | |
"4 20 32 60 8 1" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 80 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "LnnUglOMh97I" | |
}, | |
"source": [ | |
"xBayes = DataFrame(bayes_set.drop(\"jogar\",axis=1))\n", | |
"yBayes = DataFrame(bayes_set[\"jogar\"])" | |
], | |
"execution_count": 81, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "Wdo19GXGh7T9", | |
"outputId": "5d2d8d9b-d710-4614-e549-e7067606c423" | |
}, | |
"source": [ | |
"from sklearn.naive_bayes import GaussianNB\n", | |
"from sklearn.naive_bayes import MultinomialNB\n", | |
"\n", | |
"modelGaussian = GaussianNB()\n", | |
"modelGaussian.fit(xBayes, yBayes)\n", | |
"\n", | |
"modelMultinomial = MultinomialNB()\n", | |
"modelMultinomial.fit(xBayes, yBayes)" | |
], | |
"execution_count": 82, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"/usr/local/lib/python3.7/dist-packages/sklearn/naive_bayes.py:206: 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", | |
"/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py:760: 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" | |
], | |
"name": "stderr" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 82 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "4OKSKJpRiIPQ", | |
"outputId": "963d02a3-ae94-4092-b558-130d3ff5ad23" | |
}, | |
"source": [ | |
"bayesTest = [[70, 20, 75, 16]]\n", | |
"\n", | |
"gaussianBayesPred = modelGaussian.predict(bayesTest)\n", | |
"multinomialBayesPred = modelMultinomial.predict(bayesTest)\n", | |
"\n", | |
"print(\"0 = não, 1 = sim\\n\")\n", | |
"print(f\"[Gaussian] A probabilidade para os valores x = [70, 20, 75, 16]: {gaussianBayesPred}\")\n", | |
"print(f\"[Multinomial] A probabilidade para os valores x = [70, 20, 75, 16]: {multinomialBayesPred}\")" | |
], | |
"execution_count": 83, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"0 = não, 1 = sim\n", | |
"\n", | |
"[Gaussian] A probabilidade para os valores x = [70, 20, 75, 16]: [0]\n", | |
"[Multinomial] A probabilidade para os valores x = [70, 20, 75, 16]: [0]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a1 | a2 | a3 | a4 | classe | |
---|---|---|---|---|---|
1 | 0 | 2 | 9 | A | |
0 | 1 | 3 | 0 | B | |
1 | 1 | 4 | 9 | C | |
0 | 2 | 2 | 0 | B | |
1 | 2 | 3 | 9 | C |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment