机器学习深度学习实战模板代码(持续更新)

本文详细介绍了机器学习和深度学习的实战过程,包括建模与问题解决流程,如数据处理、特征工程、模型选择和超参数优化。讨论了数据预处理的各个环节,如数据清洗、数据采样和归一化。此外,还涵盖了文本预处理、图像数据扩增、特征选择和模型融合策略。最后,列举了多种可视化方法和模型,以及在不同分类、回归任务中的应用,如时间序列分析、推荐系统和自然语言处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

赛题的分类:

太阳底下无新事,都是出现过的赛题只是换了场景和数据 

 

 

 

 

建模与问题解决流程

了解场景和目标
了解评估准则 

1.数据处理

       数据清洗

        观察数据是否平衡 比如广告点击 不点才是大概率

        需要清除离心点,不然会破坏模型性能

        不可信的样本丢掉(需要人根据常识判断)

        缺省值极多的字段考虑不用

       数据采样

        数据采样:上下采样和保证样本均衡(1:3差不多,1:10差距太大)

 

        数据归一化

        训练集,验证集,测试集都使用训练集训练的归一化

from sklearn.preprocessing import StandardScaler
#x_train : [None,28,28] -> [None,784]    standardscaler传入的是二维数据
scaler = StandardScaler().fit_transform(x_train.reshape(-1,1)),reshape(-1,28,28)

     

文本数据的预处理

                词粒度的文本长度

                

                 字粒度的文本长度

 

统计词的数量

2.特征工程

        

        时间类的特征可以做成间隔型,也可以做出频度

        比如离双十一若干天,比如离放假若干天;比如一周剁手多少次

        文本特征的数据可以求n-gram,可以做词袋于统计词频,可以TF- IDF来看词的权重

       统计型数据 是用来拿到数值相对总体的位置

       sklearn特征抽取:

6.3. Preprocessing data — scikit-learn 0.24.2 documentation

API Reference — scikit-learn 0.24.2 documentation

        数据量特别大,那逐类特征做scaling

        特征的贡献优于模型

        

         离散化

                比如大于xx,为1;小于xx,为0

                然后独热编码 

                

enc = preprocessing.OneHotEncoder()
enc.fit(训练编码器的数据)
enc.transform(新数据).toarray()
from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder(handle_unknown='ignore')  #训练数据
X = [['Male', 1], ['Female', 3], ['Female', 2]] #训练
enc.fit(X)enc.transform([['Female', 1], ['Male', 4]]).toarray() #测试数据

        缺失值 数量比较少的用众数填充,适中的把缺省值作为独立的类型和其他类做one-hot,缺省值非常多直接放弃

        特征的分布长尾且差距很大:可以推出这个特征对结果的贡献不一定很大,如果是连续值,要进行离散化

         

特征选择:通过特征变换产生了特别多特征就需要特征选择

recursive featyre elimination :把对最终结果贡献绝对值少的特征直接踢掉

feature selection using SelectFromModel:有个feature importance

特征融合:

gp1 = SymbolicTransformer(generations=1, population_size=1000,
                              hall_of_fame=600, n_components=100,
                              function_set=function_set,
                              parsimony_coefficient=0.0005,
                              max_samples=0.9, verbose=1,
                              random_state=0, n_jobs=3)


label = data['TARGET']  #换成标签
train = data.drop(columns=['TARGET'])  #要融合的特征
train = data[['列名1','列名2']]

gp1.fit(train,label)
new_df2 = gp1.transform(train)


#可视化融合的特征
from IPython.display import Image
import pydotplus
graph = gp1._best_programs[0].export_graphviz()
graph = pydotplus.graphviz.graph_from_dot_data(graph)
Image(graph.create_png())

3·模型选择
4.寻找最佳超参数:交叉验证

cross-validation:通过多次测试的平均

确定哪个模型好

from sklearn.model_selection import cross_val_score
clf = svm.SVC(kernel='linear',C=1)
scores = cross_val_score(clf,X_train,y_train,cv=5)
scores

grid search

进一步确定模型的超参数

参数搜索

from scipy.stats import reciprocal
param_distribution={
    "hidden_layers":[1,2,3,4],
    "layer_size":np.arrange(1,100),
    
    "learning_rate":reciprocal(1e-4,1e-2),
}
from sklearn.model_selection import RandomizedSearchCV
random_search_cv = RandomizedSearchCV(sklearn_model,param_distribution,n_iter=10,n_jobs = 5)
random_search_cv.fit(x_train,y_train,epochs = 100,validation_data =(x_valid,y_valid),callbacks=callbacks)

print(random_search_cv.best_params_) #获取最好的参数

print(random_search_cv.best_score_)   #获取最好的结果

print(random_search_cv.best_estimator_) #获取最好的模型

