第1关:标准化

# -*- coding: utf-8 -*-
 
from sklearn.preprocessing import scale,MaxAbsScaler,MinMaxScaler
 
#实现数据预处理方法
def Preprocessing(x,y):
    '''
    x(ndarray):处理 数据
    y(str):y等于'z_score'使用z_score方法
           y等于'minmax'使用MinMaxScaler方法
           y等于'maxabs'使用MaxAbsScaler方法
    '''
    #********* Begin *********#
    if y=='z_score':
       x = scale(x)
       return x
    elif y=='minmax':
       x = MinMaxScaler().fit_transform(x)
       return x
    elif y=='maxabs':
       x = MaxAbsScaler().fit_transform(x)
       return x
    #********* End *********#
 

第2关:非线性转换

# -*- coding: utf-8 -*-
from sklearn.preprocessing import QuantileTransformer
 
#实现非线性转换方法
def non_linear_transformation(x,y):
    '''
    x(ndarray):待处理数据
    y(int):y等于0映射到均匀分布
           y等于1映射到高斯分布
    '''
    #********* Begin *********#
    if y==0:
       x = QuantileTransformer(random_state=666).fit_transform(x)
       return x
    elif y==1:
       x = QuantileTransformer(output_distribution='normal',  random_state=666).fit_transform(x)
       return x
    #********* End *********#
 

第3关:归一化

# -*- coding: utf-8 -*-
 
from sklearn.preprocessing import normalize
 
#实现数据归一化方法
def normalization(x,y):
    '''
    x(ndarray):待处理数据
    y(int):y等于1则使用"l1"归一化
           y等于2则使用"l2"归一化
    '''
    #********* Begin *********#
    if y==1:
       x = normalize(x,'l1')
       return x
    elif y==2:
       x = normalize(x,'l2')
       return x
    #********* End *********#

第4关:离散值编码

# -*- coding: utf-8 -*-
import numpy as np
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
 
def onehot_label(label):
    '''
    input:label(list):待处理标签
    output:lable(ndarray):onehot处理后的标签
    '''
    #********* Begin *********#
    label = LabelEncoder().fit_transform(label)
    label = np.array(label).reshape(len(label),1)
    label = OneHotEncoder().fit_transform(label).toarray()
    return label
    #********* End *********#




第5关:生成多项式特征

# -*- coding: utf-8 -*-
from sklearn.preprocessing import PolynomialFeatures
def polyfeaturs(x,y):
    '''
    x(ndarray):待处理特征
    y(int):y等于0生成二项式特征
           y等于1生成二项式特征,只需要特征之间交互
    '''
    #********* Begin *********#
    if y==0:
       x = PolynomialFeatures(2).fit_transform(x)
       return x
    elif y==1:
       x = PolynomialFeatures(degree=2, interaction_only=True).fit_transform(x)
       return x
    #********* End *********#

第6关:估算缺失值

# -*- coding: utf-8 -*-
from sklearn.preprocessing import Imputer
 
def imp(x,y):
    '''
    x(ndarray):待处理数据
    y(str):y为'mean'则用取平均方式补充缺失值
           y为'meian'则用取中位数方式补充缺失值
           y为'most_frequent'则用出现频率最多的值代替缺失值        
    '''
    #********* Begin *********#
    if y=='mean':
       x = Imputer(missing_values='NaN', strategy='mean', axis=0).fit_transform(x)
       return x
    elif y=='meian':
       x = Imputer(missing_values='NaN', strategy='meian', axis=0).fit_transform(x)
       return x
    elif y=='most_frequent':
       x = Imputer(missing_values='NaN', strategy='most_frequent', axis=0).fit_transform(x)
       return x
    #********* End *********#

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