 |
NumCpp
2.1.0
A C++ implementation of the Python Numpy library
|
Go to the documentation of this file.
62 template<
typename dtype>
64 Axis inAxis =
Axis::NONE,
const std::string& inInterpMethod =
"linear")
68 if (inPercentile < 0.0 || inPercentile > 100.0)
73 if (inInterpMethod !=
"linear" &&
74 inInterpMethod !=
"lower" &&
75 inInterpMethod !=
"higher" &&
76 inInterpMethod !=
"nearest" &&
77 inInterpMethod !=
"midpoint")
79 std::string errStr =
"input interpolation method is not a vaid option.\n";
80 errStr +=
"\tValid options are 'linear', 'lower', 'higher', 'nearest', 'midpoint'.";
90 for (
auto value : inArray)
103 for (
int32 i =
static_cast<int32>(inArray.
size()) - 1; i > -1; --i)
105 if (!
isnan(inArray[i]))
114 std::vector<double> arrayCopy;
116 for (
auto value : inArray)
120 arrayCopy.push_back(value);
125 if (arrayCopy.size() < 2)
130 const auto i =
static_cast<int32>(
std::floor(
static_cast<double>(numNonNan - 1) * inPercentile / 100.0));
131 const auto indexLower =
static_cast<uint32>(clip<uint32>(i, 0, numNonNan - 2));
135 if (inInterpMethod ==
"linear")
137 const double percentI =
static_cast<double>(indexLower) /
static_cast<double>(numNonNan - 1);
138 const double fraction = (inPercentile / 100.0 - percentI) /
139 (
static_cast<double>(indexLower + 1) /
static_cast<double>(numNonNan - 1) - percentI);
141 const double returnValue = arrayCopy[indexLower] + (arrayCopy[indexLower + 1] - arrayCopy[indexLower]) * fraction;
146 if (inInterpMethod ==
"lower")
152 if (inInterpMethod ==
"higher")
158 if (inInterpMethod ==
"nearest")
160 const double percent = inPercentile / 100.0;
161 const double percent1 =
static_cast<double>(indexLower) /
static_cast<double>(numNonNan - 1);
162 const double percent2 =
static_cast<double>(indexLower + 1) /
static_cast<double>(numNonNan - 1);
163 const double diff1 = percent - percent1;
164 const double diff2 = percent2 - percent;
166 switch (argmin<double>({ diff1, diff2 }).item())
181 if (inInterpMethod ==
"midpoint")
183 NdArray<dtype> returnArray = { (arrayCopy[indexLower] + arrayCopy[indexLower + 1]) / 2.0 };
198 for (
uint32 row = 0; row < inShape.
rows; ++row)
203 if (outValue.
size() == 1)
205 returnArray[row] = outValue.
item();
221 for (
uint32 row = 0; row < inShape.
rows; ++row)
226 if (outValue.
size() == 1)
228 returnArray[row] = outValue.
item();
value_type item() const
Definition: NdArrayCore.hpp:2958
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4312
std::int32_t int32
Definition: Types.hpp:37
NdArray< dtype > nanpercentile(const NdArray< dtype > &inArray, double inPercentile, Axis inAxis=Axis::NONE, const std::string &inInterpMethod="linear")
Definition: nanpercentile.hpp:63
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:53
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4608
std::uint32_t uint32
Definition: Types.hpp:41
dtype floor(dtype inValue) noexcept
Definition: floor.hpp:49
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:44
size_type size() const noexcept
Definition: NdArrayCore.hpp:4326
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1491
Axis
Enum To describe an axis.
Definition: Types.hpp:47
const double nan
NaN.
Definition: Constants.hpp:45
Definition: Coordinate.hpp:45
uint32 rows
Definition: Core/Shape.hpp:45
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1147
void sort(RandomIt first, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:630
bool isnan(dtype inValue) noexcept
Definition: isnan.hpp:52