pytutorial/numpy/ndarray
David Rotermund 905809396f
Update README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
2023-12-15 00:41:56 +01:00
..
README.md Update README.md 2023-12-15 00:41:56 +01:00

The N-dimensional array (ndarray)

{:.no_toc}

* TOC {:toc}

The goal

Class has a very important job as a core container type in Python. It is really hard to find a good overview how to use them in a good practice manner.

Questions to David Rotermund

Chaining of (ndarray) methods

import numpy as np
a = np.ones((3, 3))
b = a.mean(axis=1).max()
print(b) # -> 1.0

numpy.ndarray.fill

ndarray.fill(value)

Fill the array with a scalar value.

import numpy as np
A = np.ones((3, 3))
A.fill(7)
print(A)

Output:

[[7. 7. 7.]
 [7. 7. 7.]
 [7. 7. 7.]]

numpy.ndarray.ndim

ndarray.ndim

Number of array dimensions.

import numpy as np

A = np.ones((3, 3))
print(A.ndim)  # -> 2

numpy.ndarray.shape

ndarray.shape

Tuple of array dimensions.

import numpy as np

A = np.ones((3, 3))
print(A.shape)  # -> (3, 3)

numpy.ndarray.size

ndarray.size

Number of elements in the array.

import numpy as np

A = np.ones((3, 3))
print(A.size) # -> 9

Array methods

Array conversion

ndarray.item(*args) Copy an element of an array to a standard Python scalar and return it.
ndarray.tolist() Return the array as an a.ndim-levels deep nested list of Python scalars.
ndarray.itemset(*args) Insert scalar into an array (scalar is cast to array's dtype, if possible)
ndarray.tostring([order]) A compatibility alias for tobytes, with exactly the same behavior.
ndarray.tobytes([order]) Construct Python bytes containing the raw data bytes in the array.
ndarray.tofile(fid[, sep, format]) Write array to a file as text or binary (default).
ndarray.dump(file) Dump a pickle of the array to the specified file.
ndarray.dumps() Returns the pickle of the array as a string.
ndarray.astype(dtype[, order, casting, ...]) Copy of the array, cast to a specified type.
ndarray.byteswap([inplace]) Swap the bytes of the array elements
ndarray.copy([order]) Return a copy of the array.
ndarray.view([dtype][, type]) New view of array with the same data.
ndarray.getfield(dtype[, offset]) Returns a field of the given array as a certain type.
ndarray.setflags([write, align, uic]) Set array flags WRITEABLE, ALIGNED, WRITEBACKIFCOPY, respectively.
ndarray.fill(value) Fill the array with a scalar value.

Shape manipulation

ndarray.reshape(shape[, order]) Returns an array containing the same data with a new shape.
ndarray.resize(new_shape[, refcheck]) Change shape and size of array in-place.
ndarray.transpose(*axes) Returns a view of the array with axes transposed.
ndarray.swapaxes(axis1, axis2) Return a view of the array with axis1 and axis2 interchanged.
ndarray.flatten([order]) Return a copy of the array collapsed into one dimension.
ndarray.ravel([order]) Return a flattened array.
ndarray.squeeze([axis]) Remove axes of length one from a.

Item selection and manipulation

ndarray.take(indices[, axis, out, mode]) Return an array formed from the elements of a at the given
ndarray.put(indices, values[, mode]) Set a.flat[n] = values[n] for all n in indices.
ndarray.repeat(repeats[, axis]) Repeat elements of an array.
ndarray.choose(choices[, out, mode]) Use an index array to construct a new array from a set of choices.
ndarray.sort([axis, kind, order]) Sort an array in-place.
ndarray.argsort([axis, kind, order]) Returns the indices that would sort this array.
ndarray.partition(kth[, axis, kind, order]) Rearranges the elements in the array in such a way that the value of the element in kth position is in the position it would be in a sorted array.
ndarray.argpartition(kth[, axis, kind, order]) Returns the indices that would partition
ndarray.searchsorted(v[, side, sorter]) Find indices where elements of v should be inserted in a to maintain order.
ndarray.nonzero() Return the indices of the elements that are non-zero.
ndarray.compress(condition[, axis, out]) Return selected slices of this array along given axis.
ndarray.diagonal([offset, axis1, axis2]) Return specified diagonals.

Calculation

ndarray.max([axis, out, keepdims, initial, ...]) Return the maximum along a given axis.
ndarray.argmax([axis, out, keepdims]) Return indices of the maximum values along the given axis.
ndarray.min([axis, out, keepdims, initial, ...]) Return the minimum along a given axis.
ndarray.argmin([axis, out, keepdims]) Return indices of the minimum values along the given axis.
ndarray.ptp([axis, out, keepdims]) Peak to peak (maximum - minimum) value along a given axis.
ndarray.clip([min, max, out]) Return an array whose values are limited to [min, max].
ndarray.conj() Complex-conjugate all elements.
ndarray.round([decimals, out]) Return a with each element rounded to the given number of decimals.
ndarray.trace([offset, axis1, axis2, dtype, out]) Return the sum along diagonals of the array.
ndarray.sum([axis, dtype, out, keepdims, ...]) Return the sum of the array elements over the given axis.
ndarray.cumsum([axis, dtype, out]) Return the cumulative sum of the elements along the given axis.
ndarray.mean([axis, dtype, out, keepdims, where]) Returns the average of the array elements along given axis.
ndarray.var([axis, dtype, out, ddof, ...]) Returns the variance of the array elements, along given axis.
ndarray.std([axis, dtype, out, ddof, ...]) Returns the standard deviation of the array elements along given axis.
ndarray.prod([axis, dtype, out, keepdims, ...]) Return the product of the array elements over the given axis
ndarray.cumprod([axis, dtype, out]) Return the cumulative product of the elements along the given axis.
ndarray.all([axis, out, keepdims, where]) Returns True if all elements evaluate to True.
ndarray.any([axis, out, keepdims, where]) Returns True if any of the elements of a evaluate to True.

Arithmetic, matrix multiplication, and comparison operations

Each of the arithmetic operations (+, -, *, /, //, %, divmod(), ** or pow(), <<, >>, &, ^, |, ~) and the comparisons (==, <, >, <=, >=, !=) is equivalent to the corresponding universal function (or ufunc for short) in NumPy.

for in-place operations see here

ndarray.__lt__(value, /) Return self<value.
ndarray.__le__(value, /) Return self<=value.
ndarray.__gt__(value, /) Return self>value.
ndarray.__ge__(value, /) Return self>=value.
ndarray.__eq__(value, /) Return self==value.
ndarray.__ne__(value, /) Return self!=value.
ndarray.__bool__(/) True if self else False
ndarray.__neg__(/) -self
ndarray.__pos__(/) +self
ndarray.__abs__(self)
ndarray.__invert__(/) ~self
ndarray.__add__(value, /) Return self+value.
ndarray.__sub__(value, /) Return self-value.
ndarray.__mul__(value, /) Return self*value.
ndarray.__truediv__(value, /) Return self/value.
ndarray.__floordiv__(value, /) Return self//value.
ndarray.__mod__(value, /) Return self%value.
ndarray.__divmod__(value, /) Return divmod(self, value).
ndarray.__pow__(value[, mod]) Return pow(self, value, mod).
ndarray.__lshift__(value, /) Return self<<value.
ndarray.__rshift__(value, /) Return self>>value.
ndarray.__and__(value, /) Return self&value.
ndarray.__or__(value, /) Return self
ndarray.__xor__(value, /) Return self^value.
ndarray.__matmul__(value, /) Return self@value.

Special methods

special methods