Wednesday, January 5, 2022

How to create and process Arrays using various functions of numpy package in Python Program

Python Program to create and process arrays using various functions of numpy package
The 'numpy' is a package in Python that contains several classes, functions, variables etc. to deal with scientific calculations. It also contains the 
a large library of mathematical functions like linear algebra.
We can work with Arrays by using the 'numpy' package as well. The 'numpy' is useful to create and process n dimensional (single or multi) arrays as well.
To work with 'numpy' in Python, we should first import it into our program by the following ways:
Method1:
import numpy

Method2:
import numpy as np

Method3:
from numpy import*

We can create Arrays in several ways, using the below functions provided by 'numpy' package.
  • array() 
  • linespace()
  • logspace()
  • arrange()
  • zeros()
  • ones()
1. array() : 
We can create an array by call the array() function from 'numpy' package in Python.
While defining the array with this function, we can specify the datatype of the elements  as 'int', 'float', or 'str' . For character type elements, no type specification is required.

However we may not required to specify the type as Python will assess the data type of elements.

Syntax:
arrayname = array([elements], datatype)

Example :
# Array of integer type elements
ary1 = array([10, 20, 30, 40, 50], int)
# Array of float type elements
ary2 = array([1.5, 2.5, 3, 4, 5.3], float)
# Array of character type elements
ary3 = array(['a', 'c', 'f', 'h', 'k'])
# Array of string type elements
ary4 = array(['Mahindra', 'Nike', 'Rolex', 'Omega', 'Tata'], dtype=str)

2. linespace() : 
By using the linespace() function, we can create an array with evenly spaced points between a starting point and ending point with 'n' number of parts of the elements based on start and end range.
Syntax:
arrayname = linespace(start, end, n)

start - represents the starting element
end - represents the ending element
n - represents the number of parts the elements should be divided. If 'n' is omitted, then the it takes the default value as 50

Example:
In the following, we are creating an array with start element as 1, end elements as 10 with 7 equally divided elements based on start and end range. Please note that start and end range is inclusive.

ary1 = linspace(1, 10, 7)
print(ary1)

n1 = len(ary1)
for i in range(n1):
    print('%.1f' % ary1[i], end=' ') # print array elements with one decimal

This array will return the array with 7 equally divided elements based on start and end range.
Output:
[ 1.   2.5  4.   5.5  7.   8.5 10. ]

1.0 2.5 4.0 5.5 7.0 8.5 10.0 

3. logspace() : 
The logspace() function is similar to linespace() function, however, this function will create an array with evenly spaced points on a logarithmically spaced scale.
Syntax:
arrayname = logspace(start, stop, n)

The logspace() function starts a value which is 10 to the power of start and end a value at the 10 to the power of end , and 'n' is the number of parts of the elements based on start and end range. If 'n' values is not specified, then its value is take as 50.

Example:
ary2 = logspace(1, 10, 5)
print('The array with logspace: ', ary2)

n2 = len(ary2)
for i in range(n2):
    print('%.1f' % ary2[i], end=' ')  # print array elements with one decimal

Output:
[1.00000000e+01 1.77827941e+03 3.16227766e+05 5.62341325e+07
 1.00000000e+10]

10.0 1778.3 316227.8 56234132.5 10000000000.0 

4. arrange() : 
The arrange() function of 'numpy' is similar to the range() function in Python. This creates the array with elements from 'start' to one element prior to the 'stop' with given step size. if the step size is omitted, by default it takes as 1. If the start value is omitted, then it will take 0 as the default value.
Syntax:
arrayname = arrange(start, stop, stepsize)

Example:
ary1 = arange(1, 10, 1.5)
print('\nThe array1 with arange: ', ary1)
n1 = len(ary1)
for i in range(n1):
    print('%.1f' % ary1[i], end=' ')

ary2 = arange(10)
print('\nThe array2 with arange: ', ary2)  # print array elements with one decimal

Output:
The array1 with arange:  [1.  2.5 4.  5.5 7.  8.5]
1.0 2.5 4.0 5.5 7.0 8.5 
The array2 with arange:  [0 1 2 3 4 5 6 7 8 9]

5. zeros() and ones() functions:
The zeros() function will create an array with all zeros and ones() function will create an array with all 1s.
Syntax(s):
zeros(n, datatype)
ones(n, datatype)

n - represents the no.of elements
datatype - it is an optional. if we not specify, then default data type use by 'numpy' is float.

Example:
n1 = zeros(5, int)
n2 = ones(5, int)
print('\nThe array with zeros: ', n1)
print('\nThe array with ones: ', n2)

Output:
The array with zeros:  [0 0 0 0 0]
The array with ones:  [1 1 1 1 1]

#--------------------------------------------------------------Thanks--------------------------------------------------------------#

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog