 |
NumCpp
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.compare(
"linear") != 0 &&
74 inInterpMethod.compare(
"lower") != 0 &&
75 inInterpMethod.compare(
"higher") != 0 &&
76 inInterpMethod.compare(
"nearest") != 0 &&
77 inInterpMethod.compare(
"midpoint") != 0)
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)
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 int32 i =
static_cast<int32>(
std::floor(
static_cast<double>(numNonNan - 1) * inPercentile / 100.0));
130 const uint32 indexLower =
static_cast<uint32>(clip<uint32>(i, 0, numNonNan - 2));
134 if (inInterpMethod.compare(
"linear") == 0)
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;
144 else if (inInterpMethod.compare(
"lower") == 0)
149 else if (inInterpMethod.compare(
"higher") == 0)
154 else if (inInterpMethod.compare(
"nearest") == 0)
156 const double percent = inPercentile / 100.0;
157 const double percent1 =
static_cast<double>(indexLower) /
static_cast<double>(numNonNan - 1);
158 const double percent2 =
static_cast<double>(indexLower + 1) /
static_cast<double>(numNonNan - 1);
159 const double diff1 = percent - percent1;
160 const double diff2 = percent2 - percent;
162 switch (argmin<double>({ diff1, diff2 }).item())
176 else if (inInterpMethod.compare(
"midpoint") == 0)
178 NdArray<dtype> returnArray = { (arrayCopy[indexLower] + arrayCopy[indexLower + 1]) / 2.0 };
195 for (
uint32 row = 0; row < inShape.
rows; ++row)
200 if (outValue.
size() == 1)
202 returnArray[row] = outValue.
item();
218 for (
uint32 row = 0; row < inShape.
rows; ++row)
223 if (outValue.
size() == 1)
225 returnArray[row] = outValue.
item();
value_type item() const
Definition: NdArrayCore.hpp:2950
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4296
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:4591
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:4310
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