sklearn特征工程(数值型、类别型、时间型、文本型)实操

利用sklearn做特征工程

一:数值型特征

1.1 对数变换(log变换)

## 对数变换
import numpy as np
log_age = df_train['Age'].apply(lambda x:np.log(x))

1.2 MinMaxscaler(最大最小值缩放)

from sklearn.preprocessing import MinMaxScaler
minmax = MinMaxScaler()
age_trans = minmax.fit_transform(df_train[['Age']])
age_trans

1.3 StandardScaler(Z-score缩放)

from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
age_std = ss.fit_transform(df_train[['Age']])
age_std

1.4 统计特征

#最小值、最大值、中位数、均值
df_train[['Age']].min()
df_train[['Age']].max()
df_train[['Age']].median()
df_train[['Age']].mean()
#分位数
df_train[['Age']].quantile(0.25)
df_train[['Age']].quantile(0.5)
df_train[['Age']].quantile(0.75)

1.5 高次特征

from sklearn.preprocessing import PolynomialFeatures
ply = PolynomialFeatures(degree = 2)
s = ply.fit_transform(df_train[['Age',"Parch"]])
s
  • 说明:参数degree代表次数,默认为2。当输入为两个特征时,输出结果会对两个特征进行组合,结果特征的次数小于等于2。比如输入为特征 [ a , b ] [a,b] [a,b],则输出为 [ 1 , a , b , a 2 , a b , b 2 ] [1,a,b,a^2, ab,b^2] [1,a,b,a2,ab,b2]

1.6 分箱/分桶操作

1.6.1 等距切分

#等距切分
df_train.loc[:,'fare_cut'] = pd.cut(df_train['Fare'],3,labels = ['low','medium','high'])

  • 等距切分的函数为pd.cut,第二个参数代表分成几份。labels参数默认为分成的区间,也可以自行设置为每个区间的名字。

1.6.2 等频切分

df_train.loc[:,'fare_qcut'] = pd.qcut(df_train['Fare'],q = [0,0.2,0.5,0.7,0.8,1])
  • 等频切分的函数为pd.qcut,第二个参数q可以为整数(代表分成的份数)或者区间(如上例,区间内为分位数)。第三个参数为labels,用法与等距切分一致。

二:类别型特征

独热向量编码(one-hot encoding)

#当特征为字符串形式的类别型特征时,比如“Embarked”代表登船口岸
embarked_oht = pd.get_dummies(df_train[['Embarked']])
#当特征为字符串形式的数值型特征时,比如“Pclass”代表船舱等级,其取值为[1,2,3],用数字代表不同等级的船舱,本质上还是类别型特征
Pclass_oht = pd.get_dummies(df_train['Pclass'].apply(lambda x:str(x)))

三:时间型特征

#将一个字符串形式的日期转换为日期格式的日期
car_sales.loc[:,'date'] = pd.to_datetime(car_sales['date_t'])
# 取出几月份
car_sales.loc[:,'month'] = car_sales['date'].dt.month
#取出星期几
car_sales.loc[:,'dow'] = car_sales['date'].dt.dayofweek
# 取出一年当中的第几天
car_sales.loc[:,'doy'] = car_sales['date'].dt.dayofyear
# 取出来是几号
car_sales.loc[:,'dom'] = car_sales['date'].dt.day
#判断是否是周末
car_sales.loc[:,'is_weekend'] = car_sales['dow'].apply(lambda x: 1 if (x==0 or x==6) else 0)

四:文本型特征

4.1 词袋模型

#countvectorizer是一个向量化的计数器
from sklearn.feature_extraction.text import CountVectorizer
vec= CountVectorizer()
doc = {
    'The MissingIndicator transformer is useful',
    'to transform a dataset into corresponding binary matrix',
    'The MissingIndicator transformer is very very useful'
}
X = vec.fit_transform(doc)
X.toarray()

array([[0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 2],
[0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0],
[1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0]], dtype=int64)

#得到词向量
vec.get_feature_names()
#结果
['binary',
 'corresponding',
 'dataset',
 'into',
 'is',
 'matrix',
 'missingindicator',
 'the',
 'to',
 'transform',
 'transformer',
 'useful',
 'very']
  • 注意:不是X.get_feature_names()

4.2 词袋模型

#在初始化计数器时,设置一下词向量的长度范围
vec = CountVectorizer(ngram_range=(1,3))
  • 参数ngram_range表示词向量的长度为[1,3](闭区间)

4.3 TF-IDF

from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer()
X = tfidf.fit_transform(corpus)
X.toarray()#得到tiidf的值
tfidf.get_feature_names()#得到特征值

你可能感兴趣的:(机器学习系列)