用Python做一个主成分分析加lstm模型,预测销售额,以及可视化损失值与正确率
时间: 2023-07-18 11:06:13 浏览: 218
基于LSTM的单特征和多特征预测python
5星 · 资源好评率100%
好的,这里是一个简单的示例代码来完成这个任务。首先,我们需要导入所需的库。
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
```
然后,我们可以读取数据并进行主成分分析。
```python
# 读取数据
data = pd.read_csv('sales_data.csv')
# 提取特征和目标变量
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
# 特征缩放
scaler = MinMaxScaler()
X = scaler.fit_transform(X)
# 主成分分析
pca = PCA(n_components=5)
X = pca.fit_transform(X)
```
接下来,我们可以将数据划分为训练集和测试集,并将其转换为LSTM所需的形状。
```python
# 划分训练集和测试集
train_size = int(len(X) * 0.8)
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]
# 将数据转换为LSTM所需的形状
n_features = X_train.shape[1]
n_steps = 3
X_train = np.array([X_train[i:i+n_steps, :] for i in range(len(X_train)-n_steps+1)])
y_train = y_train[n_steps-1:]
X_test = np.array([X_test[i:i+n_steps, :] for i in range(len(X_test)-n_steps+1)])
y_test = y_test[n_steps-1:]
```
然后,我们可以构建LSTM模型并训练它。
```python
# 构建LSTM模型
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# 训练模型
history = model.fit(X_train, y_train, epochs=100, validation_data=(X_test, y_test), verbose=0)
```
最后,我们可以绘制损失值和正确率的可视化图表。
```python
# 绘制损失值和正确率的可视化图表
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='test')
plt.title('Loss')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(history.history['accuracy'], label='train')
plt.plot(history.history['val_accuracy'], label='test')
plt.title('Accuracy')
plt.legend()
plt.show()
```
完整代码如下:
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
# 读取数据
data = pd.read_csv('sales_data.csv')
# 提取特征和目标变量
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
# 特征缩放
scaler = MinMaxScaler()
X = scaler.fit_transform(X)
# 主成分分析
pca = PCA(n_components=5)
X = pca.fit_transform(X)
# 划分训练集和测试集
train_size = int(len(X) * 0.8)
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]
# 将数据转换为LSTM所需的形状
n_features = X_train.shape[1]
n_steps = 3
X_train = np.array([X_train[i:i+n_steps, :] for i in range(len(X_train)-n_steps+1)])
y_train = y_train[n_steps-1:]
X_test = np.array([X_test[i:i+n_steps, :] for i in range(len(X_test)-n_steps+1)])
y_test = y_test[n_steps-1:]
# 构建LSTM模型
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# 训练模型
history = model.fit(X_train, y_train, epochs=100, validation_data=(X_test, y_test), verbose=0)
# 绘制损失值和正确率的可视化图表
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='test')
plt.title('Loss')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(history.history['accuracy'], label='train')
plt.plot(history.history['val_accuracy'], label='test')
plt.title('Accuracy')
plt.legend()
plt.show()
```
阅读全文