Python机器学习中的DictVectorizer(特征向量化)的使用说明

特征转换

最近在看《PYTHON机器学习及实践+从零开始通往KAGGLE竞赛之路》这本书,

书中采用最简单直接的方式介绍了机器学习的入门实践语句,简单介绍原理以后,就开始代码实现了。

刚好看到一个例子,关于DictVectorizer的使用,很是喜欢这种操作方式,代码如下:

from sklearn.feature_extraction import DictVectorizer

dict_vec = DictVectorizer(sparse=False)# #sparse=False意思是不产生稀疏矩阵

X_train = dict_vec.fit_transform(X_train.to_dict(orient='record'))
X_test = dict_vec.transform(X_test.to_dict(orient='record'))
print(dict_vec.feature_names_)#查看转换后的列名
print(X_train)#查看转换后的训练集
['age','pclass=1st', 'pclass=2nd', 'pclass=3rd', 'sex=female', 'sex=male']
[[31.19418104  0.          0.          1.          0.          1.        ]
 [31.19418104  1.          0.          0.          1.          0.        ]
 [31.19418104  0.          0.          1.          0.          1.        ]
 ...
 [12.          0.          1.          0.          1.          0.        ]
 [18.          0.          1.          0.          0.          1.        ]
 [31.19418104  0.          0.          1.          1.          0.        ]]

原pclass和sex列如下:

full[['Pclass','Sex']].head()
	Pclass	Sex
0	3	male
1	1	female
2	3	female
3	1	female
4	3	male

即pclass和sex两列分类变量转换为了数值型变量(只有0和1),age列数值型保持不变,达到了机器学习的识别目的。


该方法可用pandas中的get_dummies实现(同样可以实现one-hot编码),操作会复杂一些,代码如下:

Pclassdf = pd.DataFrame()
Pclassdf = pd.get_dummies(full['Pclass'],prefix='Pclass')
Pclassdf.head()
	Pclass_1	Pclass_2	Pclass_3
0	0	0	1
1	1	0	0
2	0	0	1
3	1	0	0
4	0	0	1

有多少特征,就会新创建多少列,在之后用pd.concat连接即可,并且需要把原Pclass给drop掉。

  • 10
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Python,我们可以使用scikit-learn库的CountVectorizer和TfidfVectorizer来实现文本特征向量。 CountVectorizer将文本转换为词频矩阵,每一行表示一个文本样本,每一列表示一个单词,单元格的数字表示该单词在该文本出现的次数。示例代码如下: ```python from sklearn.feature_extraction.text import CountVectorizer # 定义文本样本 corpus = [ 'This is the first document.', 'This is the second document.', 'And this is the third one.', 'Is this the first document?', ] # 创建CountVectorizer对象 vectorizer = CountVectorizer() # 对文本进行向量 X = vectorizer.fit_transform(corpus) # 打印词汇表 print(vectorizer.get_feature_names()) # 打印向量结果 print(X.toarray()) ``` 输出结果如下: ``` ['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this'] [[0 1 1 1 0 0 1 0 1] [0 1 0 1 0 1 1 0 1] [1 0 0 1 1 0 1 1 1] [0 1 1 1 0 0 1 0 1]] ``` TfidfVectorizer将文本转换为TF-IDF权重矩阵,每一行表示一个文本样本,每一列表示一个单词,单元格的数字表示该单词在该文本的TF-IDF权重。示例代码如下: ```python from sklearn.feature_extraction.text import TfidfVectorizer # 定义文本样本 corpus = [ 'This is the first document.', 'This is the second document.', 'And this is the third one.', 'Is this the first document?', ] # 创建TfidfVectorizer对象 vectorizer = TfidfVectorizer() # 对文本进行向量 X = vectorizer.fit_transform(corpus) # 打印词汇表 print(vectorizer.get_feature_names()) # 打印向量结果 print(X.toarray()) ``` 输出结果如下: ``` ['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this'] [[0. 0.46979139 0.58028582 0.38408524 0. 0. 0.38408524 0. 0.38408524] [0. 0.46979139 0. 0.38408524 0. 0.58028582 0.38408524 0. 0.38408524] [0.51785612 0. 0. 0.34261901 0.51785612 0. 0.34261901 0.51785612 0.34261901] [0. 0.46979139 0.58028582 0.38408524 0. 0. 0.38408524 0. 0.38408524]] ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值