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
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.layers import Dropout, Dense, SimpleRNN
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)
len=3
result_x=df1[0:len].values.flatten()
result_y=[]
for i in range(1,df1.shape[0]-len):
sub=np.mat(df1[i:i+len].values.flatten())
result_x=np.vstack((result_x,sub))
for i in range(0, df1.shape[0] - len):
rate=(df1['close'][i+len]-df1['close'][i+len-1])/df1['close'][i+len-1]
result_y.append(rate)
x=result_x.A
y=np.array(result_y)
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%
# 归一化
sc = MinMaxScaler(feature_range=(0, 1)) # 定义归一化:归一化到(0,1)之间
training_set_scaled = sc.fit_transform(X_train) # 求得训练集的最大值,最小值这些训练集固有的属性,并在训练集上进行归一化
test_set = sc.transform(X_test) # 利用训练集的属性对测试集进行归一化
print(test_set.shape)
# 归一化
sc_y = MinMaxScaler(feature_range=(0, 1)) # 定义归一化:归一化到(0,1)之间
training_set_scaled_y = sc_y.fit_transform(y_train.reshape(-1,1)) # 求得训练集的最大值,最小值这些训练集固有的属性,并在训练集上进行归一化
test_set_y = sc_y.transform(y_test.reshape(-1,1)) # 利用训练集的属性对测试集进行归一化
# model=tf.keras.Sequential()
# model.add(layers.Dense(42,input_shape=(df1.shape
股票预测 day4 过拟合
最新推荐文章于 2023-02-10 21:34:56 发布