数据挖掘之机器学习
调参
GridSearchCV() 对估算器指定参数值进行详尽搜索
1  | from sklearn.model_selection import GridSearchCV  | 
- estimator : estimator object.每个估算器需要提供一个
score函数或填写scoring参数。 - param_grid : dict or list of dictionaries,键作为参数名称,list 作为参数的字典。或存有这样的字典的列表。
 - scoring : string, callable, list/tuple, dict or None, default: None,
 - cv : int, cross-validation generator or an iterable, optional,如果是整数,则代表 KFold
 - refit : boolean, or string, default=True,应用已找到的最好的参数到整个数据集上。
Methods description decision_function(X) Call decision_function on the estimator with the best found parameters. fit(X[, y, groups]) Run fit with all sets of parameters. get_params([deep]) Get parameters for this estimator. inverse_transform(Xt) Call inverse_transform on the estimator with the best found params. predict(X) Call predict on the estimator with the best found parameters. predict_log_proba(X) Call predict_log_proba on the estimator with the best found parameters. predict_proba(X) Call predict_proba on the estimator with the best found parameters. score(X[, y]) Returns the score on the given data, if the estimator has been refit. set_params(**params) Set the parameters of this estimator. transform(X) Call transform on the estimator with the best found parameters.  
RandomizedSearchCV()
1  | from sklearn.model_selection import RandomizedSearchCV  | 
- estimator : estimator object.指定估算器对象。
 - param_distributions : dict,给定以参数名为键,list 为参数的字典。或提供一个分布,分布必须提供一个
rvs方法进行采样,例如来自 scipy.stats.distributions 的方法。 - n_iter : int, default=10,采样参数设置数量。
 - scoring : string, callable, list/tuple, dict or None, default: None
 - cv : int, cross-validation generator or an iterable, optional
 - refit : boolean, or string default=True
 - random_state : int, RandomState instance or None, optional, default=None
 
模型
分类
RandomForestClassifier
1  | from sklearn.ensemble import RandomForestClassifier  | 
ExtraTreesClassifier
1  | from sklearn.ensemble import ExtraTreesClassifier  | 
GradientBoostingClassifier
1  | from sklearn.ensemble import GradientBoostingClassifier  | 
XGBClassifier
1  | from xgboost.sklearn import XGBClassifier  | 
Lightgbm
1  | import lightgbm as lgb  | 
模型融合
BaggingClassifier
1  | from sklearn.ensemble import BaggingClassifier  | 
VotingClassifier
1  | from sklearn.ensemble import VotingClassifier  | 
StackingClassifier
1  | from sklearn.ensemble import RandomForestClassifier  | 
数据处理
特征放缩
MinMax scaling 归一化
该方法更容易受离散点影响X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
1  | X_scaled = X_std * (max - min) + min  | 
- feature_range : tuple (min, max), default=(0, 1),归一化后值的范围
 - copy : boolean, optional, default True,是否复制数据在新的数据上归一化
 
零均值标准化
1  | >>> from sklearn.preprocessing import StandardScaler  | 
- copy : boolean, optional, default True,是否复制数据在新的数据上执行
 - with_mean : boolean, True by default,若为 True 则在缩放前将数据居中。但在稀疏矩阵上是行不通的。
 - with_std : boolean, True by default,若为 True,则将数据放缩到单位方差或等效于单位标准差
 
处理空数据
Imputer() 处理丢失值
各属性必须是数值
1  | from sklearn.preprocessing import Imputer  | 
处理文本数据
pandas.factorize() 将输入值编码为枚举类型或分类变量
1  | housing_cat = housing['ocean_proximity']  | 
参数
- values : ndarray (1-d);序列
 - sort : boolean, default False;根据值排序
 - na_sentinel : int, default -1;给未找到赋的值
 - size_hint : hint to the hashtable sizer
 
返回值
- labels : the indexer to the original array
 - uniques : ndarray (1-d) or Index;当传递的值是 Index 或 Series 时,返回独特的索引。
 
OneHotEncoder 编码整数特征为 one-hot 向量
返回值为稀疏矩阵
1  | from sklearn.preprocessing import OneHotEncoder  | 
注意fit_transform()期望一个二维数组,所以这里将数据 reshape 了。
处理文本特征示例
1  | housing_cat = housing['ocean_proximity']  | 
LabelEncoder 标签编码
LabelEncoder`是一个可以用来将标签规范化的工具类,它可以将标签的编码值范围限定在[0,n_classes-1]。简单来说就是对不连续的数字或者文本进行编号。
1  | >>> from sklearn import preprocessing  | 
当然,它也可以用于非数值型标签的编码转换成数值标签(只要它们是可哈希并且可比较的):
1  | >>> le = preprocessing.LabelEncoder()  | 
LabelBinarizer 标签二值化
LabelBinarizer 是一个用来从多类别列表创建标签矩阵的工具类:
1  | >>> from sklearn import preprocessing  | 
对于多类别是实例,可以使用:class:MultiLabelBinarizer:
1  | >>> lb = preprocessing.MultiLabelBinarizer()  | 
本文作者 : HeoLis
原文链接 : https://ishero.net/%E6%95%B0%E6%8D%AE%E6%8C%96%E6%8E%98%E4%B9%8B%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0.html
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
学习、记录、分享、获得