sklearn api

Chapter 2 – End-to-end Machine Learning project
1. Setup:同上忽略
2. Get the data:
#获取数据
housing = load_housing_data()
housing.head()
 
#数据集划分的几种方法:
1. 利用下标,选择对应行数据,区分train、test。
2. crc32、hashlib等接口。
3. 使用sklearn.model_selection中的train_test_split划分数据集。
4. 使用sklearn.model_selection中的StratifiedShuffleSplit划分数据集。
3. Discover and visualize the data to gain insights:
#可视化某些数据,从而发现数据规律
4. Prepare the data for Machine Learning algorithms:
#训练集中的样本、标签分离
#确实值处理:
1. 调用dropna(),subset为子设置。
2. 调用drop()
3. 按照平均值进行填充缺失值。调用接口fillna(替换值,inplace=True)。
4. 调用高级接口,SimpleImputer。
#编码处理,例如非数值型特征转换为数值型数据。
1. SimpleImputer
2. OneHotEncoder
#自定义transformer(转换器)
from sklearn.base import BaseEstimator, TransformerMixin
 
# column index
rooms_ix, bedrooms_ix, population_ix, household_ix = 3, 4, 5, 6
 
class CombinedAttributesAdder(BaseEstimator, TransformerMixin):
    def __init__(self, add_bedrooms_per_room = True): # no *args or **kargs
        self.add_bedrooms_per_room = add_bedrooms_per_room
    def fit(self, X, y=None):
        return self  # nothing else to do
    def transform(self, X, y=None):
        rooms_per_household = X[:, rooms_ix] / X[:, household_ix]
        population_per_household = X[:, population_ix] / X[:, household_ix]
        if self.add_bedrooms_per_room:
            bedrooms_per_room = X[:, bedrooms_ix] / X[:, rooms_ix]
            return np.c_[X, rooms_per_household, population_per_household,
                         bedrooms_per_room]
        else:
            return np.c_[X, rooms_per_household, population_per_household]
 
attr_adder = CombinedAttributesAdder(add_bedrooms_per_room=False)
housing_extra_attribs = attr_adder.transform(housing.values)
 
添加了两列数据。
#pipeline使用举例
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
 
num_pipeline = Pipeline([
        ('imputer', SimpleImputer(strategy="median")),
        ('attribs_adder', CombinedAttributesAdder()),
        ('std_scaler', StandardScaler()),
    ])
 
housing_num_tr = num_pipeline.fit_transform(housing_num)
缺失值处理、自定义处理器添加特征、数据标准化,使用pipeline打包处理。
#ColumnTransformer
num_attribs = list(housing_num)
cat_attribs = ["ocean_proximity"]
 
full_pipeline = ColumnTransformer([
        ("num", num_pipeline, num_attribs),
        ("cat", OneHotEncoder(), cat_attribs),
    ])
 
housing_prepared = full_pipeline.fit_transform(housing)
column处理。
#旧方式处理数据,即设置两个pipeline,然后调用FeatureUnion进行组合。
from sklearn.base import BaseEstimator, TransformerMixin
 
# Create a class to select numerical or categorical columns 
class OldDataFrameSelector(BaseEstimator, TransformerMixin):
    def __init__(self, attribute_names):
        self.attribute_names = attribute_names
    def fit(self, X, y=None):
        return self
    def transform(self, X):
        return X[self.attribute_names].values
num_attribs = list(housing_num)
cat_attribs = ["ocean_proximity"]
 
old_num_pipeline = Pipeline([
        ('selector', OldDataFrameSelector(num_attribs)),
        ('imputer', SimpleImputer(strategy="median")),
        ('attribs_adder', CombinedAttributesAdder()),
        ('std_scaler', StandardScaler()),
    ])
 
old_cat_pipeline = Pipeline([
        ('selector', OldDataFrameSelector(cat_attribs)),
        ('cat_encoder', OneHotEncoder(sparse=False)),
    ])
from sklearn.pipeline import FeatureUnion
 
old_full_pipeline = FeatureUnion(transformer_list=[
        ("num_pipeline", old_num_pipeline),
        ("cat_pipeline", old_cat_pipeline),
    ])
 
