Numpy Crash Course (Part 1)
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:
— Accessing numPy objects/arrays
So without further ado, lets get into it!
CREATING A NUMPY OBJECT
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
Checking out properties of our numPy array
Creating a numPy array specifiying the properties
Creating an array specifying the dimension:
Creating an array specifying the data type:
ACCESSING NUMPY ARRAY ELEMENTS
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:
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:
Replacing Array values with new ones:
Working with different kinds of arrays:
The data types and characters representing them are:
i
- integerb
- booleanu
- unsigned integerf
- floatc
- complex floatm
- timedeltaM
- datetimeO
- objectS
- stringU
- unicode stringV
- fixed chunk of memory for other type ( void )
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:
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
Shaping and reshaping arrays:
But some conversions might cause an error.
Check out the code for this story by clicking on this link
Part 2, Part 3 will be out soon, so stay tuned!