输入包含NaN,无穷大或dtype('float64')太大的值,但没有nan或无穷大值

时间:2019-05-25 18:11:39

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

  

输入内容包含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')太大的值

1 个答案:

答案 0 :(得分: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.dtypes检查您的列采用哪种格式

相关问题