python实现横坐标是数据点的编号(0,1,...,N)代码
时间: 2023-10-03 15:07:25 浏览: 198
以下是一个简单的Python代码实现,用于在横坐标上显示数据点的编号:
```python
import matplotlib.pyplot as plt
# 生成一些示例数据
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
# 将横坐标设置为数据点的编号
plt.plot(range(len(x)), y)
# 添加横坐标标签
plt.xlabel('Data Point Number')
# 显示图形
plt.show()
```
在这个例子中,我们使用`range(len(x))`生成一个从0到N的整数序列,其中N是数据点的数量。这个序列被用作横坐标,而数据点的纵坐标则是`y`列表中对应位置的值。最后,我们添加了横坐标标签,并使用`plt.show()`显示图形。
相关问题
写一段python代码,读取一个csv文件,该文件没有表头,第一列为时间编号,编号以1,2,3……这样自然数的形式出现,第二列为充电量数值,将该数据集的前90%划分为训练集,其余部分设置为测试集,利用L1/L2正则化改进的lstm模型预测该时间序列,并打印rmse作为评价指标,并分别绘制训练集的真实值和预测值图片以及测试集的真实值和预测值图片,两张图片的横坐标单位为time/h,纵坐标单位为kwh
好的,下面是代码实现,需要安装pandas、numpy、matplotlib、sklearn、tensorflow等库:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
from tensorflow.keras import regularizers
# 读取csv文件
data = pd.read_csv('data.csv', header=None)
values = data.values[:, 1].astype('float32')
# 数据归一化
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values.reshape(-1, 1))
# 划分训练集和测试集
train_size = int(len(scaled) * 0.9)
train, test = scaled[:train_size, :], scaled[train_size:, :]
# 将序列数据转换为监督学习数据
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
n_vars = 1 if type(data) is list else data.shape[1]
df = pd.DataFrame(data)
cols, names = [], []
for i in range(n_in, 0, -1):
cols.append(df.shift(i))
names += [('var%d(t-%d)' % (j + 1, i)) for j in range(n_vars)]
for i in range(0, n_out):
cols.append(df.shift(-i))
if i == 0:
names += [('var%d(t)' % (j + 1)) for j in range(n_vars)]
else:
names += [('var%d(t+%d)' % (j + 1, i)) for j in range(n_vars)]
agg = pd.concat(cols, axis=1)
agg.columns = names
if dropnan:
agg.dropna(inplace=True)
return agg
n_lag = 3
n_features = 1
reframed = series_to_supervised(scaled, n_lag, 1)
# 划分输入和输出
values = reframed.values
train_X, train_y = values[:train_size, :-1], values[:train_size, -1]
test_X, test_y = values[train_size:, :-1], values[train_size:, -1]
# 转换成3D格式[Lag, Samples, Features]
train_X = train_X.reshape((train_X.shape[0], n_lag, n_features))
test_X = test_X.reshape((test_X.shape[0], n_lag, n_features))
# 构建LSTM模型
model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2]),
kernel_regularizer=regularizers.l1_l2(l1=0.01, l2=0.01)))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
# 训练模型
history = model.fit(train_X, train_y, epochs=50, batch_size=72,
validation_data=(test_X, test_y), verbose=2, shuffle=False)
# 预测测试集
yhat = model.predict(test_X)
test_X = test_X.reshape((test_X.shape[0], n_lag * n_features))
# 反归一化
inv_yhat = scaler.inverse_transform(yhat)
inv_y = scaler.inverse_transform(test_y.reshape(-1, 1))
# 计算rmse
rmse = np.sqrt(np.mean((inv_y - inv_yhat)**2))
print('Test RMSE: %.3f' % rmse)
# 绘制训练集和测试集预测结果
train_predict = model.predict(train_X)
train_predict = scaler.inverse_transform(train_predict)
train_actual = scaler.inverse_transform([train_y])
test_predict = scaler.inverse_transform(yhat)
test_actual = scaler.inverse_transform(test_y.reshape(-1, 1))
plt.figure(figsize=(10, 6))
plt.subplot(2, 1, 1)
plt.plot(train_actual.flatten(), label='Train Actual')
plt.plot(train_predict.flatten(), label='Train Predict')
plt.legend()
plt.subplot(2, 1, 2)
plt.plot(test_actual.flatten(), label='Test Actual')
plt.plot(test_predict.flatten(), label='Test Predict')
plt.legend()
plt.show()
```
注:该代码适用于只有一列数据的csv文件,如果有多列数据需要进行修改。
其中,L1正则化和L2正则化分别通过`regularizers.l1_l2(l1=0.01, l2=0.01)`实现,参数`l1`和`l2`分别表示L1正则化和L2正则化的权重。RMSE是评价指标,通过`np.sqrt(np.mean((inv_y - inv_yhat)**2))`计算,其中`inv_y`和`inv_yhat`分别表示真实值和预测值的反归一化结果。训练集和测试集的真实值和预测值图片通过matplotlib库绘制。
写一段python代码,读取一个csv文件,该文件的第一列为时间编号,编号以1,2,3……这样自然数的形式出现,第二列为充电量数值,将该数据集的前70%划分为训练集,其余部分设置为测试集,将该数据集所构成的时间序列进行EMD分解,分解出的子序列中低频的部分用svr模型进行训练,高频的部分用lstm模型进行训练,然后用stacking方法的集成学习对总体进行训练,并对总序列进行预测,并打印rmse作为评价指标,并分别绘制训练集的真实值和预测值图片以及测试集的真实值和预测值图片,两张图片的横坐标单位为time/h,纵坐标单位为kwh
# 导入需要的库
import pandas as pd
import numpy as np
from PyEMD import EMD
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
# 读取csv文件
df = pd.read_csv('data.csv')
# 划分训练集和测试集
train_size = int(len(df) * 0.7)
train, test = df.values[:train_size, :], df.values[train_size:, :]
# 对时间序列进行EMD分解
emd = EMD()
imfs = emd(df.iloc[:, 1].values)
# 分离出低频部分和高频部分
low_freq = imfs[0]
high_freq = imfs[1:]
# 划分训练集和测试集
train_low, test_low = low_freq[:train_size], low_freq[train_size:]
train_high, test_high = high_freq[:, :train_size], high_freq[:, train_size:]
# 对低频部分进行SVR模型训练
scaler = StandardScaler()
train_low_scaled = scaler.fit_transform(train_low.reshape(-1, 1))
test_low_scaled = scaler.transform(test_low.reshape(-1, 1))
svr = SVR(kernel='rbf', C=100, gamma=0.1, epsilon=.1)
svr.fit(train_low_scaled, train[:, 1])
# 对高频部分进行LSTM模型训练
train_high = train_high.T
test_high = test_high.T
def create_lstm_model(train_X, train_y, n_steps, n_features):
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.fit(train_X, train_y, epochs=200, verbose=0)
return model
n_steps = 3
n_features = len(high_freq)
train_X, train_y = [], []
test_X, test_y = [], []
for i in range(n_steps, len(train_high)):
train_X.append(train_high[i-n_steps:i, :])
train_y.append(train[i, 1])
for i in range(n_steps, len(test_high)):
test_X.append(test_high[i-n_steps:i, :])
test_y.append(test[i, 1])
train_X, train_y = np.array(train_X), np.array(train_y)
test_X, test_y = np.array(test_X), np.array(test_y)
lstm = create_lstm_model(train_X, train_y, n_steps, n_features)
# 进行集成学习
def stacked_dataset(train_X, train_y, test_X):
stackX = None
for model in [svr, lstm]:
model.fit(train_X, train_y)
yhat = model.predict(test_X)
if stackX is None:
stackX = yhat
else:
stackX = np.column_stack((stackX, yhat))
return stackX
stacked_train_X = stacked_dataset(train_high.T, train[:, 1], train_X.reshape((train_X.shape[0], n_steps*n_features)))
stacked_test_X = stacked_dataset(test_high.T, test[:, 1], test_X.reshape((test_X.shape[0], n_steps*n_features)))
model = SVR(kernel='linear')
model.fit(stacked_train_X, train[:, 1])
yhat = model.predict(stacked_test_X)
print('RMSE:', np.sqrt(mean_squared_error(test[:, 1], yhat)))
# 绘制训练集和测试集的真实值和预测值图片
plt.plot(train[:, 0], train[:, 1], label='True Train')
plt.plot(train[:, 0], svr.predict(train_low_scaled), label='Predicted Train - SVR')
plt.plot(train[:, 0][n_steps:], lstm.predict(train_X).flatten(), label='Predicted Train - LSTM')
plt.plot(test[:, 0], test[:, 1], label='True Test')
plt.plot(test[:, 0], yhat, label='Predicted Test - Stacking')
plt.xlabel('Time/h')
plt.ylabel('kwh')
plt.legend()
plt.show()
阅读全文