Numpy Arrays : operations and properties

Arrays in Numpy Example

Arrays in numpy are obtained using the object np.array(), it is capable of creating n-dimensional array depending on the type of data you feed in.

The following examples show how to create a

  • One dimensional array
  • Two-dimensional array
  • Three-dimensional array

As mentioned in the previous article, numpy basic data structure is numpy array and numpy. Here we will try to generate random arrays using numpy. for this, we will be using the following function

np.random.seed(0) np.random.randint()

the np.random.randint() object takes input and creates arrays of various size

Properties of narray

  • ndim– the number of dimension of the array ie, no 1 dimension, 2 dimensions 3 dimensions etc
  • shape– the size of each dimension ie, no of rows, columns etc for 2d
  • size– the total size of the array ie. no of elements in the array


Accessing elements in an array

Numpy arrays work similar to tuples and lists and can be iterated.

Array Indexing: Accessing Single Elements

In the following example, we are creating a 2-dimensional array with 9 elements.

  • Element in row 1, col 1 d2_array[0][0]result 1
  • Element in row 1, col 1 d2_array[1][0]result 4
  • Element in row 3, col 3 d2_array[2][2]result 9

Note: You can also get the same results by using d2_array[i,j] in the above example instead of using [i][j] and get the same results.

Accessing Subarrays using Slicing

to access subarrays with the slice notation, marked by the colon (:) character. The NumPy slicing syntax follows that of the standard Python list. general template is x[start:stop:step]

These types of slicing operations are very common when we start dealing with data sets and when we dive in deeper working with datasets, we will use pandas to load CSV files into arrays and use a lot of inbuilt functions to clean and manipulate data for particular data science problem.


Slicing of numpy arrays gives views rather than copies of the array data. This is one area in which NumPy array slicing differs from Python list slicing. If you change data values in views, you are also changing values in master data.

if you want to make a copy of data, make use of x =some_numpy_array[1:3].copy() function

Phone: 512-539-0390
NJ Training Academy Inc , 405 Dry Gulch Bend
Cedar Park, Texas, 78613