标签:对象 import 介绍 encode cal tar component grid pipe
并行处理、流水线处理、自动化调参、持久化是sklearn优雅地进行数据挖掘的核心。
from numpy import hstack, vstack, array, median, nan from numpy.random import choice from sklearn.datasets import load_iris iris = load_iris() iris.data #特征矩阵加工 #使用vstack增加一行含缺失值的样本(nan, nan, nan, nan) #使用hstack增加一列表示花的颜色(0-白、1-黄、2-红),花的颜色是随机的,意味着颜色并不影响花的分类 iris.data = hstack((choice([0, 1, 2], size=iris.data.shape[0]+1).reshape(-1,1), vstack((iris.data, array([nan, nan, nan, nan]).reshape(1,-1))))) #目标值向量加工 #增加一个目标值,对应含缺失值的样本,值为众数 iris.target = hstack((iris.target, array([median(iris.target)])))
下标是上述介绍的技术在sklearn说对应的方法或者类,以便于查询,具体使用后面部分将详细展开。
包 | 类或方法 | 说明 |
sklearn.pipeline | Pipeline | 流水线处理 |
sklearn.pipeline | FeatureUnion | 并行处理 |
sklearn.model_selection | GridSearchCV | 网络搜索调参 |
externals.joblib | dump | 数据持久化 |
externals.joblib | load | 从文件系统中加载数据至内存 |
并行处理可以分为整体并行处理和部分并行处理,其区别如下:
代码如下:
from numpy import log1p from sklearn.preprocessing import FunctionTransformer from sklearn.preprocessing import Binarizer from sklearn.pipeline import FeatureUnion step2_1 = (‘ToLog‘, FunctionTransformer(log1p)) step2_2 = (‘ToBinary‘, Binarizer()) step2 = (‘FeatureUnion‘, FeatureUnion(transformer_list=[step2_1, step2_2]))
在某些特定场景下,我们只需要对特征矩阵的某些列进行转换,而不是所有列,因此可以使用部分并行处理,代码如下:
from sklearn.pipeline import FeatureUnion, _fit_one_transformer, _fit_transform_one, _transform_one from sklearn.externals.joblib import Parallel, delayed from scipy import sparse import numpy as np #部分并行处理,继承FeatureUnion class FeatureUnionExt(FeatureUnion): #相比FeatureUnion,多了idx_list参数,其表示每个并行工作需要读取的特征矩阵的列 def __init__(self, transformer_list, idx_list, n_jobs=1, transformer_weights=None): self.idx_list = idx_list FeatureUnion.__init__(self, transformer_list=map(lambda trans:(trans[0], trans[1]), transformer_list), n_jobs=n_jobs, transformer_weights=transformer_weights) #由于只部分读取特征矩阵,方法fit需要重构 def fit(self, X, y=None): transformer_idx_list = map(lambda trans, idx:(trans[0], trans[1], idx), self.transformer_list, self.idx_list) transformers = Parallel(n_jobs=self.n_jobs)( #从特征矩阵中提取部分输入fit方法 delayed(_fit_one_transformer)(trans, X[:,idx], y) for name, trans, idx in transformer_idx_list) self._update_transformer_list(transformers) return self #由于只部分读取特征矩阵,方法fit_transform需要重构 def fit_transform(self, X, y=None, **fit_params): transformer_idx_list = map(lambda trans, idx:(trans[0], trans[1], idx), self.transformer_list, self.idx_list) result = Parallel(n_jobs=self.n_jobs)( #从特征矩阵中提取部分输入fit_transform方法 delayed(_fit_transform_one)(trans, name, X[:,idx], y, self.transformer_weights, **fit_params) for name, trans, idx in transformer_idx_list) Xs, transformers = zip(*result) self._update_transformer_list(transformers) if any(sparse.issparse(f) for f in Xs): Xs = sparse.hstack(Xs).tocsr() else: Xs = np.hstack(Xs) return Xs #由于只部分读取特征矩阵,方法transform需要重构 def transform(self, X): transformer_idx_list = map(lambda trans, idx:(trans[0], trans[1], idx), self.transformer_list, self.idx_list) Xs = Parallel(n_jobs=self.n_jobs)( #从特征矩阵中提取部分输入transform方法 delayed(_transform_one)(trans, name, X[:,idx], self.transformer_weights) for name, trans, idx in transformer_idx_list) if any(sparse.issparse(f) for f in Xs): Xs = sparse.hstack(Xs).tocsr() else: Xs = np.hstack(Xs) return Xs
我们对特征矩阵的第1列进行定性特征编码,对第2、3、4列进行对数函数转换,对第5列进行定量特征二值化处理,代码如下:
from numpy import log1p from sklearn.preprocessing import OneHotEncoder from sklearn.preprocessing import FunctionTransformer from sklearn.preprocessing import Binarizer step2_1 = (‘OneHotEncoder‘, OneHotEncoder(sparse=False)) step2_2 = (‘ToLog‘, FunctionTransformer(log1p)) step2_3 = (‘ToBinary‘, Binarizer()) step2 = (‘FeatureUnionExt‘, FeatureUnionExt(transformer_list=[step2_1, step2_2, step2_3], idx_list=[[0], [1, 2, 3], [4]]))
流水线上除了最后一个工作外,都要执行fit_transform方法,上一个工作的输出作为下一个工作的输入,最后一个工作必须实现fit方法,输入为上一个工作的输出,代码如下:
from numpy import log1p from sklearn.preprocessing import Imputer from sklearn.preprocessing import OneHotEncoder from sklearn.preprocessing import FunctionTransformer from sklearn.preprocessing import Binarizer from sklearn.preprocessing import MinMaxScaler from sklearn.feature_selection import SelectKBest from sklearn.feature_selection import chi2 from sklearn.decomposition import PCA from sklearn.linear_model import LogisticRegression from sklearn.pipeline import Pipeline step1 = (‘Imputer‘, Imputer()) step2_1 = (‘OneHotEncoder‘, OneHotEncoder(sparse=False)) step2_2 = (‘ToLog‘, FunctionTransformer(log1p)) step2_3 = (‘ToBinary‘, Binarizer()) step2 = (‘FeatureUnionExt‘, FeatureUnionExt(transformer_list=[step2_1, step2_2, step2_3], idx_list=[[0], [1, 2, 3], [4]])) step3 = (‘MinMaxScaler‘, MinMaxScaler()) step4 = (‘SelectKBest‘, SelectKBest(chi2, k=3)) step5 = (‘PCA‘, PCA(n_components=2)) step6 = (‘LogisticRegression‘, LogisticRegression(penalty=‘l2‘)) pipeline = Pipeline(steps=[step1, step2, step3, step4, step5, step6])
使用网格搜索调参,代码如下:
from sklearn.model_selection import GridSearchCV #新建网格搜索对象 #第一参数为待训练的模型 #param_grid为待调参数组成的网格,字典格式,键为参数名称(格式“对象名称__子对象名称__参数名称”),值为可取的参数值列表 grid_search = GridSearchCV(pipeline, param_grid={‘FeatureUnionExt__ToBinary__threshold‘:[1.0, 2.0, 3.0, 4.0], ‘LogisticRegression__C‘:[0.1, 0.2, 0.4, 0.8]}) grid_search.fit(iris.data, iris.target)
代码如下:
dump(grid_search, ‘grid_search.dmp‘, compress=3) grid_search = load(‘grid_search.dmp‘)
http://www.cnblogs.com/jasonfreak/p/5448462.html
标签:对象 import 介绍 encode cal tar component grid pipe
原文地址:https://www.cnblogs.com/CZiFan/p/10773641.html