order ={"0":"Open","1":"High","2":"Low","3":"Close","4":"Volume*e-6"} yahooData = np.load('yahooData.npy') Adj_Close,High,Low,Close,Open = np.hsplit(yahooData,5) Volume = Adj_Close yahooData = np.hstack([Open,High,Low,Close,Volume]) shape = yahooData.shape print "shape = ",shape print "-"*80 # print yahooData[0:10,:] print "Close shape = ",Close.shape myNNmodel = MyNeurolNetworkModel() kdj = myNNmodel.calculate_kdj(yahooData) #(None,3) print "kdj = ",kdj.shape declose = myNNmodel.calculate_dclose(Close) #(None,4) print "declose = ",declose.shape logfit = myNNmodel.calculate_logfit(Close) #(None,1) print "logfit = ",logfit.shape closeExponent = myNNmodel.calculate_exponent(Close,exponent = 0.9) #(None,1) print "closeExponent = ",closeExponent.shape from sklearn.decomposition import PCA pca = PCA(n_components = 2)
# x_train = x_sample[train_start:train_end,:] x_train = np.vstack([x_sample[0:100,:],x_sample[300:500,:],x_sample[700:900,:],x_sample[1100:1150,:]]) y_train = np.vstack([y_sample[0:100,:],y_sample[300:500,:],y_sample[700:900,:],y_sample[1100:1150,:]]) print "x_train.shape = ",x_train.shape sample_number = x_train.shape[0] test_start = 1150 test_end = 1200 y_test = y_sample[test_start:test_end,:] x_test = x_sample[test_start:test_end,:] outdir = os.path.join(os.path.dirname(__file__),'stockClose/') myNNmodel = MyNeurolNetworkModel() # myNNmodel.outdir = outdir myNNmodel.errorRate = 0.0105 myNNmodel.learningRate = 0.001 print "myNNmodel outdir = ",myNNmodel.outdir if not os.path.exists(outdir): os.mkdir(outdir) # myNNmodel.train(x_train,y_train) print "layerOne = ",myNNmodel.layerOne print "myNNmodel train successfully ..." y_test_predict = myNNmodel.predict(x_test) from matplotlib import pyplot as plt
# x_train = x_sample[train_start:train_end,:] x_train = np.vstack([x_sample[0:100,:],x_sample[300:500,:],x_sample[700:900,:],x_sample[1100:1150,:]]) y_train = np.vstack([y_sample[0:100,:],y_sample[300:500,:],y_sample[700:900,:],y_sample[1100:1150,:]]) print "x_train.shape = ",x_train.shape sample_number = x_train.shape[0] test_start = 1150 test_end = 1200 y_test = y_sample[test_start:test_end,:] x_test = x_sample[test_start:test_end,:] outdir = os.path.join(os.path.dirname(__file__),'stock_close/') myNNmodel = MyNeurolNetworkModel() # myNNmodel.outdir = outdir myNNmodel.errorRate = 0.0105 myNNmodel.learningRate = 0.001 print "myNNmodel outdir = ",myNNmodel.outdir if not os.path.exists(outdir): os.mkdir(outdir) # myNNmodel.train(x_train,y_train) print "myNNmodel train successfully ..." y_test_predict = myNNmodel.predict(x_test)
y_train = np.vstack([ y_sample[0:100, :], y_sample[300:500, :], y_sample[700:900, :], y_sample[1100:1150, :] ]) print "x_train.shape = ", x_train.shape sample_number = x_train.shape[0] test_start = 1150 test_end = 1200 y_test = y_sample[test_start:test_end, :] x_test = x_sample[test_start:test_end, :] outdir = os.path.join(os.path.dirname(__file__), 'stock_close/') myNNmodel = MyNeurolNetworkModel() # myNNmodel.outdir = outdir myNNmodel.errorRate = 0.0105 myNNmodel.learningRate = 0.001 print "myNNmodel outdir = ", myNNmodel.outdir if not os.path.exists(outdir): os.mkdir(outdir) # myNNmodel.train(x_train,y_train) print "myNNmodel train successfully ..." y_test_predict = myNNmodel.predict(x_test) from matplotlib import pyplot as plt
print "ysample = ",ysample.shape # indexList = np.random.permutation(shape[0]) indexList = range(shape[0]) x_train = xsample[indexList[0:538]] y_train = ysample[indexList[0:538]] print "x_train.shape = ",x_train.shape print "y_train.shape = ",y_train.shape x_test = xsample[indexList[538:]] y_test = ysample[indexList[538:]] print "x_test.shape = ",x_test.shape print "y_test.shape = ",y_test.shape myNNmodel = MyNeurolNetworkModel() myNNmodel.errorRate = 0.918 myNNmodel.layerOne = 15 myNNmodel.learningRate = 0.001 myNNmodel.trainTimes = 4000 # myNNmodel.batchSize = 20 y_predict = myNNmodel.predict(x_test) print y_predict[0:10] np.save('./npyfile/y_predict1',y_predict) result = myNNmodel.f_measure(y_predict,y_test) print "result = ",result
y_sample = np.zeros((shape[0]-related,1)) for i in xrange(shape[0] - related): x_sample[i,:] = closeArray[i:i+related,0].reshape(1,related) y_sample[i,0] = closeArray[i+related,0] train_start = 600 train_end = 1150 y_train = y_sample[train_start:train_end,:] x_train = x_sample[train_start:train_end,:] test_start = 1000 test_end = 1200 y_test = y_sample[test_start:test_end,:] x_test = x_sample[test_start:test_end,:] test_start_1 = 400 test_end_1 = 600 x_test_1 = x_sample[test_start:test_end,:] y_test_1 = y_sample[test_start:test_end,:] mYnnModel = MyNeurolNetworkModel() mYnnModel.errorRate = 0.040 if is_train == 1: mYnnModel.train(x_train,y_train) return crossDomainResponse({"code":200,"msg":"ok"}) else: y_predict = mYnnModel.predict(x_test) return crossDomainResponse({"code":200,"msg":"ok","y_test":y_test.tolist(),"y_predict":y_predict.tolist()})
print "ysample = ",ysample.shape # indexList = np.random.permutation(shape[0]) indexList = range(shape[0]) x_train = xsample[indexList[0:538]] y_train = ysample[indexList[0:538]] print "x_train.shape = ",x_train.shape print "y_train.shape = ",y_train.shape x_test = xsample[indexList[538:]] y_test = ysample[indexList[538:]] print "x_test.shape = ",x_test.shape print "y_test.shape = ",y_test.shape myNNmodel = MyNeurolNetworkModel() myNNmodel.errorRate = 0.918 myNNmodel.layerOne = 15 myNNmodel.isDropout = True myNNmodel.learningRate = 0.001 myNNmodel.trainTimes = 4000 # myNNmodel.train(x_train,y_train) ''' y_predict = myNNmodel.predict(x_train) print y_predict[0:10] for i in xrange(y_predict.shape[0]): if y_predict[i] >= 0.5: y_predict[i] = 1
columns = np.hsplit(dataset, 9) xsample = np.hstack(columns[0:8]) ysample = columns[8] shape = xsample.shape print "xsample = ", xsample.shape print "ysample = ", ysample.shape # indexList = np.random.permutation(shape[0]) indexList = range(shape[0]) x_train = xsample[indexList[0:538]] y_train = ysample[indexList[0:538]] print "x_train.shape = ", x_train.shape print "y_train.shape = ", y_train.shape x_test = xsample[indexList[538:]] y_test = ysample[indexList[538:]] print "x_test.shape = ", x_test.shape print "y_test.shape = ", y_test.shape myNNmodel = MyNeurolNetworkModel() myNNmodel.errorRate = 0.918 myNNmodel.layerOne = 15 myNNmodel.isDropout = True myNNmodel.learningRate = 0.001 myNNmodel.trainTimes = 7000 myNNmodel.train(x_train, y_train) print "train model successfully!"
columns = np.hsplit(dataset,9) xsample = np.hstack(columns[0:8]) ysample = columns[8] shape = xsample.shape print "xsample = ",xsample.shape print "ysample = ",ysample.shape # indexList = np.random.permutation(shape[0]) indexList = range(shape[0]) x_train = xsample[indexList[0:538]] y_train = ysample[indexList[0:538]] print "x_train.shape = ",x_train.shape print "y_train.shape = ",y_train.shape x_test = xsample[indexList[538:]] y_test = ysample[indexList[538:]] print "x_test.shape = ",x_test.shape print "y_test.shape = ",y_test.shape myNNmodel = MyNeurolNetworkModel() myNNmodel.errorRate = 0.918 myNNmodel.layerOne = 15 myNNmodel.isDropout = True myNNmodel.learningRate = 0.001 myNNmodel.trainTimes = 7000 myNNmodel.train(x_train,y_train) print "train model successfully!"
#!usr/bin/env/python # -*- coding: utf-8 -*- import numpy as np import os from myNeurolNetworkModel import MyNeurolNetworkModel order = {"0": "Open", "1": "High", "2": "Low", "3": "Close", "4": "Volume*e-6"} yahooData = np.load('yahoo_finance5.npy') Open, High, Low, Close, Volume = np.hsplit(yahooData, 5) shape = yahooData.shape print("shape = ", shape) print("-" * 80) print("Close shape = ", Close.shape) myNNmodel = MyNeurolNetworkModel() kdj = myNNmodel.calculate_kdj(yahooData) #(None,3) print("kdj = ", kdj.shape) declose = myNNmodel.calculate_dclose(Close) #(None,4) print("declose = ", declose.shape) logfit = myNNmodel.calculate_logfit(Close) #(None,1) print("logfit = ", logfit.shape) closeExponent = myNNmodel.calculate_exponent(Close, exponent=0.9) #(None,1) print("closeExponent = ", closeExponent.shape) from sklearn.decomposition import PCA pca = PCA(n_components=2) newData = pca.fit_transform(np.hstack([Open, High, Low, Volume])) #(None,2)
import os from myNeurolNetworkModel import MyNeurolNetworkModel ''' 每天的五个数据 high,low,close,open,adj_close, ''' order = {"0": "Open", "1": "High", "2": "Low", "3": "Close", "4": "Volume*e-6"} yahooData = np.load('yahoo_finance5.npy') Open, High, Low, Close, Volume = np.hsplit(yahooData, 5) shape = yahooData.shape print "shape = ", shape print "-" * 80 print "Close shape = ", Close.shape myNNmodel = MyNeurolNetworkModel() kdj = myNNmodel.calculate_kdj(yahooData) print "kdj = ", kdj.shape declose = myNNmodel.calculate_dclose(Close) print "declose = ", declose.shape logfit = myNNmodel.calculate_logfit(Close) print "logfit = ", logfit.shape closeExponent = myNNmodel.calculate_exponent(Close) print "closeExponent = ", closeExponent.shape # x_sample = np.hstack([Open,High,Low,Volume,kdj,logfit,closeExponent]) x_sample = np.hstack([Open, kdj, logfit, closeExponent]) y_sample = Close[1:]
train_start = 600 train_end = 1150 y_train = y_sample[train_start:train_end, :] x_train = x_sample[train_start:train_end, :] test_start = 1000 test_end = 1200 y_test = y_sample[test_start:test_end, :] x_test = x_sample[test_start:test_end, :] test_start_1 = 400 test_end_1 = 600 x_test_1 = x_sample[test_start:test_end, :] y_test_1 = y_sample[test_start:test_end, :] mYnnModel = MyNeurolNetworkModel() mYnnModel.errorRate = 0.040 if is_train == 1: mYnnModel.train(x_train, y_train) return crossDomainResponse({"code": 200, "msg": "ok"}) else: y_predict = mYnnModel.predict(x_test) return crossDomainResponse({ "code": 200, "msg": "ok", "y_test": y_test.tolist(), "y_predict": y_predict.tolist() })
xsample = np.hstack(columns[0:8]) ysample = columns[8] shape = xsample.shape print "xsample = ",xsample.shape print "ysample = ",ysample.shape # indexList = np.random.permutation(shape[0]) indexList = range(shape[0]) x_train = xsample[indexList[0:538]] y_train = ysample[indexList[0:538]] print "x_train.shape = ",x_train.shape print "y_train.shape = ",y_train.shape x_test = xsample[indexList[538:]] y_test = ysample[indexList[538:]] print "x_test.shape = ",x_test.shape print "y_test.shape = ",y_test.shape myNNmodel = MyNeurolNetworkModel() myNNmodel.errorRate = 0.918 myNNmodel.layerOne = 15 myNNmodel.learningRate = 0.001 myNNmodel.trainTimes = 7000 # myNNmodel.batchSize = 20 myNNmodel.train(x_train,y_train,x_test,y_test) print "train model successfully!"
y_sample[1100:1250, :] ]) print "x_train.shape = ", x_train.shape sample_number = x_train.shape[0] test_start = 900 test_end = 1100 y_test = y_sample[test_start:test_end, :] x_test = x_sample[test_start:test_end, :] outdir = './images/' if not os.path.exists(outdir): os.mkdir(outdir) myNNmodel = MyNeurolNetworkModel() myNNmodel.inputNumber = 4 # myNNmodel.outdir = outdir myNNmodel.errorRate = 0.01111 myNNmodel.learningRate = 0.001 # myNNmodel.train(x_train,y_train) print "myNNmodel train successfully ..." y_test_predict = myNNmodel.predict(x_test) from matplotlib import pyplot as plt plt.plot(y_test, 'ro') plt.plot(y_test_predict, 'bo') plt.plot(y_test, 'r-')
columns = np.hsplit(dataset, 9) xsample = np.hstack(columns[0:8]) ysample = columns[8] shape = xsample.shape print "xsample = ", xsample.shape print "ysample = ", ysample.shape # indexList = np.random.permutation(shape[0]) indexList = range(shape[0]) x_train = xsample[indexList[0:538]] y_train = ysample[indexList[0:538]] print "x_train.shape = ", x_train.shape print "y_train.shape = ", y_train.shape x_test = xsample[indexList[538:]] y_test = ysample[indexList[538:]] print "x_test.shape = ", x_test.shape print "y_test.shape = ", y_test.shape myNNmodel = MyNeurolNetworkModel() myNNmodel.errorRate = 0.918 myNNmodel.layerOne = 15 myNNmodel.learningRate = 0.001 myNNmodel.trainTimes = 7000 # myNNmodel.batchSize = 20 myNNmodel.train(x_train, y_train, x_test, y_test) print "train model successfully!"
#!usr/bin/env/python # -*- coding: utf-8 -*- import numpy as np import os from myNeurolNetworkModel import MyNeurolNetworkModel order ={"0":"Open","1":"High","2":"Low","3":"Close","4":"Volume*e-6"} yahooData = np.load('yahoo_finance5.npy') Open,High,Low,Close,Volume = np.hsplit(yahooData,5) shape = yahooData.shape print("shape = ",shape) print("-"*80) print("Close shape = ",Close.shape) myNNmodel = MyNeurolNetworkModel() kdj = myNNmodel.calculate_kdj(yahooData) #(None,3) print("kdj = ",kdj.shape) declose = myNNmodel.calculate_dclose(Close) #(None,4) print("declose = ",declose.shape) logfit = myNNmodel.calculate_logfit(Close) #(None,1) print("logfit = ",logfit.shape) closeExponent = myNNmodel.calculate_exponent(Close,exponent = 0.9) #(None,1) print("closeExponent = ",closeExponent.shape) from sklearn.decomposition import PCA pca = PCA(n_components = 2)
#!usr/bin/env/python # -*- coding: utf-8 -*- import numpy as np import os from myNeurolNetworkModel import MyNeurolNetworkModel myNNmodel = MyNeurolNetworkModel() v = myNNmodel.random_vector(10,100) print("v = ",v.shape) yahooData = np.load('yahoo_finance5.npy') print("-"*100) sample = yahooData[v] print("sample.shape = ",sample.shape)