博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于sklearn同时处理连续特征和离散特征
阅读量:2349 次
发布时间:2019-05-10

本文共 1202 字,大约阅读时间需要 4 分钟。

核心思路:

先用LabelEncoder对离散特征编码,因为onehotencoder只能处理数值

然后使用OneHotEncoder编码,生成稀疏表示的特征

再使用sparse.hstack连接连续特征和稀疏特征

为什么不使用pd.get_dummy呢,因为这样是直接生成的稠密矩阵,内存开销太大

 

# coding=utf-8# @author: bryanfrom sklearn.preprocessing import LabelEncoderfrom sklearn.preprocessing import OneHotEncoderfrom scipy import sparsefor feature in cate_feature + con_feature:    data[feature] = LabelEncoder().fit_transform(data[feature].values)enc = OneHotEncoder()train_x=train[numeric_feature]test_x=test[numeric_feature]for feature in cate_feature+con_feature:    enc.fit(data[feature].values.reshape(-1, 1))    train_a=enc.transform(train[feature].values.reshape(-1, 1))    test_a = enc.transform(test[feature].values.reshape(-1, 1))    train_x= sparse.hstack((train_x, train_a))    test_x = sparse.hstack((test_x, test_a))# 文本one hotfrom sklearn.feature_extraction.text import CountVectorizer# 每行用空格join起来data['corpus']=data['corpus'].apply(lambda x:' '.join(x.split(';')))#如果corpus里面是数字,可能会提示empty vocabulary; perhaps the documents only contain stop words#改成这样就行了CountVectorizer(token_pattern='(?u)\\b\\w+\\b')property_feature = CountVectorizer().fit_transform(data['corpus'])train_x=sparse.hstack((train_property_feature,train_x))

 

转载地址:http://dmmvb.baihongyu.com/

你可能感兴趣的文章
inline函数必须在头文件中定义吗?
查看>>
内存泄漏检查工具valgrind使用方法
查看>>
Solution of Codility
查看>>
java解析XML的四种方式及比较
查看>>
单例模式(java)详细
查看>>
策略模式(java)
查看>>
java线程中信号量Semaphore类的应用
查看>>
如何设置CentOS为中文显示
查看>>
Nginx配置
查看>>
php-fpm配置
查看>>
Centos 系统时间与当前时间相差和时区解决办法
查看>>
Linux下如何进行FTP设置
查看>>
linux之LVM操作案例
查看>>
由于CentOS的系统安装了epel-release-latest-7.noarch.rpm 导致在使用yum命令时出现Error: xz compression not available问题。
查看>>
php中抽象类和接口的概念与区别
查看>>
php抽象类和接口
查看>>
如何在linux CentOS 上安装chrome 谷歌浏览器
查看>>
laravel5 怎么实现事务
查看>>
GitLab安装说明
查看>>
Git查看、删除、重命名远程分支和tag
查看>>