Analytics Vidhya
Published in

Analytics Vidhya

The Basics of Numpy Arrays

Data manipulation in Python is nearly synonymous with Numpy array manipulation, even newer tools like Pandas are built around the Numpy array. This section will present several examples using Numpy and manipulation to access data and subarrays, and to split, reshape and join arrays.

Let’s start by defining three random arrays: a one-dimensional, two-dimensional, and three dimensional array. We’ll use Numpy’s random number generator, which we will seed with a set value in order to ensure that the same random arrays are generated each time this code is run.

In order to go through the Introduction of Numpy follow the below link:

import numpy as npnp.random.seed(0) # seed for reproducibilityx1 = np.random.randint(10, size=(3,4,5))

Each array has attributes ndim(the number of dimensions), shape(the size of each dimension), size(the total size of array), dtype(the datatype of array), itemsize(the size of each array element), and nbytes(the total size of array).

print("x1 ndim: ", x1.ndim)  #printing number of dimensions of array
print("x1 shape: ", x1.shape) #printing the size of each dimension
print("x1 size: ", x1.size) #printing total size of array
print("dtype: ", x1.dtype) # printing the data type of array
print("itemsize: ", x1.itemsize) #printing size of each array element
print("nbytes: ", x1.nbytes) #printing the total size of array

Array Indexing: Accessing single elements

If you are familiar with the indexing in Python lists, the same is the case with one-dimensional array indexing of Numpy array.

x2=np.array([5, 0, 3, 3, 7, 9])
x2[0] # print first element
x2[-1] #print last element

In case of multidimensional arrays, you can access items using a comma-separated tuple of indices:

x3=np.array([[3, 5, 2, 4],
[7, 6, 8, 8],
[1, 6, 7, 7]])
x3[0,0] #print first element
x3[2, -1] #print last element of 2nd row

Modifying values of array:

x3[0, 0] = 12

Array Slicing: Accessing Subarrays

One dimensional Subarrays

The Numpy slicing syntax follows that of the standard Python list; to access a slice of an array x, use this:

x[start:stop:step]

x = np.arange(10)
x[:5] # first 5 elements
x[5:] #elements after index 5
x[::2] #every alternate element
x[::-1] # reversed array

Multidimensional subarrays

Multidimensional slices work in the same way, with multiple slices separated by commas.

x3=np.array([[3, 5, 2, 4],
[7, 6, 8, 8],
[1, 6, 7, 7]])
x3[:2, :3] #two rows, three columns
x3[:3, ::2] #all rows, every alternate column
x3[::-1, ::-1] # subarray dimensions reversed together
x3[:, 0] #first column of x3
x3[0, :] # first row of x2

Reshaping of Arrays:

The most flexible way of reshaping is to use reshape() method.

grid = np.arange(1, 10).reshape((3, 3))

Array Concatenation

Concatenation, or joining of two arrays in Numpy, is primarily accomplished through the routines np.concatenate, np.vstack and np.hstack.

x=np.array([1, 2, 3])
y=np.array([3, 2, 1])
np.concatenate([x,y])

Vertically stack the arrays

x=np.array([1, 2, 3])
grid=np.array([[9, 8, 7],
[6, 5, 4]])
# vertically stack the arrays
np.vstack([x, grid])

Horizontally stack the arrays

grid=np.array([[9, 8, 7],
[6, 5, 4]])
y=np.array([[99],
[99]])
np.hstack([grid, y])

Splitting of Arrays:

The opposite of concatenation is splitting, which is implemented by the functions np.split

x=[1, 2, 3, 99, 99, 3, 2, 1]
x1, x2, x3 = np.split(x, [3, 5])
print(x1, x2, x3)

In order to access the entire code, follow the below link:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store