pd.DataFrame(y_test)
时间: 2023-11-03 07:23:24 浏览: 54
I'm sorry, as an AI language model, I don't have access to any specific data or code. Please provide more information and context so that I can assist you better.
相关问题
import numpy as np import pandas as pd import matplotlib.pyplot as plt import BPNN from sklearn import metrics from sklearn.metrics import mean_absolute_error from sklearn.metrics import mean_squared_error #导入必要的库 df1=pd.read_excel(r'D:\Users\Desktop\大数据\44.xls',0) df1=df1.iloc[:,:] #进行数据归一化 from sklearn import preprocessing min_max_scaler = preprocessing.MinMaxScaler() df0=min_max_scaler.fit_transform(df1) df = pd.DataFrame(df0, columns=df1.columns) x=df.iloc[:,:4] y=df.iloc[:,-1] #划分训练集测试集 cut=4#取最后cut=30天为测试集 x_train, x_test=x.iloc[4:],x.iloc[:4]#列表的切片操作,X.iloc[0:2400,0:7]即为1-2400行,1-7列 y_train, y_test=y.iloc[4:],y.iloc[:4] x_train, x_test=x_train.values, x_test.values y_train, y_test=y_train.values, y_test.values #神经网络搭建 bp1 = BPNN.BPNNRegression([4, 16, 1]) train_data=[[sx.reshape(4,1),sy.reshape(1,1)] for sx,sy in zip(x_train,y_train)] test_data = [np.reshape(sx,(4,1))for sx in x_test] #神经网络训练 bp1.MSGD(train_data, 1000, len(train_data), 0.2) #神经网络预测 y_predict=bp1.predict(test_data) y_pre = np.array(y_predict) # 列表转数组 y_pre=y_pre.reshape(4,1) y_pre=y_pre[:,0] #画图 #展示在测试集上的表现 draw=pd.concat([pd.DataFrame(y_test),pd.DataFrame(y_pre)],axis=1); draw.iloc[:,0].plot(figsize=(12,6)) draw.iloc[:,1].plot(figsize=(12,6)) plt.legend(('real', 'predict'),loc='upper right',fontsize='15') plt.title("Test Data",fontsize='30') #添加标题 #输出精度指标 print('测试集上的MAE/MSE') print(mean_absolute_error(y_pre, y_test)) print(mean_squared_error(y_pre, y_test) ) mape = np.mean(np.abs((y_pre-y_test)/(y_test)))*100 print('=============mape==============') print(mape,'%') # 画出真实数据和预测数据的对比曲线图 print("R2 = ",metrics.r2_score(y_test, y_pre)) # R2 运行上述程序。在下面这一步中draw=pd.concat([pd.DataFrame(y_test),pd.DataFrame(y_pre)],axis=1);我需要将归一化的数据变成真实值,输出对比图,该怎么修改程序
在将归一化的数据变回真实值的过程中,需要将归一化的因变量(即最后一列)逆转换回来,以得到真实的预测值和真实值。代码如下:
```
# 将预测结果和真实结果逆归一化
y_predict = min_max_scaler.inverse_transform(y_pre.reshape(-1, 1))
y_test = min_max_scaler.inverse_transform(y_test.reshape(-1, 1))
# 画图
draw=pd.concat([pd.DataFrame(y_test), pd.DataFrame(y_predict)], axis=1)
draw.iloc[:,0].plot(figsize=(12,6))
draw.iloc[:,1].plot(figsize=(12,6))
plt.legend(('real', 'predict'), loc='upper right', fontsize='15')
plt.title("Test Data", fontsize='30')
# 输出精度指标
print('测试集上的MAE/MSE')
print(mean_absolute_error(y_predict, y_test))
print(mean_squared_error(y_predict, y_test))
mape = np.mean(np.abs((y_predict-y_test)/(y_test)))*100
print('=============mape==============')
print(mape,'%')
# 输出R2分数
print("R2 = ", metrics.r2_score(y_test, y_predict))
```
在此代码中,我们使用了 MinMaxScaler 对象的 inverse_transform() 方法将归一化的结果转换回原始的数据范围。运行修改后的代码,即可得到真实值与预测值的对比图,以及各种精度指标的输出。
逐行解释这段代码 column = list(average.columns) data = average.loc[:, column[0]:column[-3]] # 自变量 target = average.loc[:, ['TIMEsurvival', 'EVENTdeath']] for i in range(1, 101): X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.3, random_state=i) # feature = Spearman(X_train, 0.85) #spearman第一行 # feature = list(feature['feature']) #spearman第二行 # X_train = X_train.loc[:, feature] #spearman第三行 train_index = X_train.index train_column = X_train.columns zscore_scaler = preprocessing.StandardScaler() X_train = zscore_scaler.fit_transform(X_train) X_train = pd.DataFrame(X_train, index=train_index, columns=train_column) # X_test = X_test.loc[:, feature] #spearman第四行 test_index = X_test.index test_column = X_test.columns X_test = zscore_scaler.transform(X_test) X_test = pd.DataFrame(X_test, index=test_index, columns=test_column) train = pd.concat([X_train, y_train], axis=1)
这段代码主要是对数据进行预处理和分割,具体解释如下:
1. `column = list(average.columns)`:将 `average` 数据的列名转换成列表形式,并赋值给 `column`。
2. `data = average.loc[:, column[0]:column[-3]]`:从 `average` 数据中选取所有行和 `column[0]` 到 `column[-3]` 列的数据,赋值给 `data`。这里的 `column[-3]` 表示从最后一列开始往前数第三列。
3. `target = average.loc[:, ['TIMEsurvival', 'EVENTdeath']]`:从 `average` 数据中选取所有行和 `TIMEsurvival'` 以及 `'EVENTdeath'` 两列的数据,赋值给 `target`。这里的 `TIMEsurvival` 表示存活时间,`EVENTdeath` 表示是否死亡。
4. `for i in range(1, 101):`:循环 100 次,每次循环都进行一次数据分割和预处理的操作。
5. `X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.3, random_state=i)`:使用 `train_test_split` 方法将 `data` 和 `target` 数据集分别划分为训练集和测试集,其中测试集占 30%,`random_state=i` 表示每次随机划分的结果都是相同的,以保证实验结果可重复。
6. `train_index = X_train.index` 和 `train_column = X_train.columns`:将训练集中的行和列名分别赋值给 `train_index` 和 `train_column` 变量。
7. `zscore_scaler = preprocessing.StandardScaler()`:实例化 `StandardScaler` 类,即进行 Z-score 标准化的对象。
8. `X_train = zscore_scaler.fit_transform(X_train)`:对训练集进行 Z-score 标准化处理。
9. `X_train = pd.DataFrame(X_train, index=train_index, columns=train_column)`:将标准化后的训练集数据转换为 DataFrame 格式,并将行和列名分别设置为 `train_index` 和 `train_column`。
10. `test_index = X_test.index` 和 `test_column = X_test.columns`:将测试集中的行和列名分别赋值给 `test_index` 和 `test_column` 变量。
11. `X_test = zscore_scaler.transform(X_test)`:对测试集进行 Z-score 标准化处理。
12. `X_test = pd.DataFrame(X_test, index=test_index, columns=test_column)`:将标准化后的测试集数据转换为 DataFrame 格式,并将行和列名分别设置为 `test_index` 和 `test_column`。
13. `train = pd.concat([X_train, y_train], axis=1)`:将标准化后的训练集数据和目标变量 `y_train` 沿列方向合并,形成新的训练集 `train`。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)