【问题标题】:Using Neural Network program to predict stock prices使用神经网络程序预测股票价格
【发布时间】:2022-01-25 21:01:27
【问题描述】:

我尝试编写神经网络。我有一个下载的.csv file,其中包含某些加密货币股票的开始时间和收盘价;我希望创建一个预测模型。

import matplotlib.pyplot as plt
import numpy as np

class NeuralNetwork:

    def __init__(self, width, activation_function):
        self.width=width
        # activation function = 'Sigmoid' , 'ReLu')
        self.activation_function=activation_function
        # number of hidden layers
        self.depth=len(width)-2
        # Weight matrix
        self.W= np.array([np.random.randn(self.width[i+1],self.width[i]) for i in range(self.depth+1)], dtype=object)
        # bais vector
        self.b= np.array([np.random.randn(self.width[i+1]) for i in range(self.depth+1)], dtype=object)
        #learning rate
        self.alpha = 0.1 

    def sigma(self,x):
        if self.activation_function == 'Sigmoid':
            return 1/(1+np.exp(-x))
        else:
            if x.any()>=0:
                return x
            else:
                return 0

    def d_sigma(self,x):
        if self.activation_function == 'Sigmoid':
            return self.sigma(x)*self.sigma(1-x)
        else:
            if x>=0:
                return 1
            else:
                return 0

  ## vectorize the function
    def sigma_vec(self,X):
        return np.array([self.sigma(x) for x in X],dtype=np.float128)
    def d_sigma_vec(self,X):
        return np.array([self.d_sigma(x) for x in X],dtype=np.float128)

    def h(self,x):  
        a=np.copy(self.b)  
        z=np.copy(self.b)
        z[0]=np.dot(self.W[0],x)+self.b[0]
        for i in range(self.depth):
            a[i]=self.sigma_vec(z[i])
            z[i+1]=np.dot(self.W[i+1],a[i])+self.b[i+1]
        return z[-1]

    def plot(self,plt,X,col):
        dx = np.linspace(X[0],X[len(X)-1], 100)
        h_dx = np.array([ self.h([x]) for x in dx],dtype=np.float128)
        plt.plot(dx,h_dx,color=col)

    def plot0(self,plt,col):
        dx = np.linspace(0,1, 100)
        h_dx = np.array([ self.h([x]) for x in dx],dtype=np.float128)
        plt.plot(dx,h_dx,color=col)

    def gradient_of_loss_function(self,h,y):
        return h-y

    def gradient(self,x,y):
        a=np.copy(self.b)
        z=np.copy(self.b)
        z[0]=np.dot(self.W[0],x)+self.b[0]
        for i in range(self.depth):
            a[i]=self.sigma_vec(z[i])
            z[i+1]=np.dot(self.W[i+1],a[i])+self.b[i+1]
            gradient_b=np.copy(self.b)
            gradient_W=np.copy(self.W)
            gradient_b[-1]=self.gradient_of_loss_function(z[-1],y)
            for i in reversed(range(self.depth)):
                gradient_b[i]=np.dot(np.transpose(self.W[i+1]),gradient_b[i+1])*self.d_sigma_vec(z[i])
                gradient_W[i+1]=np.outer(gradient_b[i+1],a[i])
                gradient_W[0]=np.outer(gradient_b[0],x)
        return gradient_b,gradient_W;

    def batch_gradient(self,X,Y):
        batch_gradient_W=0
        batch_gradient_b=0
        for i in range(len(X)):
            gradient_b,gradient_W = self.gradient(X[i],Y[i])
            batch_gradient_W += gradient_W
            batch_gradient_b += gradient_b
        return batch_gradient_b,batch_gradient_W;

    def train(self,X,Y,iterate):
        for i in range(iterate):
            alpha= self.alpha/len(X)
            batch_gradient_b,batch_gradient_W =self.batch_gradient(X,Y)
            self.W -= alpha*batch_gradient_W
            self.b -= alpha*batch_gradient_b
with open(mergedcsv, newline='') as csvfile:
    rows=csv.reader(csvfile)
    for row in rows:
        i=i+1
    print(i)
UsePercent = 1
datanum=i
with open(mergedcsv, newline='') as csvfile:
    i=1
    rows=csv.reader(csvfile)
    data = []
    data_axis = []
    putime = []
    hide_data = []
    hide_data_axis = []
    hide_putime = []
    time=0
    for row in rows:
 #       if i <=4 and i>1:
        if i<=2:
            i=i+1
            continue;
        i=i+1
        if(i>datanum*UsePercent):
            try:
                hide_data.append(round(float(row[5]),2))
                time= float(row[1])
                time = int(time/1000)
                hide_data_axis.append(time)
                hide_putime.append(i)
            except ValueError:
                print("",end="")
                print(time)
            continue
        try:
            data.append(round(float(row[5]),2))
            time= float(row[1])
            time = int(time/1000)
            data_axis.append(time)
            putime.append(i)
        except ValueError:
            print("",end="")
            print(time)

创建模型需要哪些合适的变量?这是我尝试过的一个;由于 sigmoid 激活函数的指数幂公式,它会导致溢出错误。我将不胜感激有关合适的 X 轴输入、Y 轴输入和宽度的建议。

Y= (np.array(data, dtype=np.float128).reshape(-1,1))[:28] #data = array of closing prices
X= (np.array(data_axis, dtype=np.float128).reshape(-1,1))[:28] #data_axis is an array representing the time

width=[1,5,3,1]
nn=NeuralNetwork(width,'Sigmoid')

plt.scatter(X,Y,color='r')
nn.plot0(plt,'b')
nn.train(X,Y,10)
nn.plot0(plt,'g')
plt.show()

提前致谢。

【问题讨论】:

  • 我怀疑这样的预测会奏效,因为改变的原因不在数据集中——而且可能永远不会。价格变动的原因可能在新闻中。

标签: python neural-network gradient-descent


【解决方案1】:

正如@ThomasWeller 已经提到的:仅对收盘价进行培训不会使您的模型表现良好。您将不得不考虑其他数据。喜欢 Google-Trends / Twitter / News。

另外我想知道你为什么不采用像 PytorchTensorflow 这样的框架?

【讨论】:

  • 我没有使用过这两个框架。其他人愿意帮助我并使用 Pytorch,但我们都被返回的错误所困扰(他们还使用收盘价作为模型。)我是否应该从相关方法重新开始?
  • 我会建议说实话。在 Github 上,您会找到从其中一个框架开始的好示例。
  • 好的,谢谢。尝试修复这段代码返回的溢出错误有什么意义吗?我很担心,因为这些溢出/无效值错误不会发生在程序中创建的随机/生成数据(余弦函数+噪声是一种有效的数据)中,而是连续发生在来自导入文件的数据中,即使在舍入浮点数或将数组 dtype 更改为 np.float128。我已经确保数据没有损坏,NumPy 可以对其进行成功的线性回归
猜你喜欢
  • 2016-08-22
  • 2014-11-29
  • 2014-11-08
  • 1970-01-01
  • 2016-10-26
  • 1970-01-01
  • 2023-01-13
  • 1970-01-01
  • 2021-08-03
相关资源
最近更新 更多