Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas data frame used as input for neural network?

I'm trying to create the most basic neural network from scratch to predict stocks for apple. The following code is what i have gotten to so far with assistance from looking at data science tutorials. However, I'm at the bit of actually feeding in the data and making sure it does so correctly.I would to feed in a pandas data frame of a stock trade. This is my view of the NN.

  • 5 Input nodes (Open,Close,High,Low,Volume) *note - this will be in a pandas data frame with a datetime index
  • AF that sums the weights of each input.
  • Sigmoid function to normalise the values
  • 1 output (adj close) *Not sure what I should use as the actual value

Then the process is to move back using the back-propagation technique.

import pandas as pd
import pandas_datareader as web
import matplotlib.pyplot as plt
import numpy as np

def sigmoid(x):
    return 1.0/(1+ np.exp(-x))

def sigmoid_derivative(x):
    return x * (1.0 - x)

class NeuralNetwork:
    def __init__(self, x, y):
        self.input      = x
        self.weights1   = #will work out when i get the correct input
        self.weights2   = #will work out when i get the correct input                
        self.y          = y
        self.output     = #will work out 

    def feedforward(self):
        self.layer1 = sigmoid(np.dot(self.input, self.weights1))
        self.output = sigmoid(np.dot(self.layer1, self.weights2))

    def backprop(self):
        # application of the chain rule to find derivative of the loss function with respect to weights2 and weights1
        d_weights2 = np.dot(self.layer1.T, (2*(self.y - self.output) * sigmoid_derivative(self.output)))
        d_weights1 = np.dot(self.input.T,  (np.dot(2*(self.y - self.output) * sigmoid_derivative(self.output), self.weights2.T) * sigmoid_derivative(self.layer1)))

        # update the weights with the derivative (slope) of the loss function
        self.weights1 += d_weights1
        self.weights2 += d_weights2


if __name__ == "__main__":
    X = #need help here
    y = #need help here
    nn = NeuralNetwork(X,y)

    for i in range(1500):
        nn.feedforward()
        nn.backprop()

    print(nn.output)

If you have any suggestions, corrections or anything please let me know because I am thrououghly invested into learning the neural networks.

Thanks.

like image 382
user218030 Avatar asked Sep 12 '25 19:09

user218030


1 Answers

Directly using Pandas in a neural network would be absolutely ridiculous. The performance would be abysmal. What you do instead is pass in the underlying numpy array.

X = df[['Open','Close','High','Low','Volume']].values

y = df['adj close'].values

Does that answer the question?

like image 136
Chad Bernier Avatar answered Sep 14 '25 11:09

Chad Bernier