基于二乘法的数据预测算法

原理:根据最近8天的销售数据预测最近一天的销售数据

开发工具:JetBrains PyCharm 2017.2.3

代码:

from numpy import *

def standRegres(xArr,yArr):
    xMat = mat(xArr); yMat = mat(yArr).T
    xTx = xMat.T*xMat
    if linalg.det(xTx) == 0.0:
        print("This matrix is singular, cannot do inverse")
        return
    ws = xTx.I * (xMat.T*yMat)
    return ws

def loadDataSet(fileName):
    allDataMat = []
    dataMat = []
    yMat = []
    fr = open(fileName)
    for line in fr.readlines():
        curLine = line.strip().split('\t')
        allDataMat.append(float(curLine[0]))

    ii = 0
    for j in range(len(allDataMat)):
        if (j > 8):
            lineArr = []
            lineArr.append(allDataMat[j - 8])
            lineArr.append(allDataMat[j - 7])
            lineArr.append(allDataMat[j - 6])
            lineArr.append(allDataMat[j - 5])
            lineArr.append(allDataMat[j - 4])
            lineArr.append(allDataMat[j - 3])
            lineArr.append(allDataMat[j - 2])
            lineArr.append(allDataMat[j - 1])
            dataMat.append(lineArr)
            yMat.append(allDataMat[j])
    return yMat,dataMat

#加载数据--X为前8个连续数据。Y为当前值
yArr,xArr = loadDataSet('myDemo.txt')
#print(yArr)
#print(xArr)
#求以前8个数为参数的回归系统(8个常量)
ws = standRegres(xArr,yArr)
print(ws)
#print("3.176513	3.960008".split('\t'))

print('-------------------------')
xMat = mat(xArr)
yMat = mat(yArr)
yHat = xMat*ws

print(yMat.tolist())
print(yHat[:,0].flatten().A[0])
print('-------------------------')
#基于总方差求平均差(波动范围)
print(sqrt(sum((yArr-yHat.T.flatten().A[0])**2)/yHat.size))

'''
print(yArr)
print(corrcoef(yHat.T,yMat))
'''
#画图
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)

a=[]
for i in range(len(xMat[:,1].flatten().A[0])):
    a.append(i)

ax.scatter(a,yMat.T[:,0].flatten().A[0])
ax.plot(a,yHat)
'''
xCopy = xMat.copy()
xCopy.sort(0)
yHat = xCopy*ws
ax.plot(xCopy[:,1],yHat)

'''
plt.show()

训练数据:

myDemo.txt

3.176513
3.816464
4.550095
4.256571
4.560815
3.929515
3.526170
3.156393
3.110301
3.149813
3.476346
4.119688
4.282233
3.486582
4.655492
3.965162
3.514900
3.125947
4.094115
3.476039
3.210610
3.190612
4.631504
4.295890
3.085028
3.448080
3.167440
3.364266
3.993482
3.891471
3.143259
3.114204
3.851484
4.621899
4.580768
3.620992
3.580501
4.618706
3.676867
4.641845
3.175939
4.264980
3.558448
3.436632
3.831052
3.182853
3.498906
3.946833
3.900583
4.238522
4.233080
3.521557
3.203344
4.278105
3.555705
3.502661
3.859776
4.275956
3.916191
3.587961
3.183004
4.225236
4.231083
4.240544
3.222372
4.021445
3.567479
3.562580
4.262059
3.208813
3.169825
4.193949
3.491678
4.533306
3.550108
4.636427
3.557078
3.552874
3.494159
3.206828
3.195266
4.221292
4.413372
4.184347
3.742878
3.201878
4.648964
3.510117
3.274434
3.579622
3.489244
4.237386
3.913749
3.228990
4.286286
4.628614
3.239536
4.457997
3.513384
3.729674
3.834274
3.811155
3.598316
4.692514
4.604859
3.864912
3.184236
3.500796
3.743365
3.622905
4.310796
3.583357
3.901852
3.233521
3.105266
3.865544
4.628625
4.231213
3.791149
3.968271
4.253910
3.194710
3.996503
3.904358
3.503976
4.557545
3.699876
4.613614
3.140401
4.206717
3.969524
4.476096
3.136528
4.279071
3.200603
3.299012
3.209873
3.632942
3.248361
3.995783
3.563262
3.649712
3.951845
3.145031
3.181577
4.637087
3.404964
3.873188
4.633648
3.154768
4.623637
3.078132
3.913596
3.221817
3.938071
3.880822
4.176436
4.648161
3.332312
4.240614
4.532224
4.557105
4.610072
4.636569
4.229813
3.500860
4.245514
4.605182
3.454340
3.180775
3.380820
4.565020
3.279973
4.554241
4.633520
4.281037
3.844426
3.891601
3.849728
3.492215
4.592374
4.632025
3.756750
3.133555
3.567919
4.363382
3.560165
4.564305
4.215055
4.174999
4.586640
3.960008
3.529963
4.213412
3.908685
3.585821
4.374394
3.213817
3.952681
3.129283

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值