亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

pandas使用get_dummies進(jìn)行one-hot編碼的方法

 更新時(shí)間:2018年07月10日 10:21:23   作者:BYR_jiandong  
今天小編就為大家分享一篇pandas使用get_dummies進(jìn)行one-hot編碼的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

離散特征的編碼分為兩種情況:

1、離散特征的取值之間沒有大小的意義,比如color:[red,blue],那么就使用one-hot編碼

2、離散特征的取值有大小的意義,比如size:[X,XL,XXL],那么就使用數(shù)值的映射{X:1,XL:2,XXL:3}

使用pandas可以很方便的對(duì)離散型特征進(jìn)行one-hot編碼

import pandas as pd
df = pd.DataFrame([
   ['green', 'M', 10.1, 'class1'], 
   ['red', 'L', 13.5, 'class2'], 
   ['blue', 'XL', 15.3, 'class1']])
 
df.columns = ['color', 'size', 'prize', 'class label']
 
size_mapping = {
   'XL': 3,
   'L': 2,
   'M': 1}
df['size'] = df['size'].map(size_mapping)
 
class_mapping = {label:idx for idx,label in enumerate(set(df['class label']))}
df['class label'] = df['class label'].map(class_mapping)

說明:對(duì)于有大小意義的離散特征,直接使用映射就可以了,{'XL':3,'L':2,'M':1}

Using the get_dummies will create a new column for every unique string in a certain column:使用get_dummies進(jìn)行one-hot編碼
pd.get_dummies(df)

以上這篇pandas使用get_dummies進(jìn)行one-hot編碼的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論