Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: exemplo utilizando pandas e notebook python #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 249 additions & 0 deletions capitulo1/classificacao.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "4fa2361b-cc5c-4bc9-83fb-2eec6d09ec3b",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.naive_bayes import MultinomialNB\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "87b294f2-3435-4664-bcd7-2de2647899f7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 1\n",
"1 1\n",
"2 1\n",
"3 -1\n",
"4 -1\n",
"5 -1\n",
"Name: target, dtype: int64"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pig1 = [1, 1, 0]\n",
"pig2 = [1, 1, 0]\n",
"pig3 = [1, 1, 0]\n",
"dog4 = [1, 1, 1]\n",
"dog5 = [0, 1, 1]\n",
"dog6 = [0, 1, 1]\n",
"\n",
"index = [pig1, pig2, pig3, dog4, dog5, dog6]\n",
"target = [1 , 1, 1, -1, -1, -1]\n",
"\n",
"\n",
"\n",
"df = pd.DataFrame(np.array(index), columns=['Gordo', 'Perna curta', 'faz auau'])\n",
"df['target'] = target\n",
"df.iloc[:,3]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ba8b53c1-f479-402d-9d5b-09204bc194ac",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>MultinomialNB()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">MultinomialNB</label><div class=\"sk-toggleable__content\"><pre>MultinomialNB()</pre></div></div></div></div></div>"
],
"text/plain": [
"MultinomialNB()"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model = MultinomialNB()\n",
"model.fit(df.iloc[:,:3].values, df.iloc[:,3].values)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5b1acc20-9419-419f-a321-817fbed7581c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 1, 1],\n",
" [1, 0, 0],\n",
" [0, 0, 1]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1 = [1, 1, 1]\n",
"x2 = [1, 0, 0]\n",
"x3 = [0, 0 ,1]\n",
"\n",
"y = [-1, 1, 1]\n",
"\n",
"\n",
"df_teste = pd.DataFrame(np.array([x1, x2, x3]), columns=['Gordo', 'Perna Curta', 'faz auau'])\n",
"df_teste['target'] = y\n",
"df_teste.iloc[:,:3].values"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6106f01c-15ae-4912-a506-0067eaac0fed",
"metadata": {},
"outputs": [],
"source": [
"result = model.predict(df_teste.iloc[:,:3].values)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "3e582814-d110-4022-94e2-b6612222dffe",
"metadata": {},
"outputs": [],
"source": [
"diff = result - y"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "a8e9e9fc-349c-42c4-9cf0-27545dee0276",
"metadata": {},
"outputs": [],
"source": [
"hits = [d for d in diff if d ==0]\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f5547cde-63b9-4165-b7b0-6b6464f640d3",
"metadata": {},
"outputs": [],
"source": [
"total_hits = len(hits)\n",
"total_element = len(y)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e9a59628-a40c-4c50-8092-e8892fb840dc",
"metadata": {},
"outputs": [],
"source": [
"rate_hits = 100 * total_hits / total_element"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "a400ab7e-460c-4876-913a-cb15c2a0d537",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-1, 1, -1])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "fdb9e720-9c98-4784-882c-fd0cc8d6c47d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 0, -2])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"diff"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "70edb849-8ad7-4dfa-96bc-10072fd4f737",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"66.66666666666667"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rate_hits"
]
}
],
"metadata": {
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}