5. Select and train a model:
#构建一个线性回归模型
from sklearn.linear_model import LinearRegression
 
lin_reg = LinearRegression()
 
#与真实数据进行对比,即模型评估
#平均值平方估计
#平均值绝对值估计
#决策树模型
from sklearn.tree import DecisionTreeRegressor
 
tree_reg = DecisionTreeRegressor(random_state=42)
6. Fine-tune your model:
#交叉验证,分别对比线性模型以及决策树
from sklearn.model_selection import cross_val_score
 
scores = cross_val_score(tree_reg, housing_prepared, housing_labels, scoring="neg_mean_squared_error", cv=10)
tree_rmse_scores = np.sqrt(-scores)
#随机回归森林模型
from sklearn.ensemble import RandomForestRegressor
 
forest_reg = RandomForestRegressor(n_estimators=10, random_state=42)
用平均值平方估计
用交叉验证估计
#svm回归模型
svm_reg = SVR(kernel="linear")
用均值平方估计
#GridSearchCV网格搜索参数最优值
from sklearn.model_selection import GridSearchCV
 
param_grid = [
    # try 12 (3×4) combinations of hyperparameters
    {'n_estimators': [3, 10, 30], 'max_features': [2, 4, 6, 8]},
    # then try 6 (2×3) combinations with bootstrap set as False
    {'bootstrap': [False], 'n_estimators': [3, 10], 'max_features': [2, 3, 4]},
  ]
 
forest_reg = RandomForestRegressor(random_state=42)
# train across 5 folds, that's a total of (12+6)*5=90 rounds of training 
grid_search = GridSearchCV(forest_reg, param_grid, cv=5,
                           scoring='neg_mean_squared_error', return_train_score=True)
grid_search.fit(housing_prepared, housing_labels)
#随机搜索参数最优值
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint
 
param_distribs = {
        'n_estimators': randint(low=1, high=200),
        'max_features': randint(low=1, high=8),
    }
 
forest_reg = RandomForestRegressor(random_state=42)
rnd_search = RandomizedSearchCV(forest_reg, param_distributions=param_distribs,
                                n_iter=10, cv=5, scoring='neg_mean_squared_error', random_state=42)
rnd_search.fit(housing_prepared, housing_labels)
#特征重要性评估,适合随机森林
feature_importances = grid_search.best_estimator_.feature_importances_
extra_attribs = ["rooms_per_hhold", "pop_per_hhold", "bedrooms_per_room"]
#cat_encoder = cat_pipeline.named_steps["cat_encoder"] # old solution
cat_encoder = full_pipeline.named_transformers_["cat"]
cat_one_hot_attribs = list(cat_encoder.categories_[0])
attributes = num_attribs + extra_attribs + cat_one_hot_attribs
sorted(zip(feature_importances, attributes), reverse=True)
#最终模型
final_model = grid_search.best_estimator_
 
X_test = strat_test_set.drop("median_house_value", axis=1)
y_test = strat_test_set["median_house_value"].copy()
 
X_test_prepared = full_pipeline.transform(X_test)
final_predictions = final_model.predict(X_test_prepared)
 
final_mse = mean_squared_error(y_test, final_predictions)
final_rmse = np.sqrt(final_mse)
#计算%95置信度下的RMSE
from scipy import stats
confidence = 0.95
squared_errors = (final_predictions - y_test) ** 2
mean = squared_errors.mean()
m = len(squared_errors)
 
np.sqrt(stats.t.interval(confidence, m - 1,
                         loc=np.mean(squared_errors),
                         scale=stats.sem(squared_errors)))
#手工计算置信度区间为:
tscore = stats.t.ppf((1 + confidence) / 2, df=m - 1)
tmargin = tscore * squared_errors.std(ddof=1) / np.sqrt(m)
np.sqrt(mean - tmargin), np.sqrt(mean + tmargin)
#z-score如下:
zscore = stats.norm.ppf((1 + confidence) / 2)
zmargin = zscore * squared_errors.std(ddof=1) / np.sqrt(m)
np.sqrt(mean - zmargin), np.sqrt(mean + zmargin)
 
