Getting started with NumPy in Python.

numpy Jun 21, 2019

A step-by-step beginners guide to use NumPy Library with Python


Python is dynamically typed general purpose programming language and because of its flexible nature it is used extensively in Data Science. And that's where the NumPy Library comes into action. NumPy library is the foundation for most of the Data Analysis and Data Preparation for Machine Learning. It is a core array processing package and it gives us the ability to work with Multi Dimensional array objects.

Prerequisites

  • Setting Up Environment: We will be using JetBrains PyCharm for programming in python. Install PyCharm on your System and you will be ready to do basic programming in Python. Or you can also use Python Software Foundation's IDLE and install it on your System.
  • Installing NumPy on PyCharm:
  • Step 1: Launch PyCharm and navigate to Files > Settings or use the shortcut keys Ctrl + Alt + S. It should look like the image below.
Installing NumPy on PyCharm
  • Step 2: Click on the + icon on the right hand side pane to launch the available packages window or simple use the shortcut keys Alt + Insert. It should look like the image below.
  • Step 3: Now search for NumPy Package and click on Install Package. This will install the package on your system. It should look like the image below.
Installing NumPy on PyCharm
  • Installing NumPy on IDLE: For installing NumPy on IDLE we will use the pip command from the Command Prompt.
  • Step 1: Fire Up your Command Prompt and type the command line pip install numpy and hit enter. This will take some to install NumPy Package for your IDLE.

Creating arrays with NumPy

Now that we are set up with our IDE and NumPy Package, Lets start the fun part Lets Begin Coding...

As NumPy is mainly used for creating homogeneous multidimensional arrays, we will start by learning to create arrays with NumPy.

Methods of creating arrays:

  • array() Method: We can create array from regular python Lists or Tuple using the array() method. This will create an array of the type of values specified. We can see in the code below that the datatype of the array created is float because one of our elements is of float type it is also called implicit conversion. Note that all the values in array should be of same datatype, hence implicit conversion occurs.
#importing numpy
from numpy import *
#creating array
arr = array([1, 2, 3, 4, 5.0])
#printing array datatype
print(arr.dtype)
#printing array
print(arr)


Output:
float64
[1. 2. 3. 4. 5.]
Using array()
  • linspace() Method: This method creates an array with evenly spaced values within a given interval. In the code below we have specified the starting value 1, the ending value 20 and the total number of values 10. Here,  the datatype of array is float because the elements of the resultant array are of float type. The default value to create parts is 50.
#importing numpy
from numpy import *
#creating an array
arr = linspace(1, 20, 10)
#printing array datatype
print(arr.dtype)
#printing array
print(arr)


Output:
float64
[ 1.          3.11111111  5.22222222  7.33333333  9.44444444 11.55555556
 13.66666667 15.77777778 17.88888889 20.        ]
Using linspace()
  • arange() Method: This method also creates an array with evenly spaced values within a given interval but it takes an argument to skip the number of values.  In the code below we have specified the starting value 1, the ending value 15 and the number of values to skip 2. This creates an array with a difference of 2 between the consecutive elements.
#importing numpy
from numpy import *
#creating an array
arr = arange(1, 15, 2)
#print array data type
print(arr.dtype)
#print array
print(arr)


Output:
int32
[ 1  3  5  7  9  11  13]
Using arange()
  • logspace() Method: This method also creates an array with evenly spaced values within a given interval. In the code below, we have specified the starting value 1, the ending value 20 and the number of elements 10. This creates an array with 10 elements ranging from 101 to 1020 .
#importing numpy
from numpy import *
#creating an array
arr = logspace(1, 20, 10)
#print array datatype
print(arr.dtype)
#printing array
print(arr)


Output:
float64
[1.00000000e+01 1.29154967e+03 1.66810054e+05 2.15443469e+07
 2.78255940e+09 3.59381366e+11 4.64158883e+13 5.99484250e+15
 7.74263683e+17 1.00000000e+20]
Using logspace()
  • zeros() Method: This is a very efficient method of creating arrays, this method creates an array of specified size and initializes all the elements with 0. And the array is of float type.
#importing numpy
from numpy import *
#creating array
arr = zeros(5)
#print array data type
print(arr.dtype)
#print array
print(arr)


Output:
float64
[0. 0. 0. 0. 0.]
Using zeros()
  • ones() Method: This method creates an array of specified size and initializes all the elements with 1. And the array is of float type.
#importing numpy
from numpy import *
#creating an array
arr = ones(10)
#printing array data type
print(arr.dtype)
#printing array
print(arr)


Output:
float64
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
Using ones()

Aditya Kumar

A thinker, reader, developer and coffee maniac. I am an undergraduate student at UPES University, Dehradun.