【问题标题】:Linear Regression ValueError: Input contains NaN, infinity or a value too large for dtype('float64')线性回归 ValueError:输入包含 NaN、无穷大或对于 dtype('float64') 来说太大的值
【发布时间】:2022-04-05 22:47:23
【问题描述】:

我正在研究线性回归模型,但出现错误:

ValueError:输入包含 NaN、无穷大或对于 dtype('float64') 来说太大的值

这是我的代码:

    ### List Column Data Types for df

    # Convert "Paid' column to float64 by first changing NaN to 0
    Training_Data['Paid'].fillna(0).astype(float)

    # Convert 'Sale Price' column to float64 by first changing NaN to 0
    #print(df.loc[pd.to_numeric(df['Sale Price'], errors='coerce').isnull()])
    #pd.to_numeric(df['Sale Price']).astype(int)
    Training_Data["Sale Price"] = Training_Data["Sale 
    Price"].astype(str).str.strip().replace("",0).astype(float)

    # List Data Types
    Training_Data.dtypes

返回:Paid float64 Sale Price float64 dtype: object

    ### List Column Data Types for df2

    # Convert "Paid' column to float64 by first changing NaN to 0
    Test_Data['Paid'].fillna(0).astype(float)

    # Convert 'Sale Price' column to float64 by first changing NaN to 0
    #print(df.loc[pd.to_numeric(df['Sale Price'], errors='coerce').isnull()])
    #pd.to_numeric(df['Sale Price']).astype(int)
    Test_Data["Sale Price"] = Test_Data["Sale 
    Price"].astype(str).str.strip().replace("",0).astype(float)

    # List Data Types
    Test_Data.dtypes

返回:Paid float64 Sale Price float64 dtype: object

    ### Declare and Drop Dependent (Measured) Variable

    SourceData_train_independent = Training_Data.drop(['Sale Price'], axis = 1) # 
    Drop depedent variable from training dataset

    SourceData_train_dependent = Training_Data['Sale Price'].copy() # New dataframe 
    with only Dependent variable value for training dataset

    SourceData_test_independent = Test_Data.drop(['Sale Price'], axis = 1)

    SourceData_test_dependent = Test_Data['Sale Price'].copy()

    SourceData_train_independent.dtypes

返回:付费 float64 dtype: object

    ### Scaling Independent Train and Test Variable

    sc_X = StandardScaler()

    X_train = sc_X.fit_transform(SourceData_train_independent.values) #scale the 
    independent variables

    y_train = SourceData_train_dependent # scaling is not required for dependent 
    variable

    X_test = sc_X.transform(SourceData_test_independent)

    y_test = SourceData_test_dependent

最后,当我运行时:

    ### Feeding Train Data

    reg = LinearRegression().fit(X_train, y_train)
    print("The Linear regression score on training data is ", 
    round(reg.score(X_train, y_train),2))

我得到了错误。所以我认为我的文件仍然有 NaN 值,我认为我已经更正了。任何人都可以帮忙吗?谢谢!

【问题讨论】:

  • 您需要将fillna的结果保存回Test_Data数据框Test_Data['Paid'] = Test_Data['Paid'].fillna(0).astype(float)
  • 你也可以像print(Test_Data['Paid'].isnull().any())这样在你的代码中进行检查,以确保你已经删除了空值
  • 即使在第二个代码块中调整该行后,我仍然收到错误
  • 仔细检查x_trainy_train 没有空值?类似np.isnan?
  • 您的转换有问题。我不明白你为什么需要使用 .strip() 方法。如果您在问题中提供数据框,则更容易

标签: python pandas numpy linear-regression sklearn-pandas


【解决方案1】:

试试这个

def check_nan_inf(df):
    for col in df.columns:
        if df[col].isnull().any():
            print(col, 'has nan')
        if np.isinf(df[col]).any():
            print(col, 'has inf')

【讨论】:

    猜你喜欢
    • 2017-11-23
    • 2016-07-31
    • 2020-08-16
    • 2020-05-25
    • 2019-02-17
    • 2020-10-23
    • 1970-01-01
    • 2023-01-11
    • 2019-08-25
    相关资源
    最近更新 更多