from numpy.random.mtrand import randint
from sklearn.datasets import make_classification
import matplotlib.pyplot as plt
import numpy as np
import random
np.random.seed(97)
X, Y = make_classification( n_features=2, n_redundant=0, n_informative=1, n_clusters_per_class=1 )
plt.scatter(X[:, 0], X[:, 1], marker='o', c=Y, edgecolor='K')
def plotModel(w1, w2, b):
x = np.linspace(-2, 4, 50)
y = (-w1*x -b)/w2
xmin, xmax = plt.gca().get_xlim()
ymin, ymax = plt.gca().get_ylim()
plt.axvline(0, -1, 1, color='k', linewidth=1)
plt.axhline(0, -1, 4, color='k', linewidth=1)
plt.plot(x, y)
plt.grid(True)
plt.xlim(xmin, xmax)
plt.ylim(xmin, ymax)
w1 = -5
w2 = 0.5
b = -1.5
plotModel(w1, w2, b)
p = [random.uniform(-2.0, 2.9), random.uniform(-2.0, 2.9)]
print("Distancia da reta:", w1* p[0] + w2 * p[1] + b)
def classify(ponto, w1, w2, b):
ret = w1 * ponto[0] + w2 * ponto[1] + b
if(ret >= 0):
return 0, 'blue'
else:
return 1, 'yellow'
classe, cor = classify(p, w1, w2, b)
print("Classificação: ", classe, cor)
plotModel(w1, w2, b)
plt.scatter(p[0], p[1], marker='x', color='r' )
acertos = 0
for k in range(len(X)):
categ, _ = classify(X[k], w1, w2, b)
if categ == Y[k]:
acertos += 1
print("Acertos: ", acertos)
print("Acurácia: {0}%".format((acertos/len(X)*100)))