Skip to content

Instantly share code, notes, and snippets.

@mateusfg7
Created November 8, 2019 22:25
Show Gist options
  • Save mateusfg7/74e66e357f38aa86c09f3338e81cf8ff to your computer and use it in GitHub Desktop.
Save mateusfg7/74e66e357f38aa86c09f3338e81cf8ff to your computer and use it in GitHub Desktop.
Introducao_a_machine_learn.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Introducao_a_machine_learn.ipynb",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/mateusfg7/74e66e357f38aa86c09f3338e81cf8ff/introducao_a_machine_learn.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "ykR-APYwRiGd",
"colab_type": "code",
"colab": {}
},
"source": [
"# sabe JS\n",
"# sabe PYTHON\n",
"# sabe Assembly\n",
"# sabe C++\n",
"# sabe Pascal\n",
"\n",
"bom1 = [1, 1, 1, 1, 1]\n",
"bom2 = [1, 1, 0, 1, 0]\n",
"bom3 = [1, 0, 1, 1, 1]\n",
"bom4 = [0, 1, 1, 1, 1]\n",
"\n",
"ruim1 = [0, 0, 0, 0, 0]\n",
"ruim2 = [1, 0, 0, 0, 0]\n",
"ruim3 = [0, 0, 1, 0, 1]\n",
"ruim4 = [0, 0, 0, 1, 0]\n",
"ruim5 = [0, 1, 1, 0, 1]\n",
"\n",
"treino_x = [bom1, bom2, bom3, bom4, ruim1, ruim2, ruim3, ruim4, ruim5]\n",
"treino_y = [1, 1, 1, 1, 0, 0, 0, 0, 0]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "cPMfLLhQl85h",
"colab_type": "code",
"outputId": "4c5381a6-1e58-4a55-c7ac-561a3e693688",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 86
}
},
"source": [
"from sklearn.svm import LinearSVC\n",
"\n",
"modelo = LinearSVC()\n",
"modelo.fit(treino_x, treino_y)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,\n",
" intercept_scaling=1, loss='squared_hinge', max_iter=1000,\n",
" multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,\n",
" verbose=0)"
]
},
"metadata": {
"tags": []
},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Au94o6HzmhLl",
"colab_type": "code",
"outputId": "06552779-dd1c-4f55-b9c7-71c74754b635",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"programador_qualquer0 = [1, 1, 1, 1, 0] # 1 bom\n",
"programador_qualquer1 = [0, 0, 0, 1, 1] # 0 ruim\n",
"programador_qualquer2 = [1, 0, 0, 1, 1] # 1 bom\n",
"programador_qualquer3 = [0, 0, 1, 1, 0] # 0 ruim\n",
"programador_qualquer4 = [1, 0, 1, 1, 0] # 1 bom\n",
"programador_qualquer5 = [0, 1, 0, 0, 1] # 0 ruim\n",
"\n",
"teste_x = [programador_qualquer0, programador_qualquer1, programador_qualquer2, programador_qualquer3, programador_qualquer4, programador_qualquer5]\n",
"teste_y = [1, 0, 1, 0, 1, 0]\n",
"\n",
"previsoes = modelo.predict(teste_x)\n",
"previsoes"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([1, 0, 1, 0, 1, 0])"
]
},
"metadata": {
"tags": []
},
"execution_count": 14
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "xa-251ftom5x",
"colab_type": "code",
"outputId": "b3f6392d-9331-4f8b-da7b-42f42bc78d25",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"from sklearn.metrics import accuracy_score\n",
"\n",
"accuracy_score(teste_y, previsoes) * 100"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"100.0"
]
},
"metadata": {
"tags": []
},
"execution_count": 16
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "u1y4ZBeOtane",
"colab_type": "code",
"outputId": "fa0d4af6-a3ba-4bef-b960-0f295b75d2b0",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
}
},
"source": [
"import pandas as pd\n",
"\n",
"uri = \"https://raw.githubusercontent.com/alura-cursos/machine-learning-introducao-a-classificacao/master/acesso.csv\"\n",
"dados = pd.read_csv(uri)\n",
"dados.head()"
],
"execution_count": 0,
"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>home</th>\n",
" <th>como_funciona</th>\n",
" <th>contato</th>\n",
" <th>comprou</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" home como_funciona contato comprou\n",
"0 1 1 0 0\n",
"1 1 1 0 0\n",
"2 1 1 0 0\n",
"3 1 1 0 0\n",
"4 1 1 0 0"
]
},
"metadata": {
"tags": []
},
"execution_count": 17
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "up4iV5WtuozI",
"colab_type": "code",
"outputId": "b796e13e-85a9-4f64-adc2-bf8342a26ef2",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
}
},
"source": [
"dados.sample(5)"
],
"execution_count": 0,
"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>home</th>\n",
" <th>como_funciona</th>\n",
" <th>contato</th>\n",
" <th>comprou</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>94</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>48</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>90</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>41</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" home como_funciona contato comprou\n",
"94 0 0 1 0\n",
"48 1 1 0 0\n",
"90 1 0 1 1\n",
"0 1 1 0 0\n",
"41 0 0 1 0"
]
},
"metadata": {
"tags": []
},
"execution_count": 18
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "OzSLZ_aou4Ay",
"colab_type": "code",
"outputId": "b53b69f1-0e7b-43a4-e559-002bab518c13",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
}
},
"source": [
"x = dados[[\"home\", \"como_funciona\", \"contato\"]]\n",
"x.head()"
],
"execution_count": 0,
"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>home</th>\n",
" <th>como_funciona</th>\n",
" <th>contato</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" home como_funciona contato\n",
"0 1 1 0\n",
"1 1 1 0\n",
"2 1 1 0\n",
"3 1 1 0\n",
"4 1 1 0"
]
},
"metadata": {
"tags": []
},
"execution_count": 20
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "ETtkipWzvcpp",
"colab_type": "code",
"outputId": "28147556-28c5-443b-ad83-43bafa227ab0",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 121
}
},
"source": [
"y = dados[\"comprou\"]\n",
"y.head()"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0 0\n",
"1 0\n",
"2 0\n",
"3 0\n",
"4 0\n",
"Name: comprou, dtype: int64"
]
},
"metadata": {
"tags": []
},
"execution_count": 25
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "akG_8Gg8voEn",
"colab_type": "code",
"outputId": "2e5086e1-9f17-472e-e85f-d74899f329ea",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"from sklearn.svm import LinearSVC\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"modelo = LinearSVC()\n",
"treino_x, teste_x, treino_y, teste_y = train_test_split(x, y)\n",
"\n",
"modelo.fit(treino_x, treino_y)\n",
"\n",
"previsoes = modelo.predict(teste_x)\n",
"accuracy_score(teste_y, previsoes) * 100"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"92.0"
]
},
"metadata": {
"tags": []
},
"execution_count": 29
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "uVTFDaLtv5tz",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment