# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/BrunoASNascimento/ml_study/blob/master/introduction_ml_project_02.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="Oangp65FQ454" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="493ce295-07c7-4c3b-dfed-969ac3e5c290"
import pandas as pd

url = 'https://gist.githubusercontent.com/guilhermesilveira/2d2efa37d66b6c84a722ea627a897ced/raw/10968b997d885cbded1c92938c7a9912ba41c615/tracking.csv'

dados = pd.read_csv(url)
dados.head(5)

# + id="acDhh1DdRhJ0" colab_type="code" colab={}
x = dados[['home',	'how_it_works',	'contact',	'bought']]
y = dados['bought']

# + id="RzA-n4xwSLkw" colab_type="code" colab={}
treino_x = x[:75]
treino_y = y[:75]
teste_x = x[75:]
teste_y = y[75:]

# + id="F4cpcY6RSnfk" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 85} outputId="7da14bf0-3ac0-4ff7-aee7-d8f04f20b25d"
from sklearn.svm import LinearSVC

modelo = LinearSVC()
modelo.fit(treino_x,treino_y)

# + id="N-kzGOMjT2vU" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} outputId="1913e80e-1294-463d-8561-fbd4f603882e"
previsoes = modelo.predict(teste_x)
print(previsoes)
print(teste_y.values)

# + id="x6wT0gWxUGSJ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="b501ef96-8fa5-4aa8-ba81-508bb2347740"
from sklearn.metrics import accuracy_score

taxa_de_acertos = accuracy_score(teste_y,previsoes)
print(f'Taxa de acertos {round(taxa_de_acertos*100,2)}%')

# + id="-1jrCaHJ6CQ9" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="0e06c2c5-feba-4c32-e967-233d48e7867a"
from sklearn.model_selection import train_test_split    
SEED = 20
treino_x,teste_x ,treino_y,teste_y =train_test_split(x,y, test_size=0.25, random_state=SEED)

modelo = LinearSVC()
modelo.fit(treino_x,treino_y)
previsoes = modelo.predict(teste_x)

taxa_de_acertos = accuracy_score(teste_y,previsoes)
print(f'Taxa de acertos {round(taxa_de_acertos*100,2)}%')

# + id="X10-nI9E7u6M" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="77b2bdb3-df5a-4927-a1c4-50baf7a29414"
print(treino_y.value_counts())

# + id="Ic2xV1Pf7-CR" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="c82dc975-dc1a-48eb-c153-40f804a3a3aa"
print(teste_y.value_counts())

# + id="r_fW_pMl8Kwb" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="cd0b00a6-cf8f-4225-e508-dae88428500c"
from sklearn.model_selection import train_test_split    
SEED = 20
treino_x,teste_x ,treino_y,teste_y =train_test_split(x,y, test_size=0.25, random_state=SEED,stratify=y)

modelo = LinearSVC()
modelo.fit(treino_x,treino_y)
previsoes = modelo.predict(teste_x)

taxa_de_acertos = accuracy_score(teste_y,previsoes)
print(f'Taxa de acertos {round(taxa_de_acertos*100,2)}%')

# + id="OUnONNLW8TsB" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 119} outputId="0c517f86-6859-41cc-9640-43a8675444c6"
print(treino_y.value_counts())
print(teste_y.value_counts())

# + [markdown] id="TBvvNylSW40e" colab_type="text"
# #Segunda parte
#

# + id="cEnpbMEb8WeZ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="bde1f807-ea99-4fc6-f650-7fbeec775f4b"
url = 'https://gist.githubusercontent.com/guilhermesilveira/1b7d5475863c15f484ac495bd70975cf/raw/16aff7a0aee67e7c100a2a48b676a2d2d142f646/projects.csv'

dados = pd.read_csv(url)
dados.head(5)

# + id="-u2A7QTENEB_" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 359} outputId="e0583b35-4d27-4c7e-f1e7-4442fbe84bcf"
change = {
    0:1,
    1:0
}
dados['finished'] = dados['unfinished'].map(change)
dados.head(10)

# + id="-1uz0zdrNjE7" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 351} outputId="467ac221-444a-438b-aa60-35fb76885716"
import seaborn as sns

sns.scatterplot(x='expected_hours',y='price',data=dados)

# + id="XtzdKh2WODBf" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 297} outputId="b3432f08-8c4c-434e-96ce-799802403981"
sns.scatterplot(x='expected_hours',y='price',hue= 'finished',data=dados)

# + id="fF63aeonONJu" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 386} outputId="a08c5144-8e81-4f26-8217-b72ef37de7b2"
sns.relplot(x='expected_hours',y='price',col= 'finished',hue= 'finished',data=dados)

# + id="ofa_5VUoOyqh" colab_type="code" colab={}
x = dados[['expected_hours','price']]
y = dados['finished']

# + id="QY7ujrKwO-5k" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="8477a304-7a64-41fe-ea8b-94dc24de7b5d"
from sklearn.model_selection import train_test_split    
SEED = 5
np.random.seed(SEED)
treino_x,teste_x ,treino_y,teste_y =train_test_split(x,y, test_size=0.25, stratify=y)

