python - expm1 中遇到溢出
问题描述
以下代码来自 Kaggle House Price Prediction 的参考笔记本:
X=train_df.drop(['SalePrice'],axis=1)
y=train_df.SalePrice
X_pwr=power_transformer.fit_transform(X)
test_std=std_scaler.fit_transform(test_df)
test_rbst=rbst_scaler.fit_transform(test_df)
test_pwr=power_transformer.fit_transform(test_df)
gb_reg = GradientBoostingRegressor(n_estimators=1792,
learning_rate=0.01005, max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=14, loss='huber', random_state =42)
gb_reg.fit(X_pwr, y)
y_head=gb_reg.predict(X_test)
test_pred_gb=gb_reg.predict(test_pwr)
test_pred_gb=pd.DataFrame(test_pred_gb,columns=['SalePrice'])
test_pred_gb.SalePrice =np.floor(np.expm1(test_pred_gb.SalePrice))
sample_sub.iloc[:,1]=(0.5 * test_pred_gb.iloc[:,0])+(0.5 *
old_prediction.iloc[:,1])
#here old_prediction is the sample prediction given by kaggle
我想知道最后一行代码的原因。为什么他们分配预测值的指数。此外,最后一行给出运行时警告:expm1 中遇到溢出。我也想知道如何解决这个溢出问题,因为在这一步之后,所有的 SalePrice 都被 Nan 取代
解决方案
对于第一个问题,如果不看更多代码就很难说,尽管我怀疑有充分的理由,因为您提供的数字np.expm1
显然很大(如果它们是房屋的销售价格,这是有道理的)。这让我想到了第二个问题:
expm1
是计算的特殊功能exp(x) - 1
。与仅x
使用exp(x) - 1
. 我不知道 numpy 执行计算的确切方式,尽管通常它是用泰勒级数完成的。您从泰勒级数开始,exp(x)
只需将初始项 1 移到另一侧,即可得到exp(x) - 1 =
一个大的多项式和。这个多项式包含 x^n 和 n! 其中 n 是多项式所采用的项数(即精度级别)。对于 large x
,数字很快就会变得笨拙!换句话说,您很快就接近了在您的操作系统上可以用比特表示的数字的极限。要显示这一点,只需尝试以下操作:
import numpy as np
import warnings
warnings.filterwarnings('error')
for i in range(200000):
try:
np.expm1(i)
except Warning:
print(i)
break
在我的系统上,打印710
. 作为一种解决方法,您可以尝试将大量数字缩小(即 200,000 美元的价格实际上是 0.2 兆美元)。
推荐阅读
- python - 使用带注释的字段执行 groupby 计数的问题
- node.js - cloudinary -base 64 图像上传给出无效的 JSON 响应
- javascript - 仅当条件为 True 时才在 python 中使用 Eel 调用 JavaScript 函数
- javascript - 帧运动退出动画不适用于 Next.JS
- android - 有没有办法在第一次实例化后使用 Hilt Injection 重新创建房间数据库?
- mysql - 我可以在 MySQL 中为复合/多列索引设置多顺序索引吗?
- sql - ora-00932 不一致的数据类型预期数字得到 char plsql
- vscode-settings - VS 代码找不到 -lgcc 文件
- javascript - 如何在laravel中使用javascript在没有预览对话框的情况下打印
- python - 遍历一个列表并找到所有属性的 ID: