Numpy Arrays

Posted under » Python Data Analysis on 14 June 2023

From Numpy intro. we have talked about “ndarray,” which is shorthand for “N-dimensional array.” An N-dimensional array is simply an array with any number of dimensions. You might also hear 1-D, or one-dimensional array, 2-D, or two-dimensional array, and so on. The NumPy ndarray class is used to represent both matrices and vectors. A vector is an array with a single dimension (there’s no difference between row and column vectors), while a matrix refers to an array with two dimensions. For 3-D or higher dimensional arrays, the term tensor is also commonly used.

An array is usually a fixed-size container of items of the same type and size. The number of dimensions and items in an array is defined by its shape. The shape of an array is a tuple of non-negative integers that specify the sizes of each dimension.

Consider this 2 axis array

[[0., 0., 0.],
 [1., 1., 1.]]

This array has 2 axes. The first axis has a length of 2 and the second axis has a length of 3.

Array attributes reflect information intrinsic to the array itself. If you need to get, or even set, properties of an array without creating a new array, you can often access an array through its attributes.

Besides creating an array from a sequence of elements, you can easily create an array filled with 0’s, 1's or empty array. The function empty creates an array whose initial content is random and depends on the state of the memory. The reason to use empty over zeros (or something similar) is speed - just remember to fill every element later.

np.ones(2)
array([1., 1.])

np.empty(2) 
array([3.14, 42.  ])

An array that contains a range of evenly spaced intervals. To do this, you will specify the first number, last number, and the step size.

np.arange(2, 9, 2)
array([2, 4, 6, 8])

You can concatenate them with np.concatenate().

x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6]])
np.concatenate((x, y), axis=0)

array([[1, 2],
       [3, 4],
       [5, 6]])

Shape and size of array

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data