7. Extra material:
A full pipeline with both preparation and prediction:
full_pipeline_with_predictor = Pipeline([
        ("preparation", full_pipeline),
        ("linear", LinearRegression())
    ])
 
full_pipeline_with_predictor.fit(housing, housing_labels)
full_pipeline_with_predictor.predict(some_data)
Model persistence using joblib:
from sklearn.externals import joblib
joblib.dump(my_model, "my_model.pkl") # DIFF
#...
my_model_loaded = joblib.load("my_model.pkl") # DIFF
Example SciPy distributions for RandomizedSearchCV:
from scipy.stats import geom, expon
geom_distrib=geom(0.5).rvs(10000, random_state=42)
expon_distrib=expon(scale=1).rvs(10000, random_state=42)
plt.hist(geom_distrib, bins=50)
plt.show()
plt.hist(expon_distrib, bins=50)
plt.show()
sklearn-api:
from sklearn.impute import SimpleImputer    缺失值处理器,高级api,statistics_显示对应设置值,支持平均值、中值、最多值。strategy显示当前选取策略。
.transform()    转化数据。
from sklearn.preprocessing import OrdinalEncoder    特征分类编码器,非数字类特征分类值转化为对应的数字。
.fit_transform()    拟合并转化数据
from sklearn.preprocessing import OneHotEncoder    onehot格式的特征分类方式。
from sklearn.base import BaseEstimator,TransformerMixin    用于自定义转换器。
.fit()    拟合数据
from sklearn.pipeline import Pipeline    管道,用于数据处理打包。
from sklearn.preprocessing import StandardScaler    数据标准化
from sklearn.compose import ColumnTransformer    列转换器,第一个参数是转换器,第二个参数是对应列数据。
from sklearn.pipeline import FeatureUnion    将多个pipeline组合。
from sklearn.linear_model import LinearRegression    sklearn中的LinearRegression(线性回归模型)
from sklearn.metrics import mean_squared_error    平均值平方估计
from sklearn.metrics import mean_absolute_error    平均值绝对值估计
from sklearn.tree import DecisionTreeRegressor    决策树模型
from sklearn.model_selection import cross_val_score    交叉验证
from sklearn.ensemble import RandomForestRegressor    随机深林模型
from sklearn.svm import SVR    svm回归模型
from sklearn.model_selection import GridSearchCV    模型最优参数查找器,best_params_,best_estimator_,cv_results_,.best_estimator_.feature_importances_
from sklearn.model_selection import RandomizedSearchCV    模型参数随机查找器
from scipy.stats import randint     
from scipy import stats     
numpy:
.random.permutation    打乱固定范围内的数据,例如12345->32154
.c_()    类似于pandas的merge,合并数据
.random.randn(4,4)    生成4*4的随机矩阵
pandas:
.read_csv()      从csv文件里读取数据
.head()    打印头部前5个数据
.info()    数据总结
.value_counts()    单列数据值统计
.describe()    数据分析
.iloc()    数字下标选择对应行数据
.ceil()    返回最小的整数,使得整数i>x
.where()    (condition, x, y)满足条件则输出x,否则输出y
.corr()    混淆矩阵
.sort_values()    按照固定规则排序
from pandas.plotting import scatter_matrix    pandas支持的画图接口
.copy()    复制数据
.drop()    丢弃数据
.isnull()    元素级显示数据是否为空值,空值为TRUE,否则为False
.dropna()    丢弃包含na的列
.median()    求中值
.fillna()    填充空值
.loc()    根据行名称选择对应行数据
.isnull().any(axis=1)    按行显示是否包含空值,默认为按列显示。
.Series()    统计函数
 
matplotlib:
.hist()    显示数据分布图
.plot()    画图
.get_cmap()    设置cmap属性
.legend()    图例
.mpimg.imread()    读取图像接口
.imshow()     
.ylabel()    设置y轴参数
.xlabel()    设置x轴参数
.colorbar()    渐变条
.ax.set_yticklabels()    刻度设置
.set_label()    设置标签
.show()    展示描述后的图画
.savefig()    保存图画
.axis()    设置x、y轴的取值范围


 

