1.数据预处理
2、 数据预处理 Preprocessing & Impute
2.1 数据无量纲化
- preprocessing.MinMaxScaler (数据归一化)
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler() #实例化
scaler = scaler.fit(data) #fit,在这里本质是生成min(x)和max(x)
result = scaler.transform(data) #通过接口导出结果
result
- preprocessing.StandardScaler (数据标准化)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler() #实例化
scaler.fit(data) #fit,本质是生成均值和方差
x_std = scaler.transform(data) #通过接口导出结果
2.2 缺失值
- impute.SimpleImputer
2.3 处理分类型特征:编码与哑变量 - preprocessing.LabelEncoder:标签专用,能够将分类转换为分类数值
- preprocessing.OrdinalEncoder:特征专用,能够将分类特征转换为分类数值
- preprocessing.OneHotEncoder:独热编码,创建哑变量
2.4 处理连续型特征:二值化与分段
- sklearn.preprocessing.Binarizer
- preprocessing.KBinsDiscretizer
3 特征选择 feature_selection
3.1 Filter过滤法
3.1.1 方差过滤
- VarianceThreshold
3.1.2 相关性过滤
- 卡方过滤
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
#假设在这里我一直我需要300个特征
X_fschi = SelectKBest(chi2, k=300).fit_transform(X_fsvar, y)
chivalue, pvalues_chi = chi2(X_fsvar,y)
- F检验
from sklearn.feature_selection import f_classif
F, pvalues_f = f_classif(X_fsvar,y)
- 互信息法
from sklearn.feature_selection import mutual_info_classif as MIC
result = MIC(X_fsvar,y)
k = result.shape[0] - sum(result <= 0)
3.2 Embedded嵌入法
- feature_selection.SelectFromModel
3.3 Wrapper包装法
- feature_selection.RFE