Skip to content

Instantly share code, notes, and snippets.

@gabriel19913
Created April 18, 2019 19:47
Show Gist options
  • Save gabriel19913/007179ff4cc7cae7f29dadd17ac854b9 to your computer and use it in GitHub Desktop.
Save gabriel19913/007179ff4cc7cae7f29dadd17ac854b9 to your computer and use it in GitHub Desktop.
clear all
clc
x1 = -0.25 + 0.08 * randn(1, 10);
x2 = 0.5 + 0.08 * randn(1, 10);
C1 = [x1;x2];
x1 = 0.7 + 0.08 * randn(1, 10);
x2 = 0.25 + 0.08 * randn(1, 10);
C2 = [x1;x2];
% Plota valores para a Classe 1
plot(C1(1,:),C1(2,:),'o')
hold on;
plot(C2(1,:),C2(2,:),'*r')
passo = 0.7;
W = randn(3,1);
x = [C1 C2; ones(1, 20)];
tal = [ones(10,1);-ones(10,1)];
cont = 0;
Y = 1;
while isempty(Y)==0
Y = [];
tall = [];
for k=1:20
if (tal(k)*W'*x(:,k))>=0
cont = cont+1;
Y(:,cont)=x(:,k);
Y(:,cont)
tall(cont,:) = tal(k);
end
end
u2 = 0:0.1:0.8;
u1 = (-W(2)/W(1))*u2 - (W(3)/W(1));
if isempty(Y) == 1
W = W;
else
W = W - passo*Y*tall;
end
end
W
u1 = (-W(2)/W(1))*u2 - (W(3)/W(1));
plot(u1,u2,'k--')
# Exercício 3
import numpy as np
import random
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
sns.set(rc={'figure.figsize':(12, 8)})
# Declarando as classes bidimensionais equiprováveis
C1 = np.random.multivariate_normal(np.array([5, 5]), np.matrix('1 0; 0 1'), 500).T
C2 = np.random.multivariate_normal(np.array([0, 0]), np.matrix('1 0; 0 1'), 500).T
# Gerando os gráficos mostrando as classes
plt.scatter(C1[0,:], C1[1,:],marker='o')
plt.scatter(C2[0,:], C2[1,:],marker='+', facecolor='red')
plt.legend(('Classe 1', 'Classe 2'))
plt.xlabel('$X_{1}$')
plt.ylabel("$X_{2}$")
plt.title('Distribuição de classes aletórias com superfície'
' de separação')
# Rede
passo = 0.2
pesos = np.random.randn(3, 1)
x = np.concatenate((np.hstack((C1,C2)), np.ones([1,1000])), axis=0)
tal = np.hstack((np.ones([1,500]), -np.ones([1,500])))
y = np.empty([3, 1])
cont = 0
while np.all(y==0):
y = np.zeros([3, 1])
tall = np.zeros([1, 1000])
for k in range(1000):
if (tal[k].dot(pesos.T).dot(x[:,k])) >=0:
cont += 1
y[:,cont] = x[:,k]
tall[cont,:] = tal[k]
u2 = np.arange(-4, 8)
u1 = (-pesos[1]/pesos[0])*u2 - (pesos[2]/pesos[0])
if not np.all(y==0):
pesos = pesos
else:
pesos = pesos - passo.dot(y).dot(tall)
u1 = (-pesos[1]/pesos[0])*u2 - (pesos[2]/pesos[0])
plt.plot(u1,u2)
print(pesos)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment