/// <summary> /// Modify a sequence in-place by shuffling its contents. /// </summary> /// <param name="x">The array or list to be shuffled.</param> /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.shuffle.html <br></br>Does not copy <paramref name="x"/></remarks> public void shuffle(NDArray x) { var count = x.size; while (count-- > 1) { var swapAt = randomizer.Next(x); var tmp = x.GetAtIndex(0); x.SetAtIndex(x.GetAtIndex(swapAt), 0); x.SetAtIndex(tmp, swapAt); } }
public override NDArray Cast(NDArray nd, NPTypeCode dtype, bool copy) { if (dtype == NPTypeCode.Empty) { throw new ArgumentNullException(nameof(dtype)); } NDArray clone() => new NDArray(nd.Storage.Clone()); if (nd.Shape.IsEmpty) { if (copy) { return(new NDArray(dtype)); } nd.Storage = new UnmanagedStorage(dtype); return(nd); } if (nd.Shape.IsScalar || (nd.Shape.size == 1 && nd.Shape.NDim == 1)) { var ret = NDArray.Scalar(nd.GetAtIndex(0), dtype); if (copy) { return(ret); } nd.Storage = ret.Storage; return(nd); } if (nd.GetTypeCode == dtype) { //casting not needed return(copy ? clone() : nd); } else { //casting needed if (copy) { if (nd.Shape.IsSliced) { nd = clone(); } return(new NDArray(new UnmanagedStorage(ArraySlice.FromMemoryBlock(nd.Array.CastTo(dtype), false), nd.Shape))); } else { var storage = nd.Shape.IsSliced ? nd.Storage.Clone() : nd.Storage; nd.Storage = new UnmanagedStorage(ArraySlice.FromMemoryBlock(storage.InternalArray.CastTo(dtype), false), storage.Shape); return(nd); } } }
public void UniformMultipleSample() { // Generate a uniform random sample from np.arange(5) of size 3: // This is equivalent to np.random.randint(0,5,3) int low = 0; int high = 5; int nrSamples = 3; NDArray actual = np.random.choice(high, (Shape)nrSamples); Assert.AreEqual(actual.size, nrSamples, "Unexpected number of elements"); // Verify that all elements in output are within the range for (int i = 0; i < actual.size; i++) { Assert.IsTrue(actual.GetAtIndex <int>(i) >= low, "Element was less than expected"); Assert.IsTrue(actual.GetAtIndex <int>(i) < high, "Element was greater than expected"); } }
public void NonUniformSample() { // Generate a non-uniform random sample from np.arange(5) of size 3: int low = 0; int high = 5; int nrSamples = 3; double[] probabilities = new double[] { 0.1, 0, 0.3, 0.6, 0 }; NDArray actual = np.random.choice(5, (Shape)nrSamples, probabilities: probabilities); Assert.AreEqual(actual.size, nrSamples, "Unexpected number of elements"); // Verify that all elements in output are within the range for (int i = 0; i < actual.size; i++) { Assert.IsTrue(actual.GetAtIndex <int>(i) >= low, "Element was less than expected"); Assert.IsTrue(actual.GetAtIndex <int>(i) < high, "Element was greater than expected"); Assert.IsTrue(actual.GetAtIndex <int>(i) != 1, "Sampled zero-probability element"); Assert.IsTrue(actual.GetAtIndex <int>(i) != 4, "Sampled zero-probability element"); } }
public override NDArray ReduceProduct(NDArray arr, int? axis_, bool keepdims = false, NPTypeCode? typeCode = null) { //in order to iterate an axis: //consider arange shaped (1,2,3,4) when we want to summarize axis 1 (2nd dimension which its value is 2) //the size of the array is [1, 2, n, m] all shapes after 2nd multiplied gives size //the size of what we need to reduce is the size of the shape of the given axis (shape[axis]) var shape = arr.Shape; if (shape.IsEmpty || shape.size==0) return NDArray.Scalar(1, (typeCode ?? arr.typecode)); if (shape.IsScalar || (shape.size == 1 && shape.NDim == 1)) { var r = NDArray.Scalar(typeCode.HasValue ? Converts.ChangeType(arr.GetAtIndex(0), typeCode.Value) : arr.GetAtIndex(0)); if (keepdims) r.Storage.ExpandDimension(0); return r; } if (axis_ == null) { var r = NDArray.Scalar(product_elementwise(arr, typeCode)); if (keepdims) r.Storage.ExpandDimension(0); else if (!r.Shape.IsScalar && r.Shape.size == 1 && r.ndim == 1) r.Storage.Reshape(Shape.Scalar); return r; } var axis = axis_.Value; while (axis < 0) axis = arr.ndim + axis; //handle negative axis if (axis >= arr.ndim) throw new ArgumentOutOfRangeException(nameof(axis)); if (shape[axis] == 1) { //if the given div axis is 1 and can be squeezed out. if (keepdims) return new NDArray(arr.Storage.Alias()); return np.squeeze_fast(arr, axis); } //handle keepdims Shape axisedShape = Shape.GetAxis(shape, axis); var retType = typeCode ?? (arr.GetTypeCode.GetAccumulatingType()); //prepare ret var ret = new NDArray(retType, axisedShape, false); var iterAxis = new NDCoordinatesAxisIncrementor(ref shape, axis); var iterRet = new NDCoordinatesIncrementor(ref axisedShape); var iterIndex = iterRet.Index; var slices = iterAxis.Slices; //resolve the accumulator type #if _REGEN1 #region Compute switch (arr.GetTypeCode) { %foreach supported_numericals,supported_numericals_lowercase% case NPTypeCode.#1: { switch (retType) { %foreach supported_numericals,supported_numericals_lowercase,supported_numericals_onevales% case NPTypeCode.#101: { do { var slice = arr[slices]; var iter = slice.AsIterator<#2>(); var moveNext = iter.MoveNext; var hasNext = iter.HasNext; |#102 sum = #103; while (hasNext()) sum *= (#102) moveNext(); ret.Set#101(Converts.To#101(sum), iterIndex); } while (iterAxis.Next() != null && iterRet.Next() != null); break; }