Numpy Crash Course (Part 1)

Faiza Anan Noor
Analytics Vidhya
Published in
5 min readDec 9, 2020

In this story, I’ll try to cover some of the basic operations of Numpy. So this will kind of act as a revision/cheatsheet sheet for people already familiar with Numpy or if they just want to brush up on their skills or existing knowledge on it. This is just part 1 though which deals with the following things.

Table of contents:

Creating a numPy object

Accessing numPy objects/arrays

Different types of numPy arrays

— Working with random values

— Reshaping Arrays

So without further ado, lets get into it!

CREATING A NUMPY OBJECT

Creating a 1D array

This is actually an example of a 1D array. We’ll see how we can make numPy arrays of other dimensions as well now.

Creating 2D,3D,0D arrays

Creating arrays of different dimensions

Checking out properties of our numPy array

Creating a numPy array specifiying the properties

Creating an array specifying the dimension:

Specifying dimension

Creating an array specifying the data type:

Specifying data type

ACCESSING NUMPY ARRAY ELEMENTS

Accessing elements of an array

In the first example, we are accessing the index 2 and index 3 of arr3 and then printing out the summation of their values.

In the second example, we are accessing the row 0,column 1 of the 2d array

Let’s examine what’s happening here by analyzing the 3rd example that is

arr5[0,1,2]

So here the first term 0 refers to the first element of the 3d array which is

[[1.1,2.2,3.3],[4.4,5.5,6.6]]

The second term 1 accesses the 2nd element of this 2d array which is

[4.4,5.5,6.6]

Finally, the last term 2 refers to the 3rd index which is the index 2 which refers to 6.6

FINAL OUTPUT: 6.6

Similarly,

Let’s examine what’s happening here by analyzing the 2nd example that is

arr5[1,1,2]

So here the first 1 refers to the first element of the 3d array which is

[[7.7,8.8,9.9],[10.1,11.1,12.1]]

The second 1 accesses the 2nd element of this 2d array which is

[10.1,11.1,12.1]

Finally, the last term 2 refers to the 3rd index which is the index 2 which refers to 12.1

FINAL OUTPUT: 12.1

Negative Indexing:

Negative Indexing

Here -1 refers to the last element. In other words, this is the first element from the right

Likewise, -2 refers to the 2nd last element and -3 refers to the 3rd last element

So for the example,

arr[1,-1]

We are accessing the 2nd element which is

[6,7,8,9,10]

and so -1 means that we are accessing 10

OUTPUT :10

Accessing array elements specifying the row, columns:

Using step value to access every other element:

Here in the first example, we are returning every other element

In the Second example, we are skipping 2 elements

Slicing Arrays:

Slicing Arrays

Replacing Array values with new ones:

Working with different kinds of arrays:

The data types and characters representing them are:

  • i - integer
  • b - boolean
  • u - unsigned integer
  • f - float
  • c - complex float
  • m - timedelta
  • M - datetime
  • O - object
  • S - string
  • U - unicode string
  • V - fixed chunk of memory for other type ( void )
OUTPUT

In the last example, conversion of a string type array to integer type isn’t possible and so it raises an error.

Working with Random Values:

Random values in numpy

Here in the first example, we are generating a random float value.

In the second one, we are generating a matrix of dimensions 2x4 with random values

Generating random integer values by specifying the dimension and the range
Generating random float values by specifying the dimension and the range

Shaping and reshaping arrays:

Reshaping arrays

But some conversions might cause an error.

Error in reshaping
Flattening an array

Check out the code for this story by clicking on this link

Part 2, Part 3 will be out soon, so stay tuned!

--

--

Faiza Anan Noor
Analytics Vidhya

A novice developer/programmer on a quest to explore the world of coding and development. It's tough, bumpy and scary. But that doesn't hold this lady back!