Skip to content

Instantly share code, notes, and snippets.

@celsowm
Created November 11, 2023 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save celsowm/b722b72845b29309223b5a3424c5ccf4 to your computer and use it in GitHub Desktop.
Save celsowm/b722b72845b29309223b5a3424c5ccf4 to your computer and use it in GitHub Desktop.
neural_network_scratch.py
import numpy as np, sys
import pandas as pd
# Ler o arquivo CSV
df = pd.read_csv('diabetes.csv')
# Extrair os dados para feature_set e labels
feature_set = feature_set = df.drop('diabetico', axis=1).values
labels = df['diabetico'].values.reshape(-1, 1)
np.random.seed(42)
weights = np.random.rand(3,1)
bias = np.random.rand(1)
lr = 0.05
def sigmoid(x):
return 1/(1+np.exp(-x))
def sigmoid_der(x):
return sigmoid(x)*(1-sigmoid(x))
for epoch in range(20000):
inputs = feature_set
# feedforward passo 1
XW = np.dot(feature_set, weights) + bias
#feedforward passo 2
z = sigmoid(XW)
# backpropagation passo 1
error = z - labels
# backpropagation passo 2
dcost_dpred = error
dpred_dz = sigmoid_der(z)
z_delta = dcost_dpred * dpred_dz
inputs = feature_set.T
weights -= lr * np.dot(inputs, z_delta)
for num in z_delta:
bias -= lr * num
single_point = np.array([0,1,0]) #np.array([1,0,0])
result = sigmoid(np.dot(single_point, weights) + bias)
print(result)
# Limiar para converter a saída da rede neural em 0 ou 1
limiar = 0.5
# Verificar se a previsão é maior que o limiar
resultado = 1 if result[0] > limiar else 0
print(resultado)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment