/
teste_regressao-logistica_poly-features.py
144 lines (100 loc) · 3.75 KB
/
teste_regressao-logistica_poly-features.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
"""
Created on Sat May 4 12:20:08 2019
@author: Welberth
"""
from sklearn.datasets import make_moons
import numpy as np
from matplotlib import pyplot as plt
from pandas import DataFrame
from sklearn.linear_model import LogisticRegression
#import seaborn as sns
from sklearn.preprocessing import PolynomialFeatures
#from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
#sns.set(style="white")
#------------------ gerando dados
X, y = make_moons(n_samples=10000, noise=0.2)
df = DataFrame(dict(x=X[:,0], y=X[:,1], label=y))
# scatter plot, dots colored by class value
colors = {0:'red', 1:'blue'}
fig, ax = plt.subplots()
grouped = df.groupby('label')
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key],s=1)
plt.show()
#------------------ regressão logistica
log_reg = LogisticRegression()
log_reg.fit(df.drop('label',axis=1), df['label'])
# AUC
probs = log_reg.predict_proba(df.drop('label',axis=1))
probs = probs[:, 1]
roc_auc_score(df['label'], probs)
#----------------- ploting decision boundary
#creating grid
xx, yy = np.mgrid[-3:3:.01, -3:3:.01]
grid = np.c_[xx.ravel(), yy.ravel()]
probs = log_reg.predict_proba(grid)[:, 1].reshape(xx.shape)
#ploting linear decision boundary
f, ax = plt.subplots(figsize=(8, 6))
ax.contour(xx, yy, probs, levels=[.5], cmap="Greys", vmin=0, vmax=.6)
ax.scatter(X[100:,0], X[100:, 1], c=y[100:], s=1,
cmap="RdBu", vmin=-.2, vmax=1.2, linewidth=1)
ax.set(aspect="equal",
xlim=(-2, 3), ylim=(-1.5, 2),
xlabel="$X_1$", ylabel="$X_2$")
#------------------- regressão logistica with polynomial features
# gerando polynomial features
poly_features = PolynomialFeatures(degree=3, include_bias=False)
X_poly = poly_features.fit_transform(X)
df_poly=DataFrame(X_poly)
# regressão
log_reg_poly = LogisticRegression()
log_reg_poly.fit(X_poly, y)
#AUC
probs = log_reg_poly.predict_proba(X_poly)
probs = probs[:, 1]
roc_auc_score(y, probs)
#----------------- ploting decision boundary
#creating grid
xx, yy = np.mgrid[-3:3:.01, -3:3:.01]
X_2=DataFrame(dict(x=xx.ravel(), y=yy.ravel()))
X_poly = poly_features.fit_transform(X_2)
probs = log_reg_poly.predict_proba(X_poly)[:, 1].reshape(xx.shape)
#ploting nomlinear decision boundary
f, ax = plt.subplots(figsize=(8, 6))
ax.contour(xx, yy, probs, levels=[.5], cmap="Greys", vmin=0, vmax=.6)
ax.scatter(X[100:,0], X[100:, 1], c=y[100:], s=1,
cmap="RdBu", vmin=-.2, vmax=1.2, linewidth=1)
ax.set(aspect="equal",
xlim=(-2, 3), ylim=(-1.5, 2),
xlabel="$X_1$", ylabel="$X_2$")
#------------------- regressão logistica with polynomial features with only 4 variables
# the two original and two of the created with the biggest coefficient in the previous regression
# gerando polynomial features
poly_features = PolynomialFeatures(degree=3, include_bias=False)
X_poly = poly_features.fit_transform(X)
df_poly=DataFrame(X_poly).iloc[:,[0,1,2,5]]
X_poly=X_poly[:,[0,1,2,5]]
# regressão
log_reg_poly = LogisticRegression()
log_reg_poly.fit(X_poly, y)
#AUC
probs = log_reg_poly.predict_proba(X_poly)
probs = probs[:, 1]
roc_auc_score(y, probs)
#----------------- ploting decision boundary
#creating grid
xx, yy = np.mgrid[-3:3:.01, -3:3:.01]
X_2=DataFrame(dict(x=xx.ravel(), y=yy.ravel()))
X_poly = poly_features.fit_transform(X_2)
X_poly=X_poly[:,[0,1,2,5]]
probs = log_reg_poly.predict_proba(X_poly)[:, 1].reshape(xx.shape)
#ploting nomlinear decision boundary
f, ax = plt.subplots(figsize=(8, 6))
ax.contour(xx, yy, probs, levels=[.5], cmap="Greys", vmin=0, vmax=.6)
ax.scatter(X[100:,0], X[100:, 1], c=y[100:], s=1,
cmap="RdBu", vmin=-.2, vmax=1.2, linewidth=1)
ax.set(aspect="equal",
xlim=(-2, 3), ylim=(-1.5, 2),
xlabel="$X_1$", ylabel="$X_2$")