我正在尝试从头开始创建最基本的神经网络,以预测苹果的库存。到目前为止,以下代码是我在查看数据科学教程的帮助下所获得的。但是,我实际上需要输入数据并确保其正确执行。我将输入股票交易的熊猫数据框。这是我对NN的看法。
然后,该过程是使用反向传播技术向后移动。
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)
如果您有任何建议,更正或任何其他内容,请让我知道,因为我已全心投入于学习神经网络。
谢谢。
答案 0 :(得分:0)
在神经网络中直接使用熊猫绝对是荒谬的。表演会很糟糕。相反,您要做的是传递基础numpy数组。
X = df[['Open','Close','High','Low','Volume']].values
y = df['adj close'].values
这能回答问题吗?