请解释pre_org = np.array(pre) * (max(y) - min(y)) + min(y) y_test_org = np.array(y_test) * (max(y) - min(y)) + min(y) errors_std_org = np.std(pre_org - y_test_org)

时间: 2023-05-28 08:01:34 浏览: 59
这几行代码的作用是将模型预测的结果还原成原始数据的值域,并计算预测误差的标准差。 首先,pre_org = np.array(pre) * (max(y) - min(y)) / min(y) 将模型预测的结果pre乘以目标变量y的值域范围(即最大值max(y)减去最小值min(y))并除以最小值min(y)。这样做是因为在训练模型时,需要将标签值(即y)归一化到同一范围内,这样才能避免不同特征之间的数值差异影响模型的训练。 同样地,y_test_org = np.array(y_test) * (max(y) - min(y)) / min(y) 将测试数据集的目标变量y_test也还原成原始数据的值域。 最后,errors_std_org = np.std(pre_org - y_test_org) 计算预测误差的标准差。这里的误差是指模型预测结果与实际目标变量之间的差值。通过计算误差的标准差,可以对模型的预测能力进行评估。标准差越小,说明模型越稳定,预测能力越强。
相关问题

请解释代码pre_org = np.array(pre) * (max(y) - min(y)) + min(y)

这段代码的作用是将数组pre中的每个元素乘以y轴数据的最大值和最小值之差,并将结果存储在新的数组pre_org中。同时,变量min(y)存储了y轴数据的最小值。 这段代码的目的可能是将预测值pre的范围缩放到与y轴数据相同的范围内,便于对两者进行比较或绘制图表。如果不进行缩放,则当pre与y的取值范围不同时,难以进行有效的数据分析和可视化。

years = np.arange(1979, 2020, 1) year_str = [str(i) for i in years] x_min = 70 x_max = 140 y_min = 10 y_max = 55 grid_lon = np.arange(x_min, x_max+1, 1.0) grid_lat = np.arange(y_min, y_max+1, 1.0) nyears = years.shape[0] nlon = grid_lon.shape[0] nlat = grid_lat.shape[0] total_grid_data = np.zeros((nyears, nlat, nlon), dtype=float) i = 0 for year in year_str: f_path = r'E:\gra_thesis\sum_pre_data_new/AMJ_pre_data/'+year+'_AMJ_sumPre.xlsx' df = pd.read_excel(f_path) df = df.dropna(axis=0) lon = df['X'] lat = df['Y'] data= df['总降水量'] Krin = pykrige.OrdinaryKriging(lon, lat, data, variogram_model="gaussian", nlags=6) grid_data, ssl = Krin.execute('grid', grid_lon, grid_lat) grid_data = np.array(grid_data) total_grid_data[i, :, :] = grid_data i = i + 1 # %% # # save data data_nc = xr.Dataset( { "precip":(("time", "lat","lon"), total_grid_data) }, coords={ "time":years, "lat":grid_lat, "lon":grid_lon, } ) data_nc.attrs["long_name"] = "total_grid_precip" data_nc.to_netcdf("E:"+"JJA_pre_total_precip.nc") 解释这段代码i

这段代码是用于读取地理数据,并对其进行插值,得到一个三维数组。years变量存储了年份序列,year_str变量是将年份序列转换为字符串序列。grid_lon变量和grid_lat变量分别是经度和纬度序列。nyears、nlon和nlat是计算数组维度的变量。total_grid_data是三维数组,用于存储地理数据的插值结果。代码使用了pykrige包进行插值,其中OrdinaryKriging()函数是调用普通克里金插值方法进行计算,variogram_model指定了变异函数的类型,nlags是变异函数的参数。for循环逐个读取每个年份的数据,同时将插值结果存储在total_grid_data中。最终代码的结果是得到了一个三维数组,其中每个元素值是地理数据的插值结果。

相关推荐

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);我需要将归一化的数据变成真实值,输出对比图,该怎么修改程序

import numpy as np import pandas as pd import matplotlib.pyplot as plt df=pd.read_csv('C:\\Users\ASUS\Desktop\AI\实训\汽车销量数据new.csv',sep=',',header=0) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.figure(figsize=(10,4)) ax1=plt.subplot(121) ax1.scatter(df['price'],df['quantity'],c='b') df=(df-df.min())/(df.max()-df.min()) df.to_csv('quantity.txt',sep='\t',index=False) train_data=df.sample(frac=0.8,replace=False) test_data=df.drop(train_data.index) x_train=train_data['price'].values.reshape(-1, 1) y_train=train_data['quantity'].values x_test=test_data['price'].values.reshape(-1, 1) y_test=test_data['quantity'].values from sklearn.linear_model import LinearRegression import joblib #model=SGDRegressor(max_iter=500,learning_rate='constant',eta0=0.01) model = LinearRegression() #训练模型 model.fit(x_train,y_train) #输出训练结果 pre_score=model.score(x_train,y_train) print('训练集准确性得分=',pre_score) print('coef=',model.coef_,'intercept=',model.intercept_) #保存训练后的模型 joblib.dump(model,'LinearRegression.model') ax2=plt.subplot(122) ax2.scatter(x_train,y_train,label='测试集') ax2.plot(x_train,model.predict(x_train),color='blue') ax2.set_xlabel('工龄') ax2.set_ylabel('工资') plt.legend(loc='upper left') model=joblib.load('LinearRegression.model') y_pred=model.predict(x_test)#得到预测值 print('测试集准确性得分=%.5f'%model.score(x_test,y_test)) #计算测试集的损失(用均方差) MSE=np.mean((y_test - y_pred)**2) print('损失MSE={:.5f}'.format(MSE)) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.figure(figsize=(10,4)) ax1=plt.subplot(121) plt.scatter(x_test,y_test,label='测试集') plt.plot(x_test,y_pred,'r',label='预测回归线') ax1.set_xlabel('工龄') ax1.set_ylabel('工资') plt.legend(loc='upper left') ax2=plt.subplot(122) x=range(0,len(y_test)) plt.plot(x,y_test,'g',label='真实值') plt.plot(x,y_pred,'r',label='预测值') ax2.set_xlabel('样本序号') ax2.set_ylabel('工资') plt.legend(loc='upper right') plt.show()怎么预测价格为15万时的销量

最新推荐

recommend-type

####这是一篇对python的详细解析

python
recommend-type

菜日常菜日常菜日常菜日常

菜日常菜日常菜日常菜日常
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB柱状图在数据分析中的作用:从可视化到洞察

![MATLAB柱状图在数据分析中的作用:从可视化到洞察](https://img-blog.csdnimg.cn/img_convert/1a36558cefc0339f7836cca7680c0aef.png) # 1. MATLAB柱状图概述** 柱状图是一种广泛用于数据可视化的图表类型,它使用垂直条形来表示数据中不同类别或组别的值。在MATLAB中,柱状图通过`bar`函数创建,该函数接受数据向量或矩阵作为输入,并生成相应的高度条形。 柱状图的优点在于其简单性和易于理解性。它们可以快速有效地传达数据分布和组别之间的比较。此外,MATLAB提供了广泛的定制选项,允许用户调整条形颜色、
recommend-type

命名ACL和拓展ACL标准ACL的具体区别

命名ACL和标准ACL的主要区别在于匹配条件和作用范围。命名ACL可以基于协议、端口和其他条件进行匹配,并可以应用到接口、VLAN和其他范围。而标准ACL只能基于源地址进行匹配,并只能应用到接口。拓展ACL则可以基于源地址、目的地址、协议、端口和其他条件进行匹配,并可以应用到接口、VLAN和其他范围。