use*_*030 3 python machine-learning neural-network dataframe data-science
我正在尝试从头开始创建最基本的神经网络来预测苹果的股票。以下代码是我迄今为止在查看数据科学教程的帮助下得到的代码。然而,我正在实际输入数据并确保其正确。我会输入股票交易的 pandas 数据框架。这是我对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)
Run Code Online (Sandbox Code Playgroud)
如果您有任何建议、更正或任何其他信息,请告诉我,因为我全身心投入学习神经网络。
谢谢。
小智 5
在神经网络中直接使用 Pandas 绝对是荒谬的。表现将会很糟糕。相反,您要做的是传入底层 numpy 数组。
X = df[['Open','Close','High','Low','Volume']].values
y = df['adj close'].values
Run Code Online (Sandbox Code Playgroud)
这能回答问题吗?
归档时间: |
|
查看次数: |
7420 次 |
最近记录: |