data = pd.read_csv(r'C:/Users/Ljimmy/Desktop/yyqc/peijian/销量数据副本3.csv', index_col=0, parse_dates=True) data.index = pd.to_datetime(data.index, unit='s') data = data.dropna() # 绘制原始时间序列图 plt.plot(data) plt.title('Original Time Series') plt.show(),增加代码,输出平滑后的效果图
时间: 2024-02-09 14:09:37 浏览: 153
在对时间序列进行平稳化处理后,可以使用以下代码输出平滑后的效果图:
```python
# 对时间序列进行平稳化处理
predictions_diff = pd.Series(data['y'].values - data['y'].shift(1).values, index=data.index).dropna()
predictions_diff_cumsum = predictions_diff.cumsum()
predictions = pd.Series(data['y'].iloc[0], index=data.index)
predictions = predictions.add(predictions_diff_cumsum, fill_value=0)
# 绘制平滑后的时间序列图
plt.plot(predictions)
plt.title('Smoothed Time Series')
plt.show()
```
这段代码使用了差分的方法对时间序列进行平稳化处理,然后对平稳化后的时间序列进行绘图。注意,这里使用了 `data['y']` 来获取时间序列中的数值,因此需要确保数据集中存在名为 'y' 的列。
相关问题
import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential from keras.layers import Dense, LSTM from sklearn.metrics import r2_score,median_absolute_error,mean_absolute_error # 读取数据 data = pd.read_csv(r'C:/Users/Ljimmy/Desktop/yyqc/peijian/销量数据rnn.csv') # 取出特征参数 X = data.iloc[:,2:].values # 数据归一化 scaler = MinMaxScaler(feature_range=(0, 1)) X[:, 0] = scaler.fit_transform(X[:, 0].reshape(-1, 1)).flatten() #X = scaler.fit_transform(X) #scaler.fit(X) #X = scaler.transform(X) # 划分训练集和测试集 train_size = int(len(X) * 0.8) test_size = len(X) - train_size train, test = X[0:train_size, :], X[train_size:len(X), :] # 转换为监督学习问题 def create_dataset(dataset, look_back=1): X, Y = [], [] for i in range(len(dataset) - look_back - 1): a = dataset[i:(i + look_back), :] X.append(a) Y.append(dataset[i + look_back, 0]) return np.array(X), np.array(Y) look_back = 12 X_train, Y_train = create_dataset(train, look_back) #Y_train = train[:, 2:] # 取第三列及以后的数据 X_test, Y_test = create_dataset(test, look_back) #Y_test = test[:, 2:] # 取第三列及以后的数据 # 转换为3D张量 X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1)) X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1)) # 构建LSTM模型 model = Sequential() model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1))) model.add(LSTM(units=50)) model.add(Dense(units=1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(X_train, Y_train, epochs=5, batch_size=32) #model.fit(X_train, Y_train.reshape(Y_train.shape[0], 1), epochs=10, batch_size=32) # 预测下一个月的销量 last_month_sales = data.tail(12).iloc[:,2:].values #last_month_sales = data.tail(1)[:,2:].values last_month_sales = scaler.transform(last_month_sales) last_month_sales = np.reshape(last_month_sales, (1, look_back, 1)) next_month_sales = model.predict(last_month_sales) next_month_sales = scaler.inverse_transform(next_month_sales) print('Next month sales: %.0f' % next_month_sales[0][0]) # 计算RMSE误差 rmse = np.sqrt(np.mean((next_month_sales - last_month_sales) ** 2)) print('Test RMSE: %.3f' % rmse)IndexError Traceback (most recent call last) Cell In[1], line 36 33 X_test, Y_test = create_dataset(test, look_back) 34 #Y_test = test[:, 2:] # 取第三列及以后的数据 35 # 转换为3D张量 ---> 36 X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1)) 37 X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1)) 38 # 构建LSTM模型 IndexError: tuple index out of range代码修改
在代码中,X_train 和 X_test 的维度为 (样本数量,时间步长),需要将其转换为 (样本数量,时间步长,特征数量) 的形式。因此在创建数据集时,需要将数据 reshape 为 (样本数量,时间步长,1),即每个时间步长只有一个特征。代码如下:
```
def create_dataset(dataset, look_back=1):
X, Y = [], []
for i in range(len(dataset) - look_back - 1):
a = dataset[i:(i + look_back), :]
X.append(a.reshape(-1, 1))
Y.append(dataset[i + look_back, 0])
return np.array(X), np.array(Y)
```
在训练和测试数据集转换为3D张量时,需要保证第三个维度为特征数量。因此需要修改如下代码:
```
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
```
修改后的完整代码如下:
```
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM
from sklearn.metrics import r2_score,median_absolute_error,mean_absolute_error
# 读取数据
data = pd.read_csv(r'C:/Users/Ljimmy/Desktop/yyqc/peijian/销量数据rnn.csv')
# 取出特征参数
X = data.iloc[:,2:].values
# 数据归一化
scaler = MinMaxScaler(feature_range=(0, 1))
X = scaler.fit_transform(X)
# 划分训练集和测试集
train_size = int(len(X) * 0.8)
test_size = len(X) - train_size
train, test = X[0:train_size, :], X[train_size:len(X), :]
# 转换为监督学习问题
def create_dataset(dataset, look_back=1):
X, Y = [], []
for i in range(len(dataset) - look_back - 1):
a = dataset[i:(i + look_back), :]
X.append(a.reshape(-1, 1))
Y.append(dataset[i + look_back, 0])
return np.array(X), np.array(Y)
look_back = 12
X_train, Y_train = create_dataset(train, look_back)
X_test, Y_test = create_dataset(test, look_back)
# 转换为3D张量
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
# 构建LSTM模型
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1)))
model.add(LSTM(units=50))
model.add(Dense(units=1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, Y_train, epochs=5, batch_size=32)
# 预测下一个月的销量
last_month_sales = data.tail(12).iloc[:,2:].values
last_month_sales = scaler.transform(last_month_sales)
last_month_sales = np.reshape(last_month_sales, (1, look_back, 1))
next_month_sales = model.predict(last_month_sales)
next_month_sales = scaler.inverse_transform(next_month_sales)
print('Next month sales: %.0f' % next_month_sales[0][0])
# 计算RMSE误差
rmse = np.sqrt(np.mean((next_month_sales - last_month_sales) ** 2))
print('Test RMSE: %.3f' % rmse)
```
import numpy as np import pandas as pd import tensorflow as tf from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import r2_score,median_absolute_error,mean_absolute_error # 读取数据 data = pd.read_csv(r'C:/Users/Ljimmy/Desktop/yyqc/peijian/销量数据rnn.csv') dataset = data.values # 数据归一化 scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(dataset) # 分割训练集和测试集 train_size = int(len(dataset) * 0.67) test_size = len(dataset) - train_size train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :] # 将数据集转化为适合GRU的数据格式 def create_dataset(dataset): X, Y = [], [] for i in range(len(dataset)-1): a = dataset[i:(i+1), :] X.append(a) Y.append(dataset[i+1, :]) return np.array(X), np.array(Y) train_X, train_Y = create_dataset(train) train_Y = train_Y[:, 2:] # 取第三列及以后的数据 test_X, test_Y = create_dataset(test) test_Y = test_Y[:, 2:] # 取第三列及以后的数据 # 定义GRU模型 model = tf.keras.Sequential([ tf.keras.layers.GRU(units=64, return_sequences=True, input_shape=(1, 3)), tf.keras.layers.GRU(units=32), tf.keras.layers.Dense(3)]) # 编译模型 model.compile(optimizer='adam', loss='mse') # 训练模型 model.fit(train_X, train_Y, epochs=100, batch_size=16, verbose=2) # 预测测试集 test_predict = model.predict(test_X) test_predict = scaler.inverse_transform(test_predict) #test_Y = scaler.inverse_transform(test_Y.reshape(-1, 1)) # 计算RMSE误差 rmse = np.sqrt(np.mean((test_predict - test_Y) ** 2)) print('Test RMSE:',rmse) # 预测下一个月的销量 last_month_sales = data.tail(1).values last_month_sales = scaler.transform(last_month_sales) next_month_sales = model.predict(np.array([last_month_sales])) next_month_sales = scaler.inverse_transform(next_month_sales) print('Next month sales:',next_month_sales[0][0])预测结果不够准确,如何增加准确率
有以下几种方式可以尝试提高准确率:
1. 增加训练数据量:可以尝试寻找更多的数据来进行训练,以提高模型的泛化能力和准确率。
2. 调整模型参数:可以尝试调整GRU模型的参数,如增加或减少GRU层的数量、增加或减少神经元的数量等,以寻找最优的模型参数组合。
3. 对数据进行更详细的分析:可以对原始数据进行更详细的分析,找到影响销量的因素,并将这些因素作为额外的特征加入到模型中,以提高模型的准确率。
4. 尝试其他类型的模型:可以尝试使用其他类型的深度学习模型,如LSTM、CNN等,以寻找更适合该数据集的模型。
阅读全文