SVR单变量单输出,并额外预测一期

预测油价,单变量单输出并输出后一个月的油价,一行代表一个月`

# -*- coding: utf-8 -*-
# @Time : 2022/11/3 18:32
# @Author : Tiger大队队长
# @File : SVR.py
import numpy as np
import pylab as plt
import pandas as pd
from sklearn.metrics import mean_squared_error
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
import matplotlib.pyplot as plt
import tensorboard
import datetime
from sklearn.metrics import mean_squared_error
import tensorflow as tf
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import GridSearchCV


def scale_inv(raw_data, data1, scale=False):
    data1 = data1.tolist()
    if scale == True:
        return raw_data * np.std(data1) + np.mean(data1)
    else:
        return raw_data * (np.max(data1) - np.min(data1)) + np.min(data1)


def data_processing(raw_data, scale=False):
    if scale == True:
        return (raw_data - np.mean(raw_data)) / np.std(raw_data)  # 标准化
    else:
        return (raw_data - np.min(raw_data)) / (np.max(raw_data) - np.min(raw_data))  # 极差规格化


pred = []
df = pd.read_csv(r'1.csv', encoding='gbk')  # 导入数据
result2 = []
result3 = []
data = df.iloc[:, 0].tolist()
data = np.array(data)
num = data.shape[0]
num0 = round(num * 0.8)
result = []
test_y = []
time_step = 6

# ------------------------------------------------------------------------------
for a in range(num0, num + 1):
    data_train = data[0:a]
    result1 = []
    predict1 = []

    train_num = a
    predict_day = 1
    data_train1 = (data_train - np.min(data_train)) / (np.max(data_train) - np.min(data_train))
    feature_data = data_train1.ravel()
    #         print('feature_data:',feature_data)
    #         print('time_step:',time_step)
    #         print('train_num + predict_day',train_num + predict_day)
    label_data = feature_data[time_step: train_num + predict_day]

    train_x = [feature_data[i:i + time_step] for i in range(train_num - time_step)]
    train_y = [label_data[i:i + predict_day] for i in range(train_num - time_step)]

    trainx = pd.DataFrame(train_x)
    trainy = pd.DataFrame(train_y)
    feature_data0 = feature_data[train_num - time_step:]  # 测试集数据
    label_data = feature_data[train_num:]

    testX = [feature_data0[i:i + time_step] for i in range(1)]
    testy = [label_data[i:i + predict_day] for i in range(1)]
    testX = pd.DataFrame(testX)
    testy = pd.DataFrame(testy)

    # SVR
    from sklearn.svm import SVR

    lower_q = np.quantile(trainx, 0.25, interpolation='lower')  # 下四分位数
    higher_q = np.quantile(trainx, 0.75, interpolation='higher')  # 上四分位数
    int_r = higher_q - lower_q  # 四分位距
    int_r = int_r / 13.49

    mlp_clf__tuned_parameters = {"kernel": ['rbf'], "tol": [1e-3], "C": [1.5], "degree": [3, 4], "coef0": [1.0, 2.0],
                                 "epsilon": [int_r], "shrinking": [True], "cache_size": [200, 300], "verbose": [False],
                                 "max_iter": [-1]}

    mlp = SVR()
    # 参数优化
    estimator = GridSearchCV(mlp, mlp_clf__tuned_parameters, n_jobs=5, cv=10, scoring='neg_mean_squared_error')
    estimator.fit(trainx, trainy)
    yhat = estimator.predict(testX)
    #         print(estimator.best_params_)   #参数输出
    yhat = yhat * (np.max(data_train) - np.min(data_train)) + np.min(data_train)  # 反归一化
    predict1.append(yhat)
    result1 = np.sum(predict1)
    result.append(result1)

test_y = data[num0:]
print(test_y)
result1 = result[0:num - num0]
print(result1)
result3.append(result1)
mape = np.abs((test_y - result1) / test_y).mean()
tmp = 0
for loop in range(1, test_y.size):
    if ((result1[loop - 1] - test_y[loop - 1]) * (test_y[loop] - test_y[loop - 1]) >= 0):
        tmp = tmp + 1;
Dstat = tmp / (test_y.size - 1)
print("mape is", mape)
print("Dstat is", Dstat)
print("下一期数据是", result[len(result) - 1])

你可能感兴趣的:(机器学习,python,tensorflow)