numpy.ndarray Array

From GM-RKB
(Redirected from numpy.Array)
Jump to navigation Jump to search

A numpy.ndarray Array is a Python array that is a NumPy data structure.



References

2018d

  • https://github.com/pytorch/pytorch/blob/v0.3.1/README.md
    • QUOTE: ... Usually one uses PyTorch either as:
      • a replacement for NumPy to use the power of GPUs.
      • a deep learning research platform that provides maximum flexibility and speed
    • If you use NumPy, then you have used Tensors (a.k.a ndarray). PyTorch provides Tensors that can live either on the CPU or the GPU, and accelerate compute by a huge amount.

2014

  • (Wikipedia, 2014) ⇒ http://en.wikipedia.org/wiki/NumPy#The_ndarray_data_structure Retrieved:2014-8-3.
    • The core functionality of NumPy is its "ndarray", for n-dimensional array, data structure. These arrays are strided views on memory. In contrast to Python's built-in list data structure (which, despite the name, is a dynamic array), these arrays are homogeneously typed: all elements of a single array must be of the same type.

      Such arrays can also be views into memory buffers allocated by C, C++. Cython and Fortran extensions to the CPython interpreter without the need to copy data around, giving a degree of compatibility with existing numerical libraries. This functionality is exploited by the SciPy package, which wraps a number of such libraries (notably BLAS and LAPACK). NumPy has built-in support for memory-mapped ndarrays.


2014

2014

 >>> from numpy  import *
 >>> a = array( [2,3,4] )
 >>> a
 array([2, 3, 4])
 >>> a.dtype
 dtype('int32')
 >>> b = array([1.2, 3.5, 5.1])
 >>> b.dtype
 dtype('float64')

2013

  • http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html
    • n array object represents a multidimensional, homogeneous array of fixed-size items. An associated data-type object describes the format of each element in the array (its byte-order, how many bytes it occupies in memory, whether it is an integer, a floating point number, or something else, etc.)

      Arrays should be constructed using array, zeros or empty (refer to the See Also section below). The parameters given here refer to a low-level method (ndarray(...)) for instantiating an array.

      For more information, refer to the numpy module and examine the the methods and attributes of an array.

2013

  • http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html
    • An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N positive integers that specify the sizes of each dimension. The type of items in the array is specified by a separate data-type object (dtype), one of which is associated with each ndarray.

      As with other container objects in Python, the contents of an]]ndarray]] can be accessed and modified by indexing or slicing the array (using, for example, N integers), and via the methods and attributes of the]]ndarray]].

      Different ndarrays can share the same data, so that changes made in one ndarray may be visible in another. That is, an ndarray can be a “view” to another ndarray, and the data it is referring to is taken care of by the “base” ndarray. ndarrays can also be views to memory owned by Python strings or objects implementing the buffer or array interfaces.

Example(s)

Import library
import numpy as np
Empty array


Populate manually
xx = np.array([-2, 1.5, 1.5e-10, 1500])
print (xx)
# >>> [ -2.00000000e+00   1.50000000e+00   1.50000000e-10   1.50000000e+03]
np.set_printoptions(suppress=True)
print (xx)
# >>> [   -2.      0.      1.5  1500.]
yy = zeros(3, Int)        # Create empty array ready to receive result
Populate from a file.
Loops
for i in range(0, 3):
  Matrix[i] = i * 2
print yy
>>>  [  4   8 -22]
Transforming into a string
Emptying
Random
#Generate 10 random integer numbers which are between 1 and 20(included).
I = np.random.randint(5, size=(1, 15))
# >>> 4 1 3 2 3 1 2 1 0 0 0 0 2 3 1
type(I)
# >>> numpy.ndarray
x = np.random.random(6)
print(x)
# [ 0.07837821  0.48002108  0.41274116  0.82993414  0.77610352  0.1023732]
np.set_printoptions(suppress=True)
print(x)
Printing
y=np.array([1.5e-10,1.5,1500])
print(y)
# >>> [  1.500e-10   1.500e+00   1.500e+03]
np.set_printoptions(suppress=True)
print(y)
# [    0.      1.5  1500.]