5.模型分析与模型融合

        学习曲线来判断overfitting或者underfitting

        解决过拟合问题,增大样本的量是最有效的方式,增加正则化可能有用

stacking :将上级分类器的结果中作为下级分类器的特征,神经网络就是在干这样的事情

stacking脚本

"""Kaggle competition: Predicting a Biological Response.

Blending {RandomForests, ExtraTrees, GradientBoosting} + stretching to
[0,1]. The blending scheme is related to the idea Jose H. Solorzano
presented here:
http://www.kaggle.com/c/bioresponse/forums/t/1889/question-about-the-process-of-ensemble-learning/10950#post10950
'''You can try this: In one of the 5 folds, train the models, then use
the results of the models as 'variables' in logistic regression over
the validation data of that fold'''. Or at least this is the
implementation of my understanding of that idea :-)

The predictions are saved in test.csv. The code below created my best
submission to the competition:
- public score (25%): 0.43464
- private score (75%): 0.37751
- final rank on the private leaderboard: 17th over 711 teams :-)

Note: if you increase the number of estimators of the classifiers,
e.g. n_estimators=1000, you get a better score/rank on the private
test set.

Copyright 2012, Emanuele Olivetti.
BSD license, 3 clauses.
"""

from __future__ import division
import numpy as np
import load_data
from sklearn.cross_validation import StratifiedKFold
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression


def logloss(attempt, actual, epsilon=1.0e-15):
    """Logloss, i.e. the score of the bioresponse competition.
    """
    attempt = np.clip(attempt, epsilon, 1.0-epsilon)
    return - np.mean(actual * np.log(attempt) +
                     (1.0 - actual) * np.log(1.0 - attempt))


if __name__ == '__main__':

    np.random.seed(0)  # seed to shuffle the train set

    n_folds = 10
    verbose = True
    shuffle = False

    X, y, X_submission = load_data.load()

    if shuffle:
        idx = np.random.permutation(y.size)
        X = X[idx]
        y = y[idx]

    skf = list(StratifiedKFold(y, n_folds))

    clfs = [RandomForestClassifier(n_estimators=100, n_jobs=-1, criterion='gini'),
            RandomForestClassifier(n_estimators=100, n_jobs=-1, criterion='entropy'),
            ExtraTreesClassifier(n_estimators=100, n_jobs=-1, criterion='gini'),
            ExtraTreesClassifier(n_estimators=100, n_jobs=-1, criterion='entropy'),
            GradientBoostingClassifier(learning_rate=0.05, subsample=0.5, max_depth=6, n_estimators=50)]

    print "Creating train and test sets for blending."

    dataset_blend_train = np.zeros((X.shape[0], len(clfs)))
    dataset_blend_test = np.zeros((X_submission.shape[0], len(clfs)))

    for j, clf in enumerate(clfs):
        print j, clf
        dataset_blend_test_j = np.zeros((X_submission.shape[0], len(skf)))
        for i, (train, test) in enumerate(skf):
            print "Fold", i
            X_tra
03-21
### 关于 Mycat 数据库中间件 #### 什么是 MycatMycat 是一款开源的分布式数据库系统,作为数据库中间件,主要用于解决大数据场景下的读写分离、分库分表等问题[^5]。它可以像 MySQL 一样被使用,开发者只需在 Mycat 中配置具体的分库分表策略即可完成复杂的数据管理任务[^1]。 #### Mycat 的主要功能特性 Mycat 提供了一系列强大的功能来提升数据库性能和可扩展性: - **分布式架构**:支持分片(Sharding)、读写分离,从而显著提高数据库处理能力和并发量。 - **高可用性**:具备主备切换以及故障自动恢复的能力,保障系统的稳定运行。 - **动态数据路由**:能根据实际业务需求灵活调整分片策略,满足不同应用场景的需求。 - **多协议兼容**:除了支持 MySQL 外,还兼容 MariaDB、Oracle 和 SQL Server 等多种数据库系统[^3]。 #### 安装与环境准备 为了成功部署并使用 Mycat,需按照以下指导进行操作: 1. 准备好 Java 运行环境(JDK 版本建议为 1.8 或更高版本),因为 Mycat 基于 Java 编写而成[^2]。 2. 下载官方发布的最新版 Mycat 软件包,并解压至目标目录下。 3. 修改 `server.xml` 文件中的相关参数设置,比如监听端口号,默认情况下为 `8066`;同时定义逻辑数据库名称及其对应的物理节点信息。 4. 启动服务进程并通过命令行工具或者图形界面访问验证连接状态正常与否。 #### 查询示例 当一切就绪之后,在日常开发过程中可以通过如下方式发起简单的查询请求: ```sql SELECT * FROM user; ``` 此条语句会经由 Mycat 解析后转发给相应的后台存储引擎执行完毕再返回结果集给前端调用方[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值