NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
percentile.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <cmath>
31
32#include "NumCpp/Core/Enums.hpp"
36#include "NumCpp/Core/Shape.hpp"
37#include "NumCpp/Core/Types.hpp"
40#include "NumCpp/NdArray.hpp"
42
43namespace nc
44{
45 //============================================================================
46 // Method Description:
62 template<typename dtype>
64 double inPercentile,
65 Axis inAxis = Axis::NONE,
67 {
69
70 if (inPercentile < 0. || inPercentile > 100.)
71 {
72 THROW_INVALID_ARGUMENT_ERROR("input percentile value must be of the range [0, 100].");
73 }
74
75 if (inArray.isempty())
76 {
77 return {};
78 }
79 else if (inArray.isscalar())
80 {
81 NdArray<double> returnArray = { static_cast<double>(inArray.front()) };
82 return returnArray;
83 }
84
85 switch (inAxis)
86 {
87 case Axis::NONE:
88 {
89 NdArray<double> arrayCopy = inArray.template astype<double>();
90 stl_algorithms::sort(arrayCopy.begin(), arrayCopy.end());
91
92 if (utils::essentiallyEqual(inPercentile, 0.))
93 {
94 NdArray<double> returnArray = { arrayCopy.front() };
95 return returnArray;
96 }
97 if (utils::essentiallyEqual(inPercentile, 100.))
98 {
99 NdArray<double> returnArray = { arrayCopy.back() };
100 return returnArray;
101 }
102
103 const auto i =
104 static_cast<uint32>(std::floor(static_cast<double>(inArray.size() - 1) * inPercentile / 100.));
105 const auto indexLower = clip<uint32>(i, 0, inArray.size() - 2);
106
107 switch (inInterpMethod)
108 {
110 {
111 const double percentI =
112 static_cast<double>(indexLower) / static_cast<double>(inArray.size() - 1);
113 const double fraction =
114 (inPercentile / 100. - percentI) /
115 (static_cast<double>(indexLower + 1) / static_cast<double>(inArray.size() - 1) - percentI);
116
117 NdArray<double> returnArray = {
118 arrayCopy[indexLower] + (arrayCopy[indexLower + 1] - arrayCopy[indexLower]) * fraction
119 };
120 return returnArray;
121 }
123 {
124 NdArray<double> returnArray = { arrayCopy[indexLower] };
125 return returnArray;
126 }
128 {
129 NdArray<double> returnArray = { arrayCopy[indexLower + 1] };
130 return returnArray;
131 }
133 {
134 const double percent = inPercentile / 100.;
135 const double percent1 =
136 static_cast<double>(indexLower) / static_cast<double>(inArray.size() - 1);
137 const double percent2 =
138 static_cast<double>(indexLower + 1) / static_cast<double>(inArray.size() - 1);
139 const double diff1 = percent - percent1;
140 const double diff2 = percent2 - percent;
141
142 switch (argmin<double>({ diff1, diff2 }).item())
143 {
144 case 0:
145 {
146 NdArray<double> returnArray = { arrayCopy[indexLower] };
147 return returnArray;
148 }
149 case 1:
150 {
151 NdArray<double> returnArray = { arrayCopy[indexLower + 1] };
152 return returnArray;
153 }
154 }
155
156 return {}; // get rid of compiler warning
157 }
159 {
160 NdArray<double> returnArray = { (arrayCopy[indexLower] + arrayCopy[indexLower + 1]) / 2. };
161 return returnArray;
162 }
163 default:
164 {
165 THROW_INVALID_ARGUMENT_ERROR("Unimplemented Interpolation method.");
166 return {}; // get rid of compiler warning
167 }
168 }
169 }
170 case Axis::COL:
171 {
172 const Shape inShape = inArray.shape();
173
174 NdArray<double> returnArray(1, inShape.rows);
175 for (uint32 row = 0; row < inShape.rows; ++row)
176 {
177 returnArray[row] =
178 percentile(NdArray<dtype>(const_cast<typename NdArray<dtype>::pointer>(&inArray.front(row)),
179 inShape.cols,
181 inPercentile,
183 inInterpMethod)
184 .item();
185 }
186
187 return returnArray;
188 }
189 case Axis::ROW:
190 {
191 return percentile(inArray.transpose(), inPercentile, Axis::COL, inInterpMethod);
192 }
193 default:
194 {
195 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
196 return {}; // get rid of compiler warning
197 }
198 }
199
200 return {}; // get rid of compiler warning
201 }
202} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
iterator end() noexcept
Definition NdArrayCore.hpp:1619
typename AllocTraits::pointer pointer
Definition NdArrayCore.hpp:148
self_type transpose() const
Definition NdArrayCore.hpp:4959
bool isempty() const noexcept
Definition NdArrayCore.hpp:3008
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
const_reference back() const noexcept
Definition NdArrayCore.hpp:2359
const_reference front() const noexcept
Definition NdArrayCore.hpp:2936
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
bool isscalar() const noexcept
Definition NdArrayCore.hpp:3032
value_type item() const
Definition NdArrayCore.hpp:3098
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
uint32 rows
Definition Core/shape.hpp:44
uint32 cols
Definition Core/shape.hpp:45
void sort(RandomIt first, RandomIt last) noexcept
Definition StlAlgorithms.hpp:697
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition essentiallyEqual.hpp:47
Definition Cartesian.hpp:39
InterpolationMethod
Definition Enums.hpp:139
@ HIGHER
Definition Enums.hpp:142
@ LOWER
Definition Enums.hpp:141
@ LINEAR
Definition Enums.hpp:140
@ NEAREST
Definition Enums.hpp:143
@ MIDPOINT
Definition Enums.hpp:144
dtype clip(dtype inValue, dtype inMinValue, dtype inMaxValue)
Definition clip.hpp:49
Axis
Enum To describe an axis.
Definition Enums.hpp:36
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
@ NONE
Definition Enums.hpp:37
NdArray< double > percentile(const NdArray< dtype > &inArray, double inPercentile, Axis inAxis=Axis::NONE, InterpolationMethod inInterpMethod=InterpolationMethod::LINEAR)
Definition percentile.hpp:63
NdArray< uint32 > argmin(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition argmin.hpp:46
@ SHELL
Definition Enums.hpp:58
NdArray< dtypeOut > astype(const NdArray< dtype > inArray)
Definition astype.hpp:43
std::uint32_t uint32
Definition Types.hpp:40