## 对数变换
import numpy as np
log_age = df_train['Age'].apply(lambda x:np.log(x))
from sklearn.preprocessing import MinMaxScaler
minmax = MinMaxScaler()
age_trans = minmax.fit_transform(df_train[['Age']])
age_trans
from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
age_std = ss.fit_transform(df_train[['Age']])
age_std
#最小值、最大值、中位数、均值
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)
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]#等距切分
df_train.loc[:,'fare_cut'] = pd.cut(df_train['Fare'],3,labels = ['low','medium','high'])
pd.cut
,第二个参数代表分成几份。labels
参数默认为分成的区间,也可以自行设置为每个区间的名字。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
,用法与等距切分一致。#当特征为字符串形式的类别型特征时,比如“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)
#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()
#在初始化计数器时,设置一下词向量的长度范围
vec = CountVectorizer(ngram_range=(1,3))
ngram_range
表示词向量的长度为[1,3](闭区间)from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer()
X = tfidf.fit_transform(corpus)
X.toarray()#得到tiidf的值
tfidf.get_feature_names()#得到特征值