modelo = LinearSVC()
modelo.fit(treino_x,treino_y)
previsoes = modelo.predict(teste_x)

taxa_de_acertos = accuracy_score(teste_y,previsoes)
print(f'Taxa de acertos {round(taxa_de_acertos*100,2)}%')

# + id="Tcf9ovKcPOL2" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="5aa3b3b3-62de-4f30-946f-33e66068d43a"
import numpy as np

previsoes_de_base = np.ones(540)
taxa_de_acertos = accuracy_score(teste_y,previsoes_de_base)
print(f'Taxa de acertos baseline {round(taxa_de_acertos*100,2)}%')

# + id="fm5EPaQBU41N" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 297} outputId="5d72ebb0-c1bc-4aa8-eb74-6e84df516f6a"
sns.scatterplot(x='expected_hours',y='price',hue= teste_y,data=teste_x)

# + id="Iae1pss-VNxx" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="1b376ea0-a0a1-448f-aa2f-cd2286ee7278"
x_min = teste_x['expected_hours'].min()
x_max = teste_x['expected_hours'].max()
y_min = teste_x['price'].min()
y_max = teste_x['price'].max()
print(x_min,
x_max,
y_min,
y_max)

# + id="-EkFSapgVrsq" colab_type="code" colab={}
pixels =100
eixo_x = np.arange(x_min, x_max, (x_max-x_min)/pixels)
eixo_y = np.arange(y_min, y_max, (y_max-y_min)/pixels)

# + id="SM699tgtWRQs" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 136} outputId="cab7f80c-ca7f-4cd4-f228-1686f38340cb"
xx , yy = np.meshgrid(eixo_x,eixo_y)
pontos = np.c_[xx.ravel(),yy.ravel()]
pontos

# + id="hnmro6byWxZS" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 136} outputId="619872a5-6f1a-43e2-c8dd-d67cb4d79a92"
z = modelo.predict(pontos)
z = z.reshape(xx.shape)
z

# + [markdown] id="4vadEkpgXtLB" colab_type="text"
# #Decision boundary
#

# + id="-wyzvwC4W_-h" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="13c94474-96d3-495f-c70e-85f9efdc11bf"
import matplotlib.pyplot as plt

plt.contourf(xx,yy,z,alpha=0.3)
plt.scatter(teste_x['expected_hours'],teste_x['price'],c=teste_y,s=1)



# + id="dkA8r5DRJhPx" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="47d31e08-172b-4746-fefb-90101fa1dc34"
from sklearn.model_selection import train_test_split    
from sklearn.svm import SVC

SEED = 5
np.random.seed(SEED)
treino_x,teste_x ,treino_y,teste_y =train_test_split(x,y, test_size=0.25, stratify=y)

modelo = SVC()
modelo.fit(treino_x,treino_y)
previsoes = modelo.predict(teste_x)

taxa_de_acertos = accuracy_score(teste_y,previsoes)
print(f'Taxa de acertos {round(taxa_de_acertos*100,2)}%')

# + id="COBHiD-tJ3Z_" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="77e195fe-3c41-44df-e48f-178ca7d7198e"
x_min = teste_x['expected_hours'].min()
x_max = teste_x['expected_hours'].max()
y_min = teste_x['price'].min()
y_max = teste_x['price'].max()

pixels =100
eixo_x = np.arange(x_min, x_max, (x_max-x_min)/pixels)
eixo_y = np.arange(y_min, y_max, (y_max-y_min)/pixels)

xx , yy = np.meshgrid(eixo_x,eixo_y)
pontos = np.c_[xx.ravel(),yy.ravel()]

z = modelo.predict(pontos)
z = z.reshape(xx.shape)

plt.contourf(xx,yy,z,alpha=0.3)
plt.scatter(teste_x['expected_hours'],teste_x['price'],c=teste_y,s=1)

# + id="0k5odgy1KXkN" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="e5d2571f-0c30-4515-dba1-40ba9c7dd235"
from sklearn.model_selection import train_test_split    
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler

SEED = 5
np.random.seed(SEED)
raw_treino_x,raw_teste_x ,treino_y,teste_y =train_test_split(x,y, test_size=0.25, stratify=y)

scaler = StandardScaler()
scaler.fit(raw_treino_x)
treino_x = scaler.transform(raw_treino_x)
teste_x = scaler.transform(raw_teste_x)

modelo = SVC()
modelo.fit(treino_x,treino_y)
previsoes = modelo.predict(teste_x)

taxa_de_acertos = accuracy_score(teste_y,previsoes)
print(f'Taxa de acertos {round(taxa_de_acertos*100,2)}%')

# + id="7HVhac7jLRUV" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="6ad068e1-013e-4f7c-bf8a-7ed2e82254a7"
data_x = teste_x[:,0]
data_y = teste_x[:,1]

x_min = data_x.min()
x_max = data_x.max()
y_min = data_y.min()
y_max = data_y.max()

pixels =100
eixo_x = np.arange(x_min, x_max, (x_max-x_min)/pixels)
eixo_y = np.arange(y_min, y_max, (y_max-y_min)/pixels)

