Data Science Economics

NumPy is a very useful library in python to work with arrays it is created in 2005 and also best to work on linear algebra and matrices. in this practical work we are going to solve some mathematical operation using NumPy library you can practice it on any supportive environment that best fitted for NumPy. we recommend you to practice on Jupiter notebook .

The first thing is to download NumPy library to solve problems.


Use code
 

import numpy as np 


we can go with any array
c = np.array ([10, 2, 30, 40,50])


The above is array that is represented by c
how we replaces  10 by 200 using numpy 
use the below  code to replace 10 by 200 as we done this through the index number so the number 10 is at zero index, so we call 
          

                      c[0] = 200


output would be [200 , 2, 30, 40,50]
Now, assign any number in an array to other unique numbers like the 5th element to 0
                      c[4] = 0
print c


output array ([100,   2,  30,  40,   0])

Slicing



Slicing the number in python is also useful in data science and Artificial Intelligence by use of slicing we can obtain numbers from array and assign to new numpy array

This process done through [start:end] important thing to remember is start include call number and end does not include the number that is mentioned at end position .
We can select the elements from 1 to 3 and assign it to a new numpy array f as follows:
Slicing the numpy array

                     f = c[1:4]
                    Print d
output array from c is  ([ 2, 30, 40])


replace the elements in array through slicing use [index which you want to replace : second index] = number for index one , number for second index.
Set the fourth element and fifth element to 600 and 800
Code:
                   c[3:5] = 600, 800
                   Print c
output array ([100,   2,  30, 600, 800])

We can also define the steps in slicing, like this: [start:end:step].

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5:2])

output    [2 4]


Explanation:
arr[1:5:2]: The slice [start:end:step] works as follows:
start=1: Start at index 1 (value 2 in this case).
end=5: Stop before index 5 (do not include value at index 5, which is 6).
step=2: Take every second element in this range.
Thus, the selected indices are 1 and 3:
Value at index 1: 2
• Value at index 3: 4

Some handy techniques for numpy arrays
Create a numpy array


a = np.array([0, 1, 2, 3, 4,5])


Get the size of NumPy array
Which counts the total number in arrays

a.size 6
Get the number of dimensions of NumPy array
a.ndim  1
as our array is one dimensional .
how to get the shape/size of NumPy array
a.shape (6,) as our array containing one row and six columns
NumPy and some of Statistical Functions
First, create a NumPy array


b= np.array([1, -1, 1, -1])


to check  the mean of NumPy array
mean = b.mean()
Get the standard deviation of NumPy array

standard_deviation=b.std()
standard_deviation

Create a numpy array

D = np.array([,-4 -1, 2, 3, 4, 5,7,7,8])


Find out  the biggest value in the NumPy array

max_d = d.max()
max_d
output  8 

To get the smallest value in the numpy array

min_d = d.min()
min_d
output = -4


Operations in NumPy Array
Numpy array operations means addition of arrays subtraction and multiplication of arrays You could use arithmetic operators directly between NumPy arrays
Addition of Array


x= np.array([1, 0])
x
array([1, 0])
y = np.array([0, 1])
y
array([0, 1])


Numpy Array Addition
To add the elements of two arrays

z = np.add(x, y)
z
Out
Array ([1, 1])


Array Multiplication
To go for multiplication of array first Create a numpy array

n= np.array([1, 2])
n
array([1, 2])


Create a numpy array

m = np.array([2, 1])
m
array([2, 1])
o= np.multiply(x,y)
o
array([2, 2])
k = np.divide(x,y)
k
array([0.5, 2. ])


Operation Function Examples


Addition np.add() np.add(x, y)
Subtraction np.subtract() np.subtract(x, y)
Multiplication np.multiply() np.multiply(n, m)
Division np.divide() np.divide(n, m)


output    array ([100,   2,  30,  40,   0])

So these are the some basic numpy functions that we can use while performing data manipulation and transformation tasks.

Leave a Reply

Your email address will not be published. Required fields are marked *