<jupyter_start><jupyter_text>Regressão Logistica <NAME>, <EMAIL>.Universidade de São Paulo, São Carlos, Brasil.https://sites.icmc.usp.br/francisco Copyright: Creative Commons  Vamos realizar a classificação em um conjunto de dados artificiais.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
from scipy.special import expit

X = np.arange(12).reshape(-1, 1)
y = np.array([0, 0, 0, 0,1,0, 1, 1, 1, 1, 1, 1])
plt.plot(X, y, 'ko')

# Ajusta o modelo de regressao logistica
model = linear_model.LogisticRegression(C=1e5)
model.fit(X, y)
loss = expit(X * model.coef_ + model.intercept_).ravel()
plt.plot(X, loss, color='red', linewidth=3)

# Podemos obter uma curva mais suave
#xx = np.arange(min(X), max(X), 0.01)
#loss = expit(xx * model.coef_ + model.intercept_).ravel()
#plt.plot(xx, loss, color='red', linewidth=3)

# Ajusta um modelo de regressao linear
linear = linear_model.LinearRegression()
linear.fit(X, y)
plt.plot(X, linear.coef_ * X + linear.intercept_, linewidth=2, linestyle='dashed')
plt.axhline(.5, color='.5')
plt.savefig('logistic.eps')
plt.show(True)
p = model.predict_proba(X)
print(p)
print("Acurácia:", model.score(X, y))<jupyter_output>Acurácia: 0.8333333333333334<jupyter_text>O modelo de regressão logística usa a função logística.<jupyter_code>import matplotlib.pyplot as plt 
import numpy as np 
import math 
  
x = np.linspace(-10, 10, 100) 
z = 1/(1 + np.exp(-x)) 
  
plt.figure(figsize=(6,4))
plt.plot(x, z) 
plt.xlabel("x", fontsize = 15) 
plt.ylabel("h(x)", fontsize = 15) 
plt.savefig('logistic-function.eps')
plt.show()<jupyter_output><empty_output><jupyter_text>Usamos o código abaixo para fazer a classificação completa.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
from scipy.special import expit

X = np.arange(12).reshape(-1, 1)
y = np.array([0, 0, 0, 0,1,0, 1, 1, 1, 1, 1, 1])
plt.plot(X, y, 'ko')

# Ajusta o modelo de regressao logistica
model = linear_model.LogisticRegression(C=1e5)
model.fit(X, y)
loss = expit(X * model.coef_ + model.intercept_).ravel()
plt.plot(X, loss, color='red', linewidth=3)

# Para comparar, ajustamos um modelo de regressao linear
linear = linear_model.LinearRegression()
linear.fit(X, y)
plt.plot(X, linear.coef_ * X + linear.intercept_, linewidth=2, linestyle='dashed')
plt.axhline(.5, color='.5')
plt.show(True)

p = model.predict_proba(X)
print("Acurácia:", model.score(X, y))<jupyter_output><empty_output><jupyter_text>Classificação de dados  Vamos inicialmente ler os dados.<jupyter_code>import random
random.seed(42) # define the seed (important to reproduce the results)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = pd.read_csv('data/vertebralcolumn-3C.csv', header=(0))
#data = pd.read_csv('data/winequality-red.csv', header=(0))

data = data.dropna(axis='rows') #remove NaN
# armazena os nomes das classes
classes = np.array(pd.unique(data[data.columns[-1]]), dtype=str)  

print("Número de linhas e colunas na matriz de atributos:", data.shape)
attributes = list(data.columns)
data.head(10)<jupyter_output>Número de linhas e colunas na matriz de atributos: (310, 7)<jupyter_text>Convertendo para o formato Numpy e normalizando:<jupyter_code>data = data.to_numpy()
nrow,ncol = data.shape
y = data[:,-1]
X = data[:,0:ncol-1]<jupyter_output><empty_output><jupyter_text>Selecionamos os conjunto de treinamento e teste usando o método hold-out.<jupyter_code>from sklearn.model_selection import train_test_split
p = 0.2 # fraction of elements in the test set
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = p, random_state = 42)<jupyter_output><empty_output><jupyter_text>Realizando a classificação por regressão logística:<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn import metrics

model = LogisticRegression(solver = 'lbfgs', multi_class = 'auto', max_iter = 1000)
model.fit(x_train, y_train)

y_pred = model.predict(x_test)
print('Acurácia: ', model.score(x_test, y_test))<jupyter_output>Acurácia:  0.8709677419354839<jupyter_text>Código completo:<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.model_selection import train_test_split

#data = pd.read_csv('data/vertebralcolumn-3C.csv', header=(0))
#data = pd.read_csv('data/winequality-red.csv', header=(0))
data = pd.read_csv('data/iris.csv', header=(0))

data = data.dropna(axis='rows') #remove NaN
classes = np.array(pd.unique(data[data.columns[-1]]), dtype=str)  

