0% found this document useful (0 votes)
545 views

NumSharp CheatSheet

This document provides a summary of key NumPy array operations in NumSharp including: - Creating 1D, 2D and 3D arrays - Inspecting array properties like shape, size, dtype - Subsetting, slicing and indexing arrays to select elements or subgroups - Performing element-wise arithmetic operations like addition, subtraction, multiplication and division on arrays - Transposing and changing the shape of arrays
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
545 views

NumSharp CheatSheet

This document provides a summary of key NumPy array operations in NumSharp including: - Creating 1D, 2D and 3D arrays - Inspecting array properties like shape, size, dtype - Subsetting, slicing and indexing arrays to select elements or subgroups - Performing element-wise arithmetic operations like addition, subtraction, multiplication and division on arrays - Transposing and changing the shape of arrays
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

C# For Data Science InspectingYour Array Subsetting, Slicing, Indexing

c.shape Array dimensions NDArray a = new double[]{1,2,3};

NumSharp Basics - Cheat Sheet c.shape[0]


c.ndim
Length of array
N umber of array dimensions
NDArray b = new double[,]{{1.5, 2, 3}, {4, 5.2, 6}, {0, -2, 2.3}};
NDArray c = new double[,,]{{{1.5,2,3}, {4,5,6}}, {{3,2,1}, {4,5,6}}};
Brought to you by https://3rdman.de c.size N umber of array elements
c.dtype Data type of array elements Subsetting
NumSharp c.dtype.ToString()
d = c.astype(np.int16);
N ame of data type
Convert an array to a different type
a[2].ToString();
3
Select the element at the
2nd index
(https://github.com/SciSharp/NumSharp) b[1,2].ToString(); Select the element at row 1

NumSharp is a C# port of NumPy,


AskingFor Help 6 column 2(same asb[1][2])

https://scisharp.github.io/NumSharp/api/index.html Slicing
targetting .NET Standard. It therefore works on multiple b["0:2"].ToString(); [[1.5, 2, 3], Select items at index 0 and 1
platforms and is a fundamental package needed for Array Mathematics b["0:2,1"].ToString();
[4, 5.2, 6]]
Select items at rows 0 and
scientific computing with C#. ArithmeticOperations [2, 5.2] 1, in column 1
NDArray a = new double[,]{{1,2,3},{4,5,6}}; b[":1"].ToString(); Select all items at row 0
NumPyArrays NDArray b = new double[,]{{3,2,1},{6,5,4}};
NDArray c = new double[]{3,2,1};
[[1.5, 2, 3]]
var g = a - b; g.ToString(); Subtraction
c["1,..."].ToString(); [[3, 2, 1], Same as [ 1, : , : ]
1D array 2D array 3D array [4, 5, 6]]
[[-2, 0, 2],
axis 1 [-2, 0, 2]]
axis 1 np.subtract(a,b); Subtraction
a["::-1"].ToString() Reversed array a
1 2 3 axis 0 [3, 2, 1]
(b + a).ToString(); Addition
1 2 3 [[4, 4, 4],
axis 0 [10, 10, 10]]
4 5 6
axis 2 np.add(b,a); Addition Array Manipulation
(a / b).ToString();
[[0.3333333333333333, 1, 3],
Division TransposingArray
CreatingArrays [0.6666666666666666, 1, 1.5]] np.transpose(b).ToString();
[[1.5, 4, 0],
Permute array dimensions
NDArray a = new double[]{1,2,3}; np.divide(a,b); Division
(a * b).ToString(); Multiplication [2, 5.2, -2],
NDArray b = new double[,]{{1,2,3},{4,5,6}}; [3, 6, 2.3]]
[[3, 4, 3],
NDArray c = new double[,,]{{{1.5,2,3}, {4,5,6}},
{{3,2,1}, {4,5,6}}};
[24, 25, 24]] ChangingArrayShape
np.multiply(a,b); Multiplication b.ravel().ToString(); Flatten the array
np.exp(b); Exponentiation
Initial Placeholders np.sqrt(b); Square root
[1.5, 2, 3, 4, 5.2, 6, 0, -2, 2.3]
var a = np.zeros((3,4)); Create an array of zeros np.sin(a); Print sines of an array
b.reshape(9,1).ToString(); [[1.5], Reshape, but don’t change data
var a = np.ones((2,3,4), np.int16); Create an array of ones np.cos(b); Element-wise cosine [2],
var d = np.arange(10,25,5); Create an array of evenly np.log(a); Element-wise natural logarithm [3],
spaced values (step value) a.dot(c).ToString(); Dot product [4],
var b = np.linspace(0,2,9); Create an array of evenly [10, 28]
spaced values (number of samples)
[5.2],
var e = np.full(7, (2, 2)); Create a constant array Comparison [6],
[0],
var f = np.eye(2); Create a 2X 2 identity matrix (a == b).ToString(); Element- w ise comparison
NDArray a = np.random Create an array with random [[False, True, False], [-2],
.randint(0, 100, (2,2)) int values [False, True, False]] [2.3]]
.astype(typeof(double)); Element-wise comparison
a < 2, a > 3 etc.
var a = np.empty((3,2)); Create an empty array NumSharp does not support all CombiningArrays
! comparison operators.
Check out the notebook (see end of the
cheat sheet) for a potential workaround. NDArray a = new double[]{1,2,3};
I/O np.array_equal(a, b); Array- w ise comparison NDArray b = new double[,]{{1.0, 1.1, 1.2},{2.1, 2.2, 2.3}};
NDArray c = new double[,]{{3.2, 3.3, 3.4},{4.3, 4.4, 4.5}};
Saving&LoadingOnDisk AggregateFunctions
np.Save((Array)a, "my_array.npy"); a.sum(); Array- w ise sum np.concatenate((a, new double[] Concatenate arrays
NDArray z = np.Load<double[]>("my_array.npy"); a.min(); Array- w ise minimum value {9, 8, 7})).ToString();
var dict = new Dictionary<string, Array>(); a.max(0); M aximum value of an array row [1, 2, 3, 9, 8, 7]
dict.Add("A", (Array)a); b.cumsum(axis:1); Cumulative sum of the elements
dict.Add("B", (Array)b); a.mean(); M ean
np.Save_Npz(dict, "array.npz"); np.vstack(c, b).ToString(); Stack arrays vertically (row-wise)
Saving&LoadingTextFiles
NpzDictionary<Array> load_dict =
np.std(b); Standard deviation
[[3.2, 3.3, 3.4],
np.Load_Npz<Array>("array.npz"); [4.3, 4.4, 4.5],
CopyingArrays [1, 1.1, 1.2],
[2.1, 2.2, 2.3]]
DataTypes var h = a.view();
np.copy(a);
Create a view of the array w ith the same data
Create a copy of the array np.hstack(b, c).ToString(); Stack arrays horizontally (column-wise)
np.bool_, np.bool8, np.@bool System.Boolean [[1, 1.1, 1.2, 3.2, 3.3, 3.4],
np.@byte, np.uint8, np.ubyte System.Byte var h = a.copy(); Create a deep copy of the array [2.1, 2.2, 2.3, 4.3, 4.4, 4.5]]
np.int16, np.uint16 System.Int16, System.UInt16
np.int32, np.uint32 System.Int32, System.UInt32 SortingArrays
np.int_, np.int64, np.int0 System.Int64
np.uint64, np.uint0, np.@uint System.UInt64 NumSharp does not seem to support sort(). It provides argsort for one dimensional Please visit the GitHub project site
arrays however.
np.float32 System.Single https://github.com/indy-3rdman/numsharp-cheatsheet
np.float_, np.float64, np.@double System.Double var x = np.array(new double[] {3.0, 1.0, 2.0});
np.@decimal System.Decimal var i = x.argsort<double>(); for more details and to try the interactive notebook.
np.@char System.Char i.ToString()
np.complex_, np.complex64, np.complex128 System.Numerics.Complex [1, 2, 0]

You might also like