Python: Numpay – P1
Learning : Python Numpy – P1
Subject: Numpay and some basic commands
In coming several posts I will talk about the numpay library and how to use some of its functions. So first what is numpy? Definition: NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. Also known as powerful package for scientific computing and data manipulation in python. As any library or package in python we need to install it on our device (we will not go through this process)
Basic commands in numpy: First of all we need to import it in our code. so we will use
import numpy as np
To create a 1 dimensional array we can use verey easy way as:
# create an array using numpy array function. my_array = np.array([1, 2, 3,4,5])
Later we will create a random array of numbers in a range.
Now, to get the length of the array we can use len command as
len(my_array) Output: 5
To get the sum of all elements in the array we use..
np.sum(my_array)
And to get the maximum and minimum numbers in the array we use ..
# Get the maximum and minimum numbers in the array my_array = np.array([1, 2, 3,4,5]) np.max(my_array) [Output]: 5 np.min(my_array) [Output]: 1
Some time we may need to create an array with certain Number of elements only one’s, to do this we can use this commands:
#create array of 1s (of length 5) np.ones(5) Output: [ 1., 1., 1., 1., 1.]
The default data type will be float, if we want to change it we need to pass the the ‘dtype’ to the command like this :
#create array of 1s (of length 5) as integer: np.ones(5, dtype = np.int) Output: [ 1, 1, 1, 1, 1]
Code output:
![]() |
So far we work on a one dimensional array, in the next post we will cover some commands that will help us in the arrays with multiple dimensions.
:: numpy Commands::
Command | comment |
my_array = np.array([1,2,3,4,5]) | Create an array with 1 to 5 integer |
len(my_array) | Get the array length |
np.sum(my_array) | get the sum of the elements in the array my_array = np.array([1,2,3,4,5]) print(np.sum(my_array)) [Output]: 15 |
np.max(my_array) | # Get the maximum number in the array my_array = np.array([1, 2, 3,4,5]) max_num = np.max(my_array) [Output]: 5 |
np.min(my_array) | # Get the minimum number in the array my_array = np.array([1, 2, 3,4,5]) min_num = np.min(my_array) [Output]: 1 |
my_array = np.ones(5) Output: [ 1., 1., 1., 1., 1.] |
create array of 1s (of length 5) np.ones(5) Output: [ 1., 1., 1., 1., 1.] |
:: numpy Sessions ::
Sessions 1 | Sessions 2 | Sessions 3 | Sessions 4 |
To Download my Python code (.py) files Click-Here
-
November 10, 2019 at 8:44 amPython: Numpay – P2 | Ali's Photography Space...
-
November 13, 2019 at 10:47 amPython: Numpay – P3 | Ali's Photography Space...
-
November 13, 2019 at 10:55 amlearning – Temp | Ali's Photography Space...