logo
Loading...

為何會出現error : '<' not supported between instances of 'float' and 'str'此錯誤 - Cupoy

想請問將1改為2為什麼會出現錯誤?1.  if len(list(app_train[col].un...

ml100-2,column,資料型態,ml100-2-d04

為何會出現error : '<' not supported between instances of 'float' and 'str'此錯誤

2019/04/24 下午 07:31
機器學習共學討論版
達明翰
觀看數:117
回答數:5
收藏數:0
ml100-2
column
資料型態
ml100-2-d04

想請問將1改為2為什麼會出現錯誤?


1.  if len(list(app_train[col].unique())) 

2.  (app_train[col].nunique()) 

>error : '<' not supported between instances of 'float' and 'str'

回答列表

  • 2019/04/24 下午 10:47
    白學群
    贊同數:0
    不贊同數:0
    留言數:0

    您好:


    照您貼上來的訊息來解讀,應該是您有什麼數字變成字串,導致show出Error Message

    記得要做運算前,要確認是否都是數字喔!!

    >error : '<' not supported between instances of 'float' and 'str'


    方便的話您把程式貼上來大家一起討論更方便


    希望有幫助到您

    ^^

  • 2019/04/24 下午 11:56
    張維元 (WeiYuan)
    贊同數:0
    不贊同數:0
    留言數:0

    看起來 nunique 是拼錯字,不過另外也附上前後文才比較好知道錯誤原因

  • 2019/04/25 上午 09:56
    達明翰
    贊同數:0
    不贊同數:0
    留言數:0

    # Create a label encoder object

    le = LabelEncoder()

    le_count = 0


    # Iterate through the columnsfor col in app_train:

    #type(col) : strif app_train[col].dtype == 'object':

    # If 2 or fewer unique categories

    if len(list(app_train[col].unique())) <= 2:  # (app_train[col].nunique())

       

        # Train on the training datale.fit(app_train[col])

        # Transform both training and testing data

        app_train[col] = le.transform(app_train[col])

        app_test[col] = le.transform(app_test[col])

        # Keep track of how many columns were label 

        encodedle_count += 1

    print('%d columns were label encoded.' % le_count)app_train.head()

  • 2019/04/25 上午 10:02
    達明翰
    贊同數:0
    不贊同數:0
    留言數:0

    謝謝回覆~

    我的問題是1. len(list(app_train[col].unique())) 與2. app_train[col].nunique() 應該有相同的int 型態的輸出

    但將1 改為2 後會出現Error,想請問一下這之間有什麼不同嗎?

  • 2019/04/25 下午 01:00
    張維元 (WeiYuan)
    贊同數:0
    不贊同數:0
    留言數:2

    喔喔!你可以看一下官方文件:


    * pandas.unique: returns 

    • If the input is an Index, the return is an Index
    • If the input is a Categorical dtype, the return is a Categorical
    • If the input is a Series/ndarray, the return will be an ndarray

    * pandas.DataFrame.nunique: returns

    • nunique: Series


    他們回傳的資料型態不同哦,一種是 ndarray,一種是 series