sklearn0.19中文文档 PDF格式高清。 .1. 广义线性模型 1.1.1. 普通最小二乘法 1.1.1.1. 普通最小二乘法复杂度 1.1.2. 岭回归 1.1.2.1. 岭回归的复杂度 1.1.2.2. 设置正则化参数:广义交叉验证 1.1.3. Lasso 1.1.3.1. 设置正则化参数 1.1.3.1.1. 使用交叉验证 1.1.3.1.2. 基于信息标准的模型选择 1.1.3.1.3. 与 SVM 的正则化参数的比较 1.1.4. 多任务 Lasso 1.1.5. 弹性网络 1.1.6. 多任务弹性网络 1.1.7. 最小角回归 1.1.8. LARS Lasso 1.1.8.1. 数学表达式 1.1.9. 正交匹配追踪法(OMP) 1.1.10. 贝叶斯回归 1.1.10.1. 贝叶斯岭回归 1.1.10.2. 主动相关决策理论 - ARD 1.1.11. logistic 回归 1.1.12. 随机梯度下降, SGD 1.1.13. Perceptron(感知器) 1.1.14. Passive Aggressive Algorithms(被动攻击算法) 1.1.15. 稳健回归(Robustness regression): 处理离群点 (outliers)和模型错误 1.1.15.1. 各种使用场景与相关概念 1.1.15.2. RANSAC: 随机抽样一致性算法(RANdom SAmple Consensus) 1.1.15.2.1. 算法细节 1.1.15.3. Theil-Sen 预估器: 广义中值估计 1.1.15.3.1. 算法理论细节 1.1.15.4. Huber 回归 1.1.15.5. 注意 1.1.16. 多项式回归:用基函数展开线性模型 1.2. 线性和二次判别分析 1.2.1. 使用线性判别分析来降维 1.2.2. LDA 和 QDA 分类器的数学公式 1.2.3. LDA 的降维数学公式 1.2.4. Shrinkage(收缩) 1.2.5. 预估算法 1.3. 内核岭回归 1.4. 支持向量机 1.4.1. 分类 1.4.1.1. 多元分类 1.4.1.2. 得分和概率 1.4.1.3. 非均衡问题 1.4.2. 回归 1.4.3. 密度估计, 异常(novelty)检测 1.4.4. 复杂度 1.4.5. 使用诀窍 1.4.6. 核函数 1.4.6.1. 自定义核 1.4.6.1.1. 使用 python 函数作为内核 1.4.6.1.2. 使用 Gram 矩阵 1.4.6.1.3. RBF 内核参数 1.4.7. 数学公式 1.4.7.1. SVC 1.4.7.2. NuSVC 1.4.7.3. SVR 1.4.8. 实现细节 1.5. 随机梯度下降 1.5.1. 分类 1.5.2. 回归 1.5.3. 稀疏数据的随机梯度下降 1.5.4. 复杂度 1.5.5. 实用小贴士 1.5.6. 数学描述 1.5.6.1. SGD 1.5.7. 实现细节 1.6. 最近邻 1.6.1. 无监督最近邻 1.6.1.1. 找到最近邻 1.6.1.2. KDTree 和 BallTree 类 1.6.2. 最近邻分类 1.6.3. 最近邻回归 1.6.4. 最近邻算法 1.6.4.1. 暴力计算 1.6.4.2. K-D 树 1.6.4.3. Ball 树 1.6.4.4. 最近邻算法的选择 1.6.4.5. leaf_size 的影响 1.6.5. 最近质心分类 1.6.5.1. 最近缩小质心 1.7. 高斯过程 1.7.1. 高斯过程回归(GPR) 1.7.2. GPR 示例 1.7.2.1. 具有噪声级的 GPR 估计 1.7.2.2. GPR 和内核岭回归(Kernel Ridge Regression)的比 较 1.7.2.3. Mauna Loa CO2 数据中的 GRR 1.7.3. 高斯过程分类(GPC) 1.7.4. GPC 示例 1.7.4.1. GPC 概率预测 1.7.4.2. GPC 在 XOR 数据集上的举例说明 1.7.4.3. iris 数据集上的高斯过程分类(GPC) 1.7.5. 高斯过程内核 1.7.5.1. 高斯过程内核 API 1.7.5.2. 基础内核 1.7.5.3. 内核操作 1.7.5.4. 径向基函数内核 1.7.5.5. Matérn 内核 1.7.5.6. 有理二次内核 1.7.5.7. 正弦平方内核 1.7.5.8. 点乘内核 1.7.5.9. 参考文献 1.7.6. 传统高斯过程 1.7.6.1. 回归实例介绍 1.7.6.2. 噪声数据拟合 1.7.6.3. 数学形式 1.7.6.3.1. 初始假设 1.7.6.3.2. 最佳线性无偏预测(BLUP) 1.7.6.3.3. 经验最佳线性无偏估计(EBLUP) 1.7.6.4. 关联模型 1.7.6.5. 回归模型 1.7.6.6. 实现细节 1.8. 交叉分解 1.9. 朴素贝叶斯 1.9.1. 高斯朴素贝叶斯 1.9.2. 多项分布朴素贝叶斯 1.9.3. 伯努利朴素贝叶斯 1.9.4. 堆外朴素贝叶斯模型拟合 1.10. 决策树 1.10.1. 分类 1.10.2. 回归 1.10.3. 多值输出问题 1.10.4. 复杂度分析 1.10.5. 实际使用技巧 1.10.6. 决策树算法: ID3, C4.5, C5.0 和 CART 1.10.7. 数学表达 1.10.7.1. 分类标准 1.10.7.2. 回归标准 1.11. 集成方法 1.11.1. Bagging meta-estimator(Bagging 元估计器) 1.11.2. 由随机树组成的森林 1.11.2.1. 随机森林 1.11.2.2. 极限随机树 1.11.2.3. 参数 1.11.2.4. 并行化 1.11.2.5. 特征重要性评估 1.11.2.6. 完全随机树嵌入 1.11.3. AdaBoost 1.11.3.1. 使用方法 1.11.4. Gradient Tree Boosting(梯度树提升) 1.11.4.1. 分类 1.11.4.2. 回归 1.11.4.3. 训练额外的弱学习器 1.11.4.4. 控制树的大小 1.11.4.5. Mathematical formulation(数学公式) 1.11.4.5.1. Loss Functions(损失函数) 1.11.4.6. Regularization(正则化) 1.11.4.6.1. 收缩率 (Shrinkage) 1.11.4.6.2. 子采样 (Subsampling) 1.11.4.7. Interpretation(解释性) 1.11.4.7.1. Feature importance(特征重要性) 1.11.4.7.2. Partial dependence(部分依赖) 1.11.5. Voting Classifier(投票分类器) 1.11.5.1. 多数类标签 (又称为 多数/硬投票) 1.11.5.1.1. 用法 1.11.5.2. 加权平均概率 (软投票) 1.11.5.3. 投票分类器(VotingClassifier)在网格搜索 (GridSearch)应用 1.11.5.3.1. 用法 1.12. 多类和多标签算法 1.12.1. 多标签分类格式 1.12.2. 1对其余 1.12.2.1. 多类学习 1.12.2.2. 多标签学习 1.12.3. 1对1 1.12.3.1. 多类别学习 1.12.4. 误差校正输出代码 1.12.4.1. 多类别学习 1.12.5. 多输出回归 1.12.6. 多输出分类 1.12.7. 链式分类器 1.13. 特征选择 1.13.1. 移除低方差特征 1.13.2. 单变量特征选择 1.13.3. 递归式特征消除 1.13.4. 使用 SelectFromModel 选取特征 1.13.4.1. 基于 L1 的特征选取 1.13.4.2. 基于 Tree(树)的特征选取 1.13.5. 特征选取作为 pipeline(管道)的一部分 1.14. 半监督学习 1.14.1. 标签传播 1.15. 等式回归 1.16. 概率校准 1.17. 神经网络模型(有监督) 1.17.1. 多层感知器 1.17.2. 分类 1.17.3. 回归 1.17.4. 正则化 1.17.5. 算法 1.17.6. 复杂性 1.17.7. 数学公式 1.17.8. 实用技巧 1.17.9. 使用 warm_start 的更多控制
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值