data = data.to_numpy()
nrow,ncol = data.shape
y = data[:,-1]
X = data[:,0:ncol-1]

p = 0.8 # fracao de elementos no conjunto de treinamento
x_train, x_test, y_train, y_test = train_test_split(X, y, train_size = p)

model = LogisticRegression(solver = 'lbfgs', multi_class = 'auto', max_iter = 1000)
model.fit(x_train, y_train)

y_pred = model.predict(x_test)
print('Acurácia: ', model.score(x_test, y_test))
classes<jupyter_output><empty_output><jupyter_text>Exercício: implemmente a classificação usando validação cruzada.<jupyter_code>import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score, KFold

df = pd.read_csv('data/iris.csv', header=(0))
df.head()
df.shape
df.isna().sum()
target = 'species'
X = df.drop(target, axis=1)
y = df[target]
model = LogisticRegression(random_state=999, solver='lbfgs', max_iter = 1000)
cv = KFold(n_splits=10)
scores = cross_val_score(model, X, y, cv=cv)
scores<jupyter_output><empty_output><jupyter_text>Região de separação Para graficar a região de separação, precisamos instalar a bibliteca mlxtend: http://rasbt.github.io/mlxtend/installation/Pode ser usado: conda install -c conda-forge mlxtend Uma propriedade importante da regressão logística é que a superfície de separação são hiperplanos (retas em duas dimensões, ou planos, em duas dimensões).<jupyter_code>from mlxtend.plotting import plot_decision_regions
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
import sklearn.datasets as skdata
from matplotlib import pyplot
from pandas import DataFrame

# Gera os dados em duas dimensões
n_samples = 100 # número de observações
# centro dos grupos
#centers = [(0, 0), (-2, -2), (2,0)]
centers = [(0, 0), (-2, -2)]
X, y = skdata.make_blobs(n_samples=100, n_features=2, cluster_std=1.0, centers=centers, 
                         shuffle=False, random_state=42)

# monta a matrix de atributos
d = np.column_stack((X,np.transpose(y)))
# converte para o formato dataframe do Pandas
data = DataFrame(data = d, columns=['X1', 'X2', 'y'])
features_names = ['X1', 'X2']
class_labels = np.unique(y)

# mostra os dados e colori de acordo com as classes
colors = ['red', 'blue', 'green', 'black']
aux = 0
for c in class_labels:
    ind = np.where(y == c)
    plt.scatter(X[ind,0][0], X[ind,1][0], color = colors[aux], label = c)
    aux = aux + 1
plt.savefig('plot_ex.eps')
plt.legend()
plt.show()

# Training a classifier
model = LogisticRegression(solver = 'lbfgs', multi_class = 'auto', max_iter = 1000)
model.fit(X, y)
# Plotting decision regions
plot_decision_regions(X, y, clf=model, legend=2)
plt.xlabel('X1')
plt.ylabel('X2')
#plt.savefig('logistic-region.eps')
plt.show()<jupyter_output><empty_output><jupyter_text>Para três classes:<jupyter_code>from mlxtend.plotting import plot_decision_regions
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
import sklearn.datasets as skdata
from matplotlib import pyplot
from pandas import DataFrame

# Gera os dados em duas dimensões
n_samples = 100 # número de observações
# centro dos grupos
centers = [(0, 0), (-2, -2), (2,0)]
X, y = skdata.make_blobs(n_samples=100, n_features=2, cluster_std=1.0, centers=centers, 
                         shuffle=False, random_state=42)

# monta a matrix de atributos
d = np.column_stack((X,np.transpose(y)))
# converte para o formato dataframe do Pandas
data = DataFrame(data = d, columns=['X1', 'X2', 'y'])
features_names = ['X1', 'X2']
class_labels = np.unique(y)

# mostra os dados e colori de acordo com as classes
colors = ['red', 'blue', 'green', 'black']
aux = 0
for c in class_labels:
    ind = np.where(y == c)
    plt.scatter(X[ind,0][0], X[ind,1][0], color = colors[aux], label = c)
    aux = aux + 1
plt.savefig('plot_ex.eps')
plt.legend()
plt.show()

# Training a classifier
model = LogisticRegression(solver = 'lbfgs', multi_class = 'auto', max_iter = 1000)
model.fit(X, y)
# Plotting decision regions
plot_decision_regions(X, y, clf=model, legend=2)
plt.xlabel('X1')
plt.ylabel('X2')
#plt.savefig('logistic-region.eps')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13220338983050847, 'fraction_numerical': 0.024519774011299435, 'mean_word_length': 5.720577069096431, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '244409', 'license': 'CC0-1.0', 'n_tokens_mistral': 3519, 'n_tokens_neox': 3329, 'n_words': 840, 'path': '0Aula6-Classificao-Regressao-logistica.ipynb', 'repo_name': 'AlvaroJoseLopes/ciencia-de-dados', 'size': '8887'}