sklearn快速入门教程:独热编码

import pandas as pd
data = pd.read_csv(r"D:\本科\kaggle数据挖掘\titanic\train.csv", index_col = 0)
data.head()

 处理缺失值:

from sklearn.impute import SimpleImputer
Embarked = data.loc[:, "Embarked"].values.reshape(-1,1)
imp_mode = SimpleImputer(strategy = "most_frequent") #most_frequent == 众数
data.loc[:,"Embarked"] = imp_mode.fit_transform(Embarked)
data.info()

 

<class 'pandas.core.frame.DataFrame'>
Int64Index: 891 entries, 1 to 891
Data columns (total 4 columns):
 #   Column      Non-Null Count  Dtype 
---  ------      --------------  ----- 
 0   Survived    891 non-null    int64 
 1   Sex         891 non-null    object
 2   Embarked    891 non-null    object
 3   Survived.1  891 non-null    int64 
dtypes: int64(2), object(2)
memory usage: 34.8+ KB
from sklearn.preprocessing import OneHotEncoder
X = data.iloc[:,0:-1] 
X

 

enc = OneHotEncoder(categories = 'auto').fit(X) #categories是自动属性——不用人为输入有什么属性(男、女),自动识别
result = enc.transform(X).toarray() #to array 转化成array
result
array([[0., 1., 0., 0., 1.],
       [1., 0., 1., 0., 0.],
       [1., 0., 0., 0., 1.],
       ...,
       [1., 0., 0., 0., 1.],
       [0., 1., 1., 0., 0.],
       [0., 1., 0., 1., 0.]])
result.shape #查看result的shape
(891, 5)

891行,5列(3+2:

男:10

女:01

S:100

C:010

Q:001

result = pd.DataFrame(result)
result

newdata = pd.concat([X,result], axis = 1)
newdata.head()

 

newdata.drop(["Sex", "Embarked"], axis = 1, inplace = True) #删除掉Sex和Embarked列
newdata.columns = ["Female", "Male", "Embarked_C", "Embarked_Q", "Embarked_S"]
newdata.head()

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值