xx , yy = np.meshgrid(eixo_x,eixo_y)
pontos = np.c_[xx.ravel(),yy.ravel()]

z = modelo.predict(pontos)
z = z.reshape(xx.shape)

plt.contourf(xx,yy,z,alpha=0.3)
plt.scatter(data_x,data_y,c=teste_y,s=1)

# + [markdown] id="Kq0akc2UM8FL" colab_type="text"
# #Terceira parte

# + id="fu1uJcvsM7B1" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="5c9a00af-a2c0-49d6-9c3a-bf4f75453e7a"
url = 'https://gist.githubusercontent.com/guilhermesilveira/4d1d4a16ccbf6ea4e0a64a38a24ec884/raw/afd05cb0c796d18f3f5a6537053ded308ba94bf7/car-prices.csv'

dados = pd.read_csv(url)
dados.head(5)

# + id="GWCIub11NzM2" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 359} outputId="97cf3b03-bf77-4cfd-8608-9d89a9ab4633"
change = {
    'yes':1,
    'no':0
}
dados['sold'] = dados['sold'].map(change)
dados.head(10)

# + id="7Z1vM0WiOM9F" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 359} outputId="072b2be6-00f0-4297-d25a-ca0c0f00b1b5"
from datetime import datetime
dados['age'] = datetime.today().year - dados['model_year']
dados['km_per_year'] = dados['mileage_per_year'] * 1.60934
dados = dados.drop(columns=['Unnamed: 0',	'mileage_per_year',	'model_year'])
dados.head(10)

# + id="GzbLaak_PiW4" colab_type="code" colab={}
x = dados[['price'	,	'age',	'km_per_year']]
y = dados['sold']

# + id="BubdtrQ9P-m9" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="3fca6d6a-c6e8-48b2-b166-95fdd344943a"
from sklearn.model_selection import train_test_split    
SEED = 5
np.random.seed(SEED)
treino_x,teste_x ,treino_y,teste_y =train_test_split(x,y, test_size=0.25, stratify=y)

modelo = LinearSVC()
modelo.fit(treino_x,treino_y)
previsoes = modelo.predict(teste_x)

taxa_de_acertos = accuracy_score(teste_y,previsoes)
print(f'Taxa de acertos {round(taxa_de_acertos*100,2)}%')

# + id="57f7s2M2QiEE" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="6fbfd079-de89-4c16-9ac2-be10488841a6"
from sklearn.dummy import DummyClassifier
np.random.seed(SEED)
dummy_stratified = DummyClassifier(strategy='stratified')
dummy_stratified.fit(treino_x,treino_y)
taxa_de_acertos = dummy_stratified.score(teste_x,teste_y)
print(f'Taxa de acertos dummy stratified {round(taxa_de_acertos*100,2)}%')

# + id="xpvrvvdZRVEN" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="bbf672c6-a4d4-47bf-f59c-6e7ed60075ef"
from sklearn.dummy import DummyClassifier
np.random.seed(SEED)
dummy_mostfrequent = DummyClassifier(strategy='most_frequent')
dummy_mostfrequent.fit(treino_x,treino_y)
taxa_de_acertos = dummy_mostfrequent.score(teste_x,teste_y)
print(f'Taxa de acertos dummy most frequent {round(taxa_de_acertos*100,2)}%')

# + id="6UbOe_ICSyEZ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="85101bf8-4336-4705-8d57-814ef0b480b5"
SEED = 5
np.random.seed(SEED)
raw_treino_x,raw_teste_x ,treino_y,teste_y =train_test_split(x,y, test_size=0.25, stratify=y)

scaler = StandardScaler()
scaler.fit(raw_treino_x)
treino_x = scaler.transform(raw_treino_x)
teste_x = scaler.transform(raw_teste_x)

modelo = SVC()
modelo.fit(treino_x,treino_y)
previsoes = modelo.predict(teste_x)

taxa_de_acertos = accuracy_score(teste_y,previsoes)
print(f'Taxa de acertos {round(taxa_de_acertos*100,2)}%')

# + id="Uo2p4__qUW0E" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="3cace667-de94-444d-abc0-03a92e04a54f"
from sklearn.tree import DecisionTreeClassifier

SEED = 5
np.random.seed(SEED)
treino_x,teste_x ,treino_y,teste_y =train_test_split(x,y, test_size=0.25, stratify=y)



modelo = DecisionTreeClassifier(max_depth=3)
modelo.fit(treino_x,treino_y)
previsoes = modelo.predict(teste_x)

taxa_de_acertos = accuracy_score(teste_y,previsoes)
print(f'Taxa de acertos {round(taxa_de_acertos*100,2)}%')

# + id="NLUdnwfFUtfX" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 598} outputId="ba41faba-d97d-4f42-a974-94a975bdde27"
from sklearn.tree import export_graphviz
import graphviz

features = x.columns
dot_data = export_graphviz(modelo,out_file=None,feature_names=features,filled=True,class_names=['no','yes'])
graph = graphviz.Source(dot_data)
graph

StarcoderdataJupyterScriptsDedupFiltered