Skip to content

Instantly share code, notes, and snippets.

@demacdolincoln
Created Jan 22, 2022
Embed
What would you like to do?
-8.171718912398114654e+00 -1.105262832665243877e+01 -1.000000000000000000e+00
-7.490187732858270664e+00 -9.563752698052290668e+00 -1.000000000000000000e+00
-9.311713952065543864e+00 -1.896071748451181760e+00 -1.000000000000000000e+00
-9.467212491967019972e+00 -1.118769877635893195e+01 -1.000000000000000000e+00
-7.911676667771658344e+00 -9.693849518264901377e+00 -1.000000000000000000e+00
-8.282893174772150147e+00 -9.144988026418158000e+00 -1.000000000000000000e+00
-8.121352601253962078e+00 -3.025941408421603640e+00 -1.000000000000000000e+00
-9.268686283323857467e+00 -1.180441999435009048e+00 -1.000000000000000000e+00
-1.080689494773629100e+01 -2.853866755413609457e+00 -1.000000000000000000e+00
-9.242493621012810578e+00 -1.155937856301682132e+01 -1.000000000000000000e+00
-7.317901872687513354e+00 -1.250204067977236555e+01 -1.000000000000000000e+00
-9.806418407327813114e+00 -4.244638882606654562e+00 -1.000000000000000000e+00
-9.279353596657722747e+00 -1.525010594129851071e+00 -1.000000000000000000e+00
-8.860289108936916236e+00 -1.054011821854292208e+01 -1.000000000000000000e+00
-9.677953326481564034e+00 -3.755129935618420234e+00 -1.000000000000000000e+00
-8.150161464883527884e+00 -8.614704964919566166e+00 -1.000000000000000000e+00
-1.076313835335253977e+01 -3.225717707918249744e+00 -1.000000000000000000e+00
-1.011741289865546811e+01 -2.254628418435252080e+00 -1.000000000000000000e+00
-9.174828053022212160e+00 -9.398750918456995507e+00 -1.000000000000000000e+00
-8.486775638175002356e+00 -9.707469031347084609e+00 -1.000000000000000000e+00
-8.805808599781544288e+00 -9.872739223719644741e+00 -1.000000000000000000e+00
-9.492443123425880813e+00 -1.263503754785473454e+01 -1.000000000000000000e+00
-7.666756025716138012e+00 -1.054756153667231366e+01 -1.000000000000000000e+00
-1.236612283410439517e+01 -2.445408643449582531e+00 -1.000000000000000000e+00
-8.278793692023867834e+00 -9.420252870375405152e+00 -1.000000000000000000e+00
-8.294090460342452786e+00 -8.598487735960654632e+00 -1.000000000000000000e+00
-1.043567517793255739e+01 -2.907089741432771213e+00 -1.000000000000000000e+00
-8.949942451060344695e+00 -1.121579418042172804e+01 -1.000000000000000000e+00
-8.289397344322672723e+00 -9.153964081277896625e+00 -1.000000000000000000e+00
-8.552895078530569961e+00 -9.843515490865019757e+00 -1.000000000000000000e+00
-8.545791483929349397e+00 -9.153679900088356192e+00 -1.000000000000000000e+00
-9.455594155142245327e+00 -9.185632614729351886e+00 -1.000000000000000000e+00
-8.870262100490478474e+00 -3.291201012716356455e+00 -1.000000000000000000e+00
-8.703878999678069661e+00 -7.678946837103543643e+00 -1.000000000000000000e+00
-8.280806177064910401e+00 -2.043475079538770167e+00 -1.000000000000000000e+00
-9.936227072515164238e+00 -2.517638932314691491e+00 -1.000000000000000000e+00
-1.007120556720299120e+01 -1.797837651707208328e+00 -1.000000000000000000e+00
-8.226340403836935877e+00 -2.581841349112839357e+00 -1.000000000000000000e+00
-1.014876684987608257e+01 -3.177070423788575848e+00 -1.000000000000000000e+00
-9.748242076573321313e+00 -2.118919203835964993e+00 -1.000000000000000000e+00
-9.289781568017156488e+00 -3.997159817000010662e+00 -1.000000000000000000e+00
-9.922075229724736545e+00 -2.709534637677058821e+00 -1.000000000000000000e+00
-9.459676164807426346e+00 -1.060754328076444075e+01 -1.000000000000000000e+00
-8.988441266374136873e+00 -1.143561530675511051e+01 -1.000000000000000000e+00
-9.351711437797979087e+00 -1.745715886657412952e+00 -1.000000000000000000e+00
-9.130559937842983587e+00 -8.588179184192854976e+00 -1.000000000000000000e+00
-8.079242086797108868e+00 -9.093779384462466808e+00 -1.000000000000000000e+00
-8.944033706340807299e+00 -8.868440993314511189e+00 -1.000000000000000000e+00
-1.015080369009737460e+01 -2.041345556428930230e+00 -1.000000000000000000e+00
-9.048193433849828438e+00 -1.007639646375991660e+01 -1.000000000000000000e+00
-1.132862276725865236e+01 -2.703635508033289003e+00 -1.000000000000000000e+00
-1.178904159005036867e+01 -3.451570069738225666e+00 -1.000000000000000000e+00
-1.020094630576773120e+01 -4.273209199386523771e+00 -1.000000000000000000e+00
-9.905973364334599296e+00 -1.033080497577856605e+01 -1.000000000000000000e+00
-9.660557779595558614e+00 -2.173131404602483663e+00 -1.000000000000000000e+00
-9.592585169139512757e+00 -2.548315440483198913e+00 -1.000000000000000000e+00
-7.833811514638868267e+00 -1.036797071883214194e+01 -1.000000000000000000e+00
-8.082537516971964209e+00 -9.639727659429896178e+00 -1.000000000000000000e+00
-9.126702310976083865e+00 -8.706517508850708964e+00 -1.000000000000000000e+00
-9.524429290438067852e+00 -3.128715733055334347e+00 -1.000000000000000000e+00
-9.885382676599167695e+00 -2.283316046700806012e+00 -1.000000000000000000e+00
-8.387766351654152075e+00 -3.315839522107415061e+00 -1.000000000000000000e+00
-8.281224587665551695e+00 -3.539654732789368374e+00 -1.000000000000000000e+00
-1.094818404163773451e+01 -5.425453280223198504e+00 -1.000000000000000000e+00
-9.382233593855394460e+00 -9.318824211869159768e+00 -1.000000000000000000e+00
-9.201626369593828514e+00 -2.769726933802680868e+00 -1.000000000000000000e+00
-1.132686803265289299e+01 -3.483421081314101109e+00 -1.000000000000000000e+00
-8.770108148547382854e+00 -8.405709001371926448e+00 -1.000000000000000000e+00
-7.968197353605480870e+00 -8.874412074735568368e+00 -1.000000000000000000e+00
-1.131698701704166510e+01 -2.231037091906831638e+00 -1.000000000000000000e+00
-7.699027767677012690e+00 -1.023690248880774867e+01 -1.000000000000000000e+00
-1.093709331899847470e+01 -9.651336731474213337e+00 -1.000000000000000000e+00
-9.300331696520057534e+00 -8.718395240852093053e+00 -1.000000000000000000e+00
-8.806722691573963147e+00 -9.605580783955439017e+00 -1.000000000000000000e+00
-8.601201575747289496e+00 -8.174595382354203466e+00 -1.000000000000000000e+00
-9.413631281142890828e+00 -1.095013471464641341e+01 -1.000000000000000000e+00
-9.565797875917898097e+00 -3.335669599048201484e+00 -1.000000000000000000e+00
-7.685209367588965002e+00 -9.983839209758075484e+00 -1.000000000000000000e+00
-9.126589092553185623e+00 -2.586764088197573752e+00 -1.000000000000000000e+00
-9.960158881137221698e+00 -4.133390780866080583e+00 -1.000000000000000000e+00
-1.001852445540278147e+01 -2.062672171918593200e+00 -1.000000000000000000e+00
-9.012340807456796554e+00 -1.085588928088407101e+01 -1.000000000000000000e+00
-6.294948269738444679e+00 -8.640569664402672601e+00 -1.000000000000000000e+00
-8.520646911212223173e+00 -9.245647117307976615e+00 -1.000000000000000000e+00
-9.423483374196500506e+00 -3.101446358467259135e+00 -1.000000000000000000e+00
-8.346226291749644588e+00 -9.817290975521734353e+00 -1.000000000000000000e+00
-8.640795829014331630e+00 -2.462835201969536758e+00 -1.000000000000000000e+00
-1.049736417778578179e+01 -1.830932894306940950e+00 -1.000000000000000000e+00
-7.741798079429365487e+00 -1.028946917622236867e+01 -1.000000000000000000e+00
-9.306810221712472853e+00 -3.795024712972875136e+00 -1.000000000000000000e+00
-9.736578664854539156e+00 -2.286520563002405915e+00 -1.000000000000000000e+00
-1.014834371159328263e+01 -2.086203653052651319e+00 -1.000000000000000000e+00
-1.121368802851395152e+01 -3.169222806818813964e+00 -1.000000000000000000e+00
-1.034607250175963600e+01 -9.132424797424892660e+00 -1.000000000000000000e+00
-8.579955893822271307e+00 -8.305086711703914659e+00 -1.000000000000000000e+00
-8.279343928487598703e+00 -4.201909041062296346e+00 -1.000000000000000000e+00
-9.272492023832658248e+00 -3.093686862535664606e+00 -1.000000000000000000e+00
-1.073187854792493745e+01 -2.988617605795805154e+00 -1.000000000000000000e+00
-1.034333876835125210e+01 -9.581157331877022898e+00 -1.000000000000000000e+00
-1.097663126999945327e+01 -1.232821395290015110e+00 -1.000000000000000000e+00
import numpy as np
from sklearn.datasets import make_blobs
from tqdm import tqdm
from matplotlib import colors, pyplot as plt
def sigmoid(z):
return 1/(1+np.exp(-z))
def perceptron(pesos, data, activ=sigmoid):
return activ(
np.sum(pesos*data, axis=1)
)
def mse(z, target):
linhas = z.shape[0]
if linhas != target.shape[0]:
raise "quantidade diferentes de items"
return 1/linhas * np.sum((z-target)**2)
load_from_csv = True
resolução = 100
bias = -0.5
if load_from_csv:
data = np.loadtxt("data.csv", delimiter=",")
target = np.loadtxt("target.csv", delimiter=",")
else:
data, target = make_blobs(centers=2)
data = np.c_[data, np.ones(data.shape[0])*-1]
np.savetxt("data.csv", data, delimiter=",")
np.savetxt("target.csv", target, delimiter=",")
xmin = data[:, 0].min() -10
xmax = data[:, 0].max() +10
ymin = data[:, 1].min() -10
ymax = data[:, 1].max() +10
x = np.linspace(xmin, xmax, resolução)
y = np.linspace(ymin, ymax, resolução)
#bias = np.arange(-1 , 1, resolução)
#xx, yy, bb = np.meshgrid(x, y, bias)
xx, yy = np.meshgrid(x, y)
#pesosx, pesosy, pesosb = list(map( lambda x: x.ravel(),(xx, yy, bb)))
pesosx, pesosy = list(map( lambda x: x.ravel(),(xx, yy)))
#del x, y, bias
resultados = np.zeros(pesosx.shape[0])
pbar = tqdm(total=pesosx.shape[0])
#for index, (x, y, b) in enumerate(zip(pesosx, pesosy, pesosb)):
for index, (X, Y) in enumerate(zip(pesosx, pesosy)):
#pesos = np.array((x, y, b))
pesos = np.array((X, Y, bias))
z = perceptron(pesos, data)
resultados[index] = mse(z, target)
if index % 10 == 0:
pbar.update(10)
pbar.close()
cols = 6
fig = plt.figure()
gs = fig.add_gridspec(nrows=4, ncols=cols)
ax0 = fig.add_subplot(gs[:2, :3])
ax0.set_aspect("equal")
ax0.scatter(data[:, 0], data[:, 1], c=target)
ax1 = fig.add_subplot(gs[:2, 3:], projection="3d")
Z = resultados.reshape(xx.shape)
ax1.plot_surface(
xx, yy, Z,
norm=colors.Normalize(vmin=Z.min(), vmax=Z.max()),
cmap=plt.cm.viridis_r, alpha=0.75
)
_Z = Z.ravel()
_xx = xx.ravel()
_yy = yy.ravel()
minimum, maximum = _Z.argmin(), _Z.argmax()
xminimum, xmaximum = _xx[minimum], _xx[maximum]
yminimum, ymaximum = _yy[minimum], _yy[maximum]
cx = np.linspace(xmaximum * np.random.rand(), xminimum, cols)
cy = np.linspace(ymaximum * np.random.rand(), yminimum, cols)
cz = [mse(perceptron(np.array((X, Y, bias)), data), target) for X, Y in zip(cx, cy)]
area = np.array((
pesosx, pesosy, np.ones(pesosx.shape)*-1
)).transpose()
ax1.scatter3D(cx, cy, cz, s=50, c="red", data="12345", depthshade=False)
for index, (_x, _y, _z) in enumerate(zip(cx, cy, cz)):
ax1.text(_x, _y, _z+0.1, f'{index}')
for i in tqdm(range(cols)):
ax3 = fig.add_subplot(gs[2, i])
ax3.set_title(f"ponto: {i}\nx = {cx[i]:.2f} | y = {cy[i]:.2f}\nerro={cz[i]:.5f}")
ax3.scatter(data[:, 0], data[:, 1], c=target)
pesos = np.array((cx[i], cy[i], bias))
czz = perceptron(pesos, area)
ax3.contourf(xx, yy, czz.reshape(xx.shape), alpha=0.25, cmap="viridis")
ax4 = fig.add_subplot(gs[3, i])
ax4.scatter(data[:, 0], data[:, 1], c=target, cmap="RdBu", marker="+",s=5)
X, Y = data[:, 0] * cx[i], data[:, 1] * cy[i]
ax4.scatter(X, Y, c=target, cmap="RdBu", marker="x", s=15)
fig.tight_layout()
fig.show()
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
1.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
1.000000000000000000e+00
0.000000000000000000e+00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment