示例#1
0
文件: TSAStock.py 项目: HK-Zhang/Corn
def KRR():
    np.random.seed(10)

    targetValues = np.genfromtxt("F:\PY\data\stock.txt", skip_header=1, dtype=None, delimiter="\t", usecols=(1))
    mse = {}
    for i in range(20, 50):
        trainingPoints = np.arange(i - 1).reshape(-1, 1)
        testPoints = np.arange(i).reshape(-1, 1)

        # training kernel matrix
        knl = mlpy.kernel_gaussian(trainingPoints, trainingPoints, sigma=1)
        # testing kernel matrix
        knlTest = mlpy.kernel_gaussian(testPoints, trainingPoints, sigma=1)

        knlRidge = KernelRidge(lmb=0.01, kernel=None)
        err = 0
        for j in range(1, 6):
            knlRidge.learn(knl, targetValues[-(i + j - 1) : -j])
            resultPoints = knlRidge.pred(knlTest)
            err += (resultPoints[-1] - targetValues[-j]) ** 2
            print i, resultPoints[-1], targetValues[-j]
        mse[i] = square(err / 5)
        print mse[i]
    sortedMse = sorted(mse.iteritems(), key=operator.itemgetter(1), reverse=False)
    print sortedMse[0]
示例#2
0
def KRRTest():
    np.random.seed(10)

    targetValues = np.genfromtxt('F:\PY\data\stock.txt',
                                 skip_header=1,
                                 dtype=None,
                                 delimiter='\t',
                                 usecols=(1))

    trainingPoints = np.arange(46).reshape(-1, 1)
    testPoints = np.arange(47).reshape(-1, 1)

    #training kernel matrix
    knl = mlpy.kernel_gaussian(trainingPoints, trainingPoints, sigma=1)
    #testing kernel matrix
    knlTest = mlpy.kernel_gaussian(testPoints, trainingPoints, sigma=1)

    knlRidge = KernelRidge(lmb=0.01, kernel=None)
    knlRidge.learn(knl, targetValues[-46:])
    resultPoints = knlRidge.pred(knlTest)

    print targetValues.size, resultPoints

    fig = plt.figure(1)
    plot1 = plt.plot(trainingPoints, targetValues[-46:], 'o')
    plot2 = plt.plot(testPoints, resultPoints)
    plt.show()
示例#3
0
def KRR():
    np.random.seed(10)

    targetValues = np.genfromtxt('F:\PY\data\stock.txt',
                                 skip_header=1,
                                 dtype=None,
                                 delimiter='\t',
                                 usecols=(1))
    mse = {}
    for i in range(20, 50):
        trainingPoints = np.arange(i - 1).reshape(-1, 1)
        testPoints = np.arange(i).reshape(-1, 1)

        #training kernel matrix
        knl = mlpy.kernel_gaussian(trainingPoints, trainingPoints, sigma=1)
        #testing kernel matrix
        knlTest = mlpy.kernel_gaussian(testPoints, trainingPoints, sigma=1)

        knlRidge = KernelRidge(lmb=0.01, kernel=None)
        err = 0
        for j in range(1, 6):
            knlRidge.learn(knl, targetValues[-(i + j - 1):-j])
            resultPoints = knlRidge.pred(knlTest)
            err += (resultPoints[-1] - targetValues[-j])**2
            print i, resultPoints[-1], targetValues[-j]
        mse[i] = square(err / 5)
        print mse[i]
    sortedMse = sorted(mse.iteritems(),
                       key=operator.itemgetter(1),
                       reverse=False)
    print sortedMse[0]
示例#4
0
文件: TSA.py 项目: HK-Zhang/Corn
def KRR():
    np.random.seed(10)

    targetValues = np.genfromtxt('F:\PY\data\Gold.csv',
                         skip_header=1,
                         dtype=None,
                         delimiter=',',
                         usecols=(1))

    trainingPoints = np.arange(125).reshape(-1, 1)
    testPoints = np.arange(126).reshape(-1, 1)

    #training kernel matrix
    knl = mlpy.kernel_gaussian(trainingPoints, trainingPoints, sigma=1)
    #testing kernel matrix
    knlTest = mlpy.kernel_gaussian(testPoints, trainingPoints, sigma=1)

    knlRidge = KernelRidge(lmb=0.01, kernel=None)
    knlRidge.learn(knl, targetValues)
    resultPoints = knlRidge.pred(knlTest)

    print(resultPoints)

    fig = plt.figure(1)
    plot1 = plt.plot(trainingPoints, targetValues, 'o')
    plot2 = plt.plot(testPoints, resultPoints)
    plt.show()
示例#5
0
文件: TSAStock.py 项目: HK-Zhang/Corn
def KRRProTy():
    np.random.seed(10)

    targetValues = np.genfromtxt("F:\PY\data\stockPT.txt", skip_header=1, dtype=None, delimiter="\t", usecols=(1))

    trainingPoints = np.arange(targetValues.size).reshape(-1, 1)
    testPoints = np.arange(targetValues.size + 1).reshape(-1, 1)

    # training kernel matrix
    knl = mlpy.kernel_gaussian(trainingPoints, trainingPoints, sigma=1)
    # testing kernel matrix
    knlTest = mlpy.kernel_gaussian(testPoints, trainingPoints, sigma=1)

    knlRidge = KernelRidge(lmb=0.01, kernel=None)
    knlRidge.learn(knl, targetValues)
    resultPoints = knlRidge.pred(knlTest)

    print targetValues.size, resultPoints

    fig = plt.figure(1)
    plot1 = plt.plot(trainingPoints, targetValues, "o")
    plot2 = plt.plot(testPoints, resultPoints)
    plt.show()
示例#6
0
文件: KRR.py 项目: zousss/PDA_Book
from mlpy import KernelRidge
import matplotlib.pyplot as plt

np.random.seed(10)

targetValues = np.genfromtxt("Gold.csv",
                     skip_header=1,
                     dtype=None,
                     delimiter=',',
                     usecols=(1))

trainingPoints = np.arange(125).reshape(-1, 1)
testPoints = np.arange(126).reshape(-1, 1)

#training kernel matrix
knl = mlpy.kernel_gaussian(trainingPoints, trainingPoints, sigma=1)
#testing kernel matrix
knlTest = mlpy.kernel_gaussian(testPoints, trainingPoints, sigma=1)

knlRidge = KernelRidge(lmb=0.01, kernel=None)
knlRidge.learn(knl, targetValues)
resultPoints = knlRidge.pred(knlTest)

print(resultPoints)

fig = plt.figure(1)
plot1 = plt.plot(trainingPoints, targetValues, 'o')
plot2 = plt.plot(testPoints, resultPoints)
plt.show()