Boost type_index库使用介绍

Boost type_index库使用介绍

Boost type_index可以获取某种类型的具体类型名称,不管是静态类型名称,还是带有cvr(const-volatile-reference)修饰的名称,还可以获取某个对象运行时的名称。

并且Boost type_index的实现消耗也是非常非常小的,如果编译器启用了rtti特性,那么他内部就实现type_info来辅助实现type_index。如果编译器没有启用rtti特性,那么他就自己实现type_index特性,利用模板+编译器的获取函数具体原型的相关宏(比如__PRETTY_FUNCTION__(GNU编译器))+一个固定名称的虚函数,就可以实现type_index的所要求的功能了。他的实现肯定小于编译器内部的rtti特性所产生的消耗。

下面会介绍一些这个库的组件和功能。

type_index类型

简单描述:

type_index类型主要就是用来存储某种类型的类型信息,关键信息就是类型的名称相关信息

type_index的类型声明位于boost/type_index.hpp中,他是一个typedef定义的类型。

当编译器启用rtti特性,或者当前编译器为vc++编译器,那么type_index实际上就是stl_type_index类型。

当编译器关闭了rtti特性,并且当前编译器不是vc++编译器的话,那么type_index实际上就是ctti_type_index类型。

主要方法:
//下面这个类是不存在,主要是为了说明type_index实际类型的主要的方法
//再次说明这个类是不存在的
class type_index
{
public:
    // 下面的三个方法,一般是用户来使用的,一般直接使用pretty_name即可
    inline const char* raw_name() const BOOST_NOEXCEPT;
    inline const char* name() const BOOST_NOEXCEPT;
    inline const string pretty_name() const BOOST_NOEXCEPT;

    // 获取此type_index实例的hash_code
    inline std::size_t hash_code() const BOOST_NOEXCEPT;    

    // 下面三个方法是三个工厂方法,他们一般是通过全局模板方法来调用的
    // 用户不用直接来调用它
    template <typename T>
    static type_index type_id() BOOST_NOEXCEPT;
    template <typename T>
    static type_index type_id_with_cvr() BOOST_NOEXCEPT;
    template <typename T>
    static type_index type_id_runtime() BOOST_NOEXCEPT;

    // 支持流运算符
    // 支持关系运算符
};
  • inline const char* raw_name() const BOOST_NOEXCEPT
    本意是用来返回原始的编译器能够认识的类型名称。

    • 当编译器是vc++编译器的时候,不管rtti是否开启,这个方法生成vc++编译器能够认识的名称,比如: typeindex::type_id<int>()::raw_name()返回:.H… …
    • 当编译器不是vc++编译器时候,当rtti开启的时候,这个方法返回类型的编译器能够识别的名称࿰
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# seeds = [2222, 5, 4, 2, 209, 4096, 2048, 1024, 2015, 1015, 820]#11 seeds = [2]#2 num_model_seed = 1 oof = np.zeros(X_train.shape[0]) prediction = np.zeros(X_test.shape[0]) feat_imp_df = pd.DataFrame({'feats': feature_name, 'imp': 0}) parameters = { 'learning_rate': 0.008, 'boosting_type': 'gbdt', 'objective': 'binary', 'metric': 'auc', 'num_leaves': 63, 'feature_fraction': 0.8,#原来0.8 'bagging_fraction': 0.8, 'bagging_freq': 5,#5 'seed': 2, 'bagging_seed': 1, 'feature_fraction_seed': 7, 'min_data_in_leaf': 20, 'verbose': -1, 'n_jobs':4 } fold = 5 for model_seed in range(num_model_seed): print(seeds[model_seed],"--------------------------------------------------------------------------------------------") oof_cat = np.zeros(X_train.shape[0]) prediction_cat = np.zeros(X_test.shape[0]) skf = StratifiedKFold(n_splits=fold, random_state=seeds[model_seed], shuffle=True) for index, (train_index, test_index) in enumerate(skf.split(X_train, y)): train_x, test_x, train_y, test_y = X_train[feature_name].iloc[train_index], X_train[feature_name].iloc[test_index], y.iloc[train_index], y.iloc[test_index] dtrain = lgb.Dataset(train_x, label=train_y) dval = lgb.Dataset(test_x, label=test_y) lgb_model = lgb.train( parameters, dtrain, num_boost_round=10000, valid_sets=[dval], early_stopping_rounds=100, verbose_eval=100, ) oof_cat[test_index] += lgb_model.predict(test_x,num_iteration=lgb_model.best_iteration) prediction_cat += lgb_model.predict(X_test,num_iteration=lgb_model.best_iteration) / fold feat_imp_df['imp'] += lgb_model.feature_importance() del train_x del test_x del train_y del test_y del lgb_model oof += oof_cat / num_model_seed prediction += prediction_cat / num_model_seed gc.collect()解释上面的python代码
05-15

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值