logo
Loading...

day006作業發問:請問要怎麼表達數據表格資料中的橫軸呢? - Cupoy

以下是我看完https://medium.com/@contactsunny/label-encod...

ml100-3,ml100-3-d06

day006作業發問:請問要怎麼表達數據表格資料中的橫軸呢?

2019/09/02 上午 00:31
機器學習共學討論版
侯秉宏
觀看數:39
回答數:2
收藏數:0
ml100-3
ml100-3-d06

以下是我看完https://medium.com/@contactsunny/label-encoder-vs-one-hot-encoder-in-machine-learning-3fc273365621所打的程式


from sklearn.preprocessing import LabelEncoder 

a=LabelEncoder() 

x[:,1]=a.fit_transform(x[:,1])


我想先將數據轉換成array再轉換成one hot encoding(即是上列網址內容),我打出程式後顯示無法識別x,請問要怎麼表達數據表格資料中的橫軸呢?


NameError                                 Traceback (most recent call last)
<ipython-input-35-ac0d0176d262> in <module>
      1 from sklearn.preprocessing import LabelEncoder
      2 a=LabelEncoder()
----> 3 x[:,1]=a.fit_transform(x[:,1])

NameError: name 'x' is not defined

回答列表

  • 2019/09/02 上午 10:01
    張維元 (WeiYuan)
    贊同數:1
    不贊同數:0
    留言數:1

    嗨,你的程式中有 x 嗎?這錯誤訊息是指沒有定義 x 哦!

  • 2019/09/03 下午 02:28
    張維元 (WeiYuan)
    贊同數:1
    不贊同數:0
    留言數:0

    您好我知道x沒有被定義,但是我想將x定義成表格的橫軸,請問如何做到?


    => 如果有一個 df 長這樣:


    ```

    d = {'col1': [1, 2], 'col2': [3, 4]}

    df = pd.DataFrame(data=d)

    df

    #    col1  col2

    # 0     1     3

    # 1     2     4

    ```


    可以用以下的方式取出橫軸的資料:


    ```

    df.loc[0]

    # col1    1

    # col2    3

    # Name: 0, dtype: int64

    ```


    ```

    df[0:1]

    #    col1  col2

    # 0     1     3

    ```


    再根據你的需求指定資料給 x 即可:)