網(wǎng)站首頁 編程語言 正文
Boost.Accumulators?
? ? ? ? Boost.Accumulators 提供了處理樣本的類。例如,您可以找到最大或最小的樣本,或者計算所有樣本的總和。雖然標(biāo)準(zhǔn)庫支持其中一些操作,但 Boost.Accumulators 還支持統(tǒng)計計算,例如均值和標(biāo)準(zhǔn)差。
? ? ? ? 該庫稱為 Boost.Accumulators,因為累加器是一個基本概念。累加器是一個容器,每次插入一個值時都會計算出一個新的結(jié)果。該值不一定存儲在累加器中。相反,累加器在輸入新值時不斷更新中間結(jié)果。
? ? ? ? Boost.Accumulators 包含三個部分:
- 框架提供了庫的整體結(jié)構(gòu)。它提供類 boost::accumulators::accumulator_set,它總是與 Boost.Accumulators 一起使用。雖然您需要了解這個類和框架中的其他一些類,但細節(jié)并不重要,除非您想開發(fā)自己的累加器。頭文件 boost/accumulators/accumulators.hpp 使您可以訪問 boost::accumulators::accumulator_set 和框架中的其他類。
- Boost.Accumulators 提供了許多執(zhí)行計算的累加器。一旦包含 boost/accumulators/statistics.hpp,您就可以訪問和使用所有這些累加器。
- Boost.Accumulators 提供運算符,例如,將一個 std::complex 類型的復(fù)數(shù)與一個 int 值相乘或?qū)蓚€向量相加。頭文件 boost/accumulators/numeric/functional.hpp 定義了 std::complex、std::valarray 和 std::vector 的運算符。您不需要自己包含頭文件,因為它包含在累加器的頭文件中。但是,您必須定義宏 BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT、BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT 和 BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT 以使運算符可用。
? ? ? ? Boost.Accumulators 提供的所有類和函數(shù)都在 boost::accumulators 或嵌套命名空間中定義。例如,所有累加器都在 boost::accumulators::tag 中定義。
示例 58.1。使用 boost::accumulators::tag::count 計數(shù)
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <iostream>
using namespace boost::accumulators;
int main()
{
accumulator_set<int, features<tag::count>> acc;
acc(4);
acc(-6);
acc(9);
std::cout << count(acc) << '\n';
}
Example?58.1?
? ? ? ? 示例 58.1 使用 boost::accumulators::tag::count,這是一個簡單的累加器,用于計算傳遞給它的值的數(shù)量。因此,由于傳遞了三個值,此示例將 3 寫入標(biāo)準(zhǔn)輸出。要使用累加器,您需要訪問類 boost::accumulators::accumulator_set,這是一個模板,它期望將要處理的值的類型作為其第一個參數(shù)。示例 58.1 將 int 作為第一個參數(shù)傳遞。
? ? ? ? 第二個參數(shù)指定要使用的累加器。您可以使用多個累加器。類名 boost::accumulators::accumulator_set 表示可以管理任意數(shù)量的累加器。
? ? ? ? 嚴格來說,您指定的是特征,而不是累加器。特征定義了應(yīng)該計算什么。你決定什么,而不是如何。功能可以有不同的實現(xiàn)。實現(xiàn)是累加器。
? ? ? ? 示例 58.1 使用 boost::accumulators::tag::count 選擇一個計算值的累加器。如果存在多個可以計算值的累加器,Boost.Accumulators 會選擇默認的累加器。
? ? ? ? 請注意,您不能將特征直接傳遞給 boost::accumulators::accumulator_set。您需要使用 boost::accumulators::features。
? ? ? ? boost::accumulators::accumulator_set 類型的對象可以像函數(shù)一樣使用??梢酝ㄟ^調(diào)用 operator() 來傳遞值。它們會立即得到處理。傳遞的值必須與作為第一個模板參數(shù)傳遞給 boost::accumulators::accumulator_set 的類型相同。
? ? ? ? 對于每個特征,都有一個同名的提取器。提取器接收累加器的當(dāng)前結(jié)果。示例 58.1 使用提取器 boost::accumulators::count()。傳遞的唯一參數(shù)是 acc。 boost::accumulators::count() 返回 3。
? ? ? ? 示例 58.2。使用均值和方差
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <iostream>
using namespace boost::accumulators;
int main()
{
accumulator_set<double, features<tag::mean, tag::variance>> acc;
acc(8);
acc(9);
acc(10);
acc(11);
acc(12);
std::cout << mean(acc) << '\n';
std::cout << variance(acc) << '\n';
}
Example?58.2?
? ? ? ? 示例 58.2 使用兩個特征 boost::accumulators::tag::mean 和 boost::accumulators::tag::variance 來計算五個值的均值和方差。該示例將 10 和 2 寫入標(biāo)準(zhǔn)輸出。
? ? ? ? 方差為 2,因為 Boost.Accumulators 為五個值中的每一個分配了 0.2 的權(quán)重。使用 boost::accumulators::tag::variance 選擇的累加器使用權(quán)重。如果未明確設(shè)置權(quán)重,則所有值都具有相同的權(quán)重。
? ? ? ? 示例 58.3。計算加權(quán)方差
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <iostream>
using namespace boost::accumulators;
int main()
{
accumulator_set<double, features<tag::mean, tag::variance>, int> acc;
acc(8, weight = 1);
acc(9, weight = 1);
acc(10, weight = 4);
acc(11, weight = 1);
acc(12, weight = 1);
std::cout << mean(acc) << '\n';
std::cout << variance(acc) << '\n';
}
Example?58.3?
? ? ? ? 示例 58.3 將 int 作為第三個模板參數(shù)傳遞給 boost::accumulators::accumulator_set。此參數(shù)指定權(quán)重的數(shù)據(jù)類型。在此示例中,權(quán)重分配給每個值。
? ? ? ? Boost.Accumulators 使用 Boost.Parameter 以名稱/值對的形式傳遞附加參數(shù),例如權(quán)重。權(quán)重的參數(shù)名稱是權(quán)重。您可以將參數(shù)視為變量并分配一個值。名稱/值對作為附加參數(shù)在每個值之后傳遞給累加器。
? ? ? ? 在示例 58.3 中,值 10 的權(quán)重為 4,而所有其他值的權(quán)重為 1。均值仍然是 10,因為權(quán)重對均值無關(guān)緊要。但是,方差現(xiàn)在是 1.25。與前面的示例相比,它有所減少,因為中間值的權(quán)重高于其他值。
? ? ? ? Boost.Accumulators 提供了更多的累加器。它們的用法與本章介紹的累加器相同。該庫的文檔包含對所有可用累加器的概述。
原文鏈接:https://yamagota.blog.csdn.net/article/details/128032406
相關(guān)推薦
- 2023-03-25 詳解Typescript?嚴格模式有多嚴格_其它
- 2023-03-13 使用webpack配置react-hot-loader熱加載局部更新_React
- 2022-10-02 iOS實現(xiàn)簡易的抽屜效果_IOS
- 2022-03-29 python實現(xiàn)k-means算法_python
- 2022-10-08 一文帶你了解React中的函數(shù)組件_React
- 2022-09-25 linux命令中cd到路徑報錯
- 2022-07-04 PyTorch搭建LSTM實現(xiàn)時間序列負荷預(yù)測_python
- 2022-10-04 python?numpy庫中數(shù)組遍歷的方法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支