欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

DL之perceptron:利用perceptron感知机对股票实现预测

发布时间:2025/3/21 编程问答 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 DL之perceptron:利用perceptron感知机对股票实现预测 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

DL之perceptron:利用perceptron感知机对股票实现预测

 

 

目录

输出结果

实现代码


 

 

 

输出结果

更新……

 

 

实现代码

import numpy as np import operator import os# create a dataset which contains 3 samples with 2 classes def createDataSet():# create a matrix: each row as a samplegroup = np.array([[20,2], [50,1], [10,3],[60,0.5]])labels = [1, -1, 1,-1] # four samples and two classesreturn group, labels#classify using perceptron def perceptronClassify(trainGroup,trainLabels):global w, bisFind = False #the flag of find the best w and bnumSamples = trainGroup.shape[0] #计算矩阵的行数mLenth = trainGroup.shape[1] #计算矩阵的列数w = [0]*mLenth #初始化wb = 0 #初始化bwhile(not isFind): #定义迭代计算w和b的循环for i in range(numSamples):if cal(trainGroup[i],trainLabels[i]) <= 0: #计算损失函数,y(wx+b)<=0时更新参数print (w,b)update(trainGroup[i],trainLabels[i]) #更新计算w和bbreak #end for loopelif i == numSamples-1:print (w,b)isFind = True #end while loopdef cal(row,trainLabel): #定义损失函数global w, bres = 0for i in range(len(row)):res += row[i] * w[i]res += bres *= trainLabelreturn res def update(row,trainLabel): #学习率为1的更新计算global w, bfor i in range(len(row)):w[i] += trainLabel * row[i]b += trainLabelg,l =createDataSet() #生成数据集 perceptronClassify(g,l) #训练分类器

 

总结

以上是生活随笔为你收集整理的DL之perceptron:利用perceptron感知机对股票实现预测的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。