-
Notifications
You must be signed in to change notification settings - Fork 51
/
utils.py
42 lines (32 loc) · 1.56 KB
/
utils.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
import numpy
import matplotlib.pyplot as plt
def plot_classifier_decision(classifier, X, y, plot_scatter=True, margin=0.1):
x_range = X[:, 0].min() - margin, X[:, 0].max() + margin
y_range = X[:, 1].min() - margin, X[:, 1].max() + margin
xx, yy = numpy.meshgrid(numpy.linspace(*x_range, num=200),
numpy.linspace(*y_range, num=200))
data = numpy.vstack([xx.flatten(), yy.flatten()]).T
p = classifier.predict_proba(data)[:, 1]
plt.contourf(xx, yy, p.reshape(xx.shape), cmap='bwr', alpha=.5)
if plot_scatter:
plt.scatter(X[:, 0], X[:, 1], c=y, cmap='bwr', s=30)
plt.xlim(*x_range)
plt.ylim(*y_range)
def plot_regressor_decision_2d(classifier, X, y, plot_scatter=True, margin=0.1):
x_range = X[:, 0].min() - margin, X[:, 0].max() + margin
y_range = X[:, 1].min() - margin, X[:, 1].max() + margin
xx, yy = numpy.meshgrid(numpy.linspace(*x_range, num=200),
numpy.linspace(*y_range, num=200))
data = numpy.vstack([xx.flatten(), yy.flatten()]).T
p = classifier.predict(data)
plt.contourf(xx, yy, p.reshape(xx.shape), cmap='bwr', alpha=.5)
if plot_scatter:
plt.scatter(X[:, 0], X[:, 1], c=y, cmap='bwr', s=30)
plt.xlim(*x_range)
plt.ylim(*y_range)
def plot_regressor_decision(regressor, X, y, margin=0.1):
plt.scatter(X[:, 0], y, c='b', s=20)
x_range = X[:, 0].min() - margin, X[:, 0].max() + margin
x_values = numpy.linspace(*x_range, num=200)
plt.plot(x_values, regressor.predict(x_values[:, None]), 'g')
plt.xlim(*x_range)