熵坎 2022-04-13 16:41 采纳率: 100%
浏览 264
已结题

在pycharm中ols拟合出现nan,inf

在预测碳排放量时,我使用ols模型,结果出现很多nan,inf

代码

import numpy as np
import pandas as pd
#载入数据
excel_path = r"Testingdata_shiyou.xlsx"
data_df = pd.read_excel(excel_path)
# 给列名重新命名,每一列就是一个自(因)变量的观测值
data_df.columns = ['year', 'I', 'P', 'A', 'Te', 'Ts', 'Td', 'Tc']
# 量纲恢复

# 数据标准化
def normalize(data):
    """对因变量作标准化变换,标准化因子是标准差"""
    return (data - data.mean()) / data.std()
#%%
import statsmodels.api as sm
X = np.log(data_df.iloc[:, 2:])
def mask_nan(X,value):
    '''
    用value值来代替nan 或inf
    '''
    X_values= tf.add(tf.zeros_like(X),value)
    mask = tf.math.is_finite(X)
    y = tf.where(mask,X,X_values)
    return y

y = np.log(data_df.iloc[:, 1])
X = sm.add_constant(X)
ols_model = sm.OLS(y,X).fit()
print(ols_model.summary())

结果

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      I   R-squared:                       1.000
Model:                            OLS   Adj. R-squared:                    nan
Method:                 Least Squares   F-statistic:                       nan
Date:                Wed, 13 Apr 2022   Prob (F-statistic):                nan
Time:                        16:29:49   Log-Likelihood:                 148.10
No. Observations:                   5   AIC:                            -286.2
Df Residuals:                       0   BIC:                            -288.2
Df Model:                           4                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         -0.0678        inf         -0        nan         nan         nan
P              0.9989        inf          0        nan         nan         nan
A              0.9892        inf          0        nan         nan         nan
Te             0.9672        inf          0        nan         nan         nan
Ts            -0.2405        inf         -0        nan         nan         nan
Td             0.0003        inf          0        nan         nan         nan
Tc             0.9808        inf          0        nan         nan         nan
==============================================================================
Omnibus:                          nan   Durbin-Watson:                   0.035
Prob(Omnibus):                    nan   Jarque-Bera (JB):                0.362
Skew:                          -0.201   Prob(JB):                        0.834
Kurtosis:                       1.745   Cond. No.                         631.
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The input rank is higher than the number of observations.


我有试过这位的定义mask函数,但结果没变化https://blog.csdn.net/qq_25737169/article/details/78847691?spm=1001.2101.3001.6650.5&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-5.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-5.pc_relevant_default&utm_relevant_index=10

想请各位帮忙看看如何解决结果里的nan和inf

  • 写回答

4条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 4月28日
      • 已采纳回答 4月20日
      • 赞助了问题酬金5元 4月13日
      • 创建了问题 4月13日

      悬赏问题

      • ¥15 如何在linux服务器做视频静态资源访问接口
      • ¥15 Python Xpath对<em>标签的爬取结果变成了- -(关键词-Xpath)
      • ¥15 el-cascader懒加载模式选中单选框加载下层无数据问题
      • ¥15 promql 参数怎么忽视大小写查询
      • ¥50 swift使用textkit读取大文本文件速度慢的问题
      • ¥20 统信linux360企业浏览器 如何配置扩展程序?(图形界面我知道)
      • ¥100 c/c++实现DoubleArrayTrie
      • ¥15 在开发/迭代一款企业信息软件时会耗费你们多长时间?
      • ¥30 关于#网络安全#的问题:非对称加密验证
      • ¥20 关于线性代数里施密特正交化和QR分解的疑问