做神经网络 逻辑回归
以30天为一个周期进行处理数据
神经网络输入的数据是numpy
loss有点大 明天再看
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVR,SVR
import pandas as pd
import tensorflow as tf
from tensorflow.keras import layers
df2=pd.read_csv('F:\\股票预测code\\hs300_stock_predict-master\\000895.csv',encoding='UTF-8')
df1=df2.iloc[:,1:]
print(df1.head())
print(df1.shape)
print(df1.shape[1])
len=30
result=df1[0:len].values.flatten()
for i in range(1,df1.shape[0]-1-len):
sub=np.mat(df1[i:i+len].values.flatten())
result=np.vstack((result,sub))
x=result.A
y=df1['close'][len:df1.shape[0]-1].to_numpy()
print(type(x))
print(type(y))
print(y)
print(x.shape)
print(y.shape)
X_train,X_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=666)#利用train_test_split进行将训练集和测试集进行分开,test_size占20%
model=tf.keras.Sequential()
model.add(layers.Dense(420,input_shape=(420,),activation='relu'))
model.add(layers.Dense(420,activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(210,activation='relu'))
model.add(layers.Dense(210,activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(105,activation='relu'))
model.add(layers.Dense(105,activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(1,activation='relu'))
model.summary()
model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
model.fit(X_train,y_train,epochs=15,validation_data=(X_test,y_test))