【问题标题】:Input contains NaN, infinity or a value too large for dtype('float64') but there are no nan or infinite values输入包含 NaN、无穷大或对于 dtype('float64') 而言太大的值,但没有 nan 或无穷大值
【发布时间】:2019-10-11 22:25:29
【问题描述】:

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

这是我使用 scikit 库运行逻辑回归代码时出现的错误。

我已尝试删除 nan 和无限值,但它不起作用

    df=pd.read_csv("bots.csv")
    df1=pd.read_csv("genuine.csv")

    df1.head

    np.where(df.values >= np.finfo(np.float64).max)
    np.where(df1.values >= np.finfo(np.float64).max)

    np.any(np.isnan(df))
    np.any(np.isnan(df1))

    np.all(np.isfinite(df))
    np.all(np.isfinite(df1))

    df1=df1[:-92]  #drop from bottom

    f_to_f_human=df['friend_to_folowers_ratio']
    f_to_f_bot=df1['friend_to_folowers_ratio']

    df1['Y']= 1 #1 for bot
    df['Y'] = 0 # 0 for human

    vx=df['Y']
    vy=df1['Y']

    A = pd.concat([df1, df])
    A

    y=A.iloc[:,-1].values
    X=A.drop(['Y'], axis=1)
    X=A.iloc[:].values
    X_train,X_test,y_train, y_test= 
    train_test_split(X,y,test_size=0.2,random_state=42)

    model=LogisticRegression(penalty='l2',C=1)
    print(X_train)
    model.fit(X_train,y_train)

我预计不会出现任何错误,但会出现错误

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

【问题讨论】:

  • 你有A = pd.concat([df1, df])A 是必须检查 nanNA 的数据帧。如果我没记错的话,连接两个数据帧可能会产生 NA 值,因为 Pandas 使用公共索引值对齐数据。
  • 我认为这是您提到的问题,但 concat 没有创建任何 NA 值。感谢您的帮助
  • X_train.isnull().sum() 返回什么?

标签: python machine-learning scikit-learn data-science


【解决方案1】:

尝试将此添加到您的代码中:

    A = pd.concat([df1, df])
    A.dropna(inplace=True)

    y=A.iloc[:,-1].values
    X=A.drop(['Y'], axis=1)
    X=A.iloc[:].values

    X_train,X_test,y_train, y_test= 
    train_test_split(X,y,test_size=0.2,random_state=42)

    model=LogisticRegression(penalty='l2',C=1)
    print(X_train)
    model.fit(X_train,y_train)

A.dropna(inplace=True) 应该删除所有的 NaN 值

另一个提示是: A.dtypesdo 检查您的列有哪种格式

【讨论】:

    猜你喜欢
    • 2019-06-01
    • 2019-03-26
    • 2017-02-19
    • 2016-03-25
    • 2017-11-23
    • 2019-05-30
    • 2017-09-06
    • 2018-02-12
    • 2018-11-15
    相关资源
    最近更新 更多