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

# # Tratamento de dados 

import pandas as pd
uri = "https://gist.githubusercontent.com/guilhermesilveira/1b7d5475863c15f484ac495bd70975cf/raw/16aff7a0aee67e7c100a2a48b676a2d2d142f646/projects.csv"
data = pd.read_csv(uri)
data.head()

# # Renomeando colunas

to_rename = {
    'expected_hours' : 'horas_esperadas',
    'price' : 'preco',
    'unfinished' : 'nao_finalizado'
}
data = data.rename(columns = to_rename)
data.head()

# # Usando swapper de valores pra criar coluna mais coerente

swapper = {
    0:1,
    1:0
}
data['finalizado'] = data.nao_finalizado.map(swapper)
data.head()

data.tail()

# # Usando Seaborn pra plotar e comparar resultados

import seaborn as sns
sns.scatterplot(x="horas_esperadas", y="preco", data=data)

sns.scatterplot(x="horas_esperadas", y="preco", hue='finalizado', data=data)

sns.relplot(x="horas_esperadas", y="preco", hue='finalizado', col='finalizado', data=data)

#  # Reaproveitando treinamento do notebook anterior

x = data[['horas_esperadas', 'preco']]
y = data['finalizado']

# +
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from sklearn.metrics import accuracy_score
import numpy as np

SEED = 4
np.random.seed(SEED)
train_x, test_x, train_y, test_y = train_test_split(x , y, test_size = 0.25, stratify = y)

print(f'Número de elementos: train_X - {len(train_x)}, train_Y - {len(train_y)}  test_X - {len(test_x)}, test_Y - {len(test_y)}')
print()
model = LinearSVC()
model.fit(train_x, train_y)

predict = model.predict(test_x)
acc = accuracy_score(test_y,predict) * 100
print(f'A precisão foi de {round(acc,2)}%')
# -

# # A importância de uma baseline

linha_base = np.ones(540)

acc = accuracy_score(test_y,linha_base) * 100
print(f'A precisão  da baseline foi de {round(acc,2)}%')

sns.scatterplot(x="horas_esperadas", y="preco", hue=test_y, data=test_x)

x_min = test_x.horas_esperadas.min()
x_max = test_x.horas_esperadas.max()
y_min = test_x.preco.min()
y_max = test_x.preco.max()
print(x_min, x_max, y_min, y_max)

pixels = 100
new_x = np.arange(x_min, x_max, (x_max - x_min) / pixels)
new_y = np.arange(y_min, y_max, (y_max - y_min) / pixels)

xx, yy  = np.meshgrid(new_x, new_y)
xx.ravel()[:110] #concatena os cara

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

Z = model.predict(pontos)
Z = Z.reshape(xx.shape)
print(Z.reshape(xx.shape))

# # Decision Boundary - A borda de decisão do algoritmo

import matplotlib.pyplot as plt
plt.contourf(xx, yy, Z, alpha = 0.3)
plt.scatter(test_x.horas_esperadas, test_x.preco, c=test_y, s=5)

# # Usando o SVC

# +
x = data[['horas_esperadas', 'preco']]
y = data['finalizado']
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
import numpy as np

SEED = 10
np.random.seed(SEED)
train_x, test_x, train_y, test_y = train_test_split(x , y, test_size = 0.25, stratify = y)

print(f'Número de elementos: train_X - {len(train_x)}, train_Y - {len(train_y)}  test_X - {len(test_x)}, test_Y - {len(test_y)}')
print()
model = SVC()
model.fit(train_x, train_y)

predict = model.predict(test_x)
acc = accuracy_score(test_y,predict) * 100
print(f'A precisão foi de {round(acc,2)}%')
linha_base = np.ones(540)
acc = accuracy_score(test_y,linha_base) * 100
print(f'A precisão  da baseline foi de {round(acc,2)}%')

sns.scatterplot(x="horas_esperadas", y="preco", hue=test_y, data=test_x)

x_min = test_x.horas_esperadas.min()
x_max = test_x.horas_esperadas.max()
y_min = test_x.preco.min()
y_max = test_x.preco.max()

pixels = 100
new_x = np.arange(x_min, x_max, (x_max - x_min) / pixels)
new_y = np.arange(y_min, y_max, (y_max - y_min) / pixels)
xx, yy  = np.meshgrid(new_x, new_y)
xx.ravel()[:110] #concatena os cara
pontos = np.c_[xx.ravel(), yy.ravel()]

Z = model.predict(pontos)
Z = Z.reshape(xx.shape)
print(Z.reshape(xx.shape))

import matplotlib.pyplot as plt
plt.contourf(xx, yy, Z, alpha = 0.3)
plt.scatter(test_x.horas_esperadas, test_x.preco, c=test_y, s=5)
# -

# # Usando scaler

# +
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
import numpy as np

SEED = 20
np.random.seed(SEED)
raw_train_x, raw_test_x, train_y, test_y = train_test_split(x , y, test_size = 0.25, stratify = y)

print(f'Número de elementos: train_X - {len(train_x)}, train_Y - {len(train_y)}  test_X - {len(test_x)}, test_Y - {len(test_y)}')
print()
model = SVC()
scaler = StandardScaler()
scaler.fit(raw_train_x)
train_x = scaler.transform(raw_train_x)
test_x = scaler.transform(raw_test_x)
model.fit(train_x, train_y)

predict = model.predict(test_x)
acc = accuracy_score(test_y,predict) * 100
print(f'A precisão foi de {round(acc,2)}%')
linha_base = np.ones(540)
acc = accuracy_score(test_y,linha_base) * 100
print(f'A precisão  da baseline foi de {round(acc,2)}%')

data_x = test_x[:,0]
data_y = test_x[:,1]
pixels = 100

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

new_x = np.arange(x_min, x_max, (x_max - x_min) / pixels)
new_y = np.arange(y_min, y_max, (y_max - y_min) / pixels)
xx, yy  = np.meshgrid(new_x, new_y)
pontos = np.c_[xx.ravel(), yy.ravel()]
Z = model.predict(pontos)
Z = Z.reshape(xx.shape)
print(Z.reshape(xx.shape))
plt.contourf(xx, yy, Z, alpha = 0.3)
plt.scatter(data_x, data_y, c=test_y, s=5)
# -




StarcoderdataJupyterScriptsDedupFiltered