 |
NumCpp
2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
|
Go to the documentation of this file.
61 template<
typename dtype>
63 Axis inAxis =
Axis::NONE,
const std::string& inInterpMethod =
"linear")
67 if (inPercentile < 0.0 || inPercentile > 100.0)
72 if (inInterpMethod !=
"linear" &&
73 inInterpMethod !=
"lower" &&
74 inInterpMethod !=
"higher" &&
75 inInterpMethod !=
"nearest" &&
76 inInterpMethod !=
"midpoint")
78 std::string errStr =
"input interpolation method is not a vaid option.\n";
79 errStr +=
"\tValid options are 'linear', 'lower', 'higher', 'nearest', 'midpoint'.";
89 for (
auto value : inArray)
102 for (
int32 i =
static_cast<int32>(inArray.
size()) - 1; i > -1; --i)
104 if (!
isnan(inArray[i]))
113 std::vector<double> arrayCopy;
115 for (
auto value : inArray)
119 arrayCopy.push_back(value);
124 if (arrayCopy.size() < 2)
129 const auto i =
static_cast<int32>(
std::floor(
static_cast<double>(numNonNan - 1) * inPercentile / 100.0));
130 const auto indexLower =
static_cast<uint32>(clip<uint32>(i, 0, numNonNan - 2));
134 if (inInterpMethod ==
"linear")
136 const double percentI =
static_cast<double>(indexLower) /
static_cast<double>(numNonNan - 1);
137 const double fraction = (inPercentile / 100.0 - percentI) /
138 (
static_cast<double>(indexLower + 1) /
static_cast<double>(numNonNan - 1) - percentI);
140 const double returnValue = arrayCopy[indexLower] + (arrayCopy[indexLower + 1] - arrayCopy[indexLower]) * fraction;
145 if (inInterpMethod ==
"lower")
151 if (inInterpMethod ==
"higher")
157 if (inInterpMethod ==
"nearest")
159 const double percent = inPercentile / 100.0;
160 const double percent1 =
static_cast<double>(indexLower) /
static_cast<double>(numNonNan - 1);
161 const double percent2 =
static_cast<double>(indexLower + 1) /
static_cast<double>(numNonNan - 1);
162 const double diff1 = percent - percent1;
163 const double diff2 = percent2 - percent;
165 switch (argmin<double>({ diff1, diff2 }).item())
180 if (inInterpMethod ==
"midpoint")
182 NdArray<dtype> returnArray = { (arrayCopy[indexLower] + arrayCopy[indexLower + 1]) / 2.0 };
197 for (
uint32 row = 0; row < inShape.
rows; ++row)
202 if (outValue.
size() == 1)
204 returnArray[row] = outValue.
item();
220 for (
uint32 row = 0; row < inShape.
rows; ++row)
225 if (outValue.
size() == 1)
227 returnArray[row] = outValue.
item();
value_type item() const
Definition: NdArrayCore.hpp:2975
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4356
std::int32_t int32
Definition: Types.hpp:36
NdArray< dtype > nanpercentile(const NdArray< dtype > &inArray, double inPercentile, Axis inAxis=Axis::NONE, const std::string &inInterpMethod="linear")
Definition: nanpercentile.hpp:62
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:52
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4652
std::uint32_t uint32
Definition: Types.hpp:40
dtype floor(dtype inValue) noexcept
Definition: floor.hpp:48
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:40
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:43
size_type size() const noexcept
Definition: NdArrayCore.hpp:4370
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1487
Axis
Enum To describe an axis.
Definition: Types.hpp:46
const double nan
NaN.
Definition: Constants.hpp:44
Definition: Coordinate.hpp:44
uint32 rows
Definition: Core/Shape.hpp:44
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1143
void sort(RandomIt first, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:629
bool isnan(dtype inValue) noexcept
Definition: isnan.hpp:51