Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

NUMPY: To create an array of zeros with shape (3, 3),

you can use np.zeros((3, 3)


In [45]: import numpy as np

# create a 3x3 array of zeros


a = np.zeros((3, 3))
print(a)

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

To create an array of ones with shape (3, 3), you can


use np.ones((3, 3))
In [46]: import numpy as np

# create a 3x3 array of ones


b = np.ones((3, 3))
print(b)

[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]

To create an array from a python list, you can use


np.array()
In [47]: import numpy as np

# create an array from a list


c = np.array([1, 2, 3])
print(c)

[1 2 3]

To manipulate an array, you can use indexing, slicing,


reshaping, and concatenation.
In [48]: import numpy as np

# create an array
a = np.array([1, 2, 3, 4, 5])

# indexing
print(a[2]) # prints 3

# slicing
print(a[1:3]) # prints [2, 3]
# reshaping
#print(a.reshape((2, 2))) # prints [[1, 2], [3, 4], [5, 6]]

# concatenation
b = np.array([6, 7, 8])
c = np.concatenate((a, b))
print(c) # prints [1, 2, 3, 4, 5, 6, 7, 8]

3
[2 3]
[1 2 3 4 5 6 7 8]

To perform mathematical operations on arrays, you


can use numpy's built-in functions. Here are some
examples:
Addition:

In [ ]: import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a + b
print(c) # prints [5, 7, 9]

Subtraction:
In [ ]: import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a - b
print(c) # prints [-3, -3, -3]

Multiplication:
In [ ]: import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a * b
print(c) # prints [4, 10, 18]

Division:
In [ ]: import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a / b
print(c) # prints [0.25, 0.4, 0.5]
Matrix Multiplication:
In [49]: import numpy as np

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


b = np.array([[5, 6], [7, 8]])
c = np.matmul(a, b)
print(c) # prints [[19, 22], [43, 50]]

[[19 22]
[43 50]]

Matrix Inversion:
In [ ]: import numpy as np

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


b = np.linalg.inv(a)
print(b)

To perform statistical analysis on arrays, you can use


numpy's built-in functions. Here are some examples:
Mean:

In [ ]: import numpy as np

a = np.array([1, 2, 3, 4, 5])
mean = np.mean(a)
print(mean) # prints 3.0

Median:
In [ ]: import numpy as np

a = np.array([1, 2, 3, 4, 5])
median = np.median(a)
print(median) # prints 3.0

Standard deviation:
In [ ]: import numpy as np

a = np.array([1, 2, 3, 4, 5])
std = np.std(a)
print(std) # prints 1.5811388300841898

Correlation:
In [ ]: import numpy as np

a = np.array([1, 2, 3, 4, 5])
b = np.array([5, 4, 3, 2, 1])
correlation = np.corrcoef(a, b)
print(correlation) # prints [[ 1., -1.], [-1., 1.]]

You can also use other functions such as np.min(),


np.max(), np.sum() and many more to perform
statistical analysis on arrays.
Also, Numpy provides inbuilt functions such as np.percentile(array, percentile) to calculate the
nth percentile of array.

In [ ]: import numpy as np

a = np.array([1, 2, 3, 4, 5])
percentile = np.percentile(a, 50)
print(percentile) # prints 3.0

Please note that correlation is a value between -1 and


1 indicating the strength and direction of the
correlation between two variables. A correlation of 1
means that the variables are perfectly positively
correlated, a correlation of -1 means that the variables
are perfectly negatively correlated, and a correlation
of 0 means that the variables are not correlated.

Here is an example of using Numpy's random number


generator to generate an array of 10 random numbers
from a uniform distribution between 0 and 1:
In [50]: import numpy as np

# Generate an array of 10 random numbers from a uniform distribution


random_numbers = np.random.rand(10)
print(random_numbers)

[0.81680708 0.22969802 0.72036386 0.80883776 0.87831027 0.31031967


0.28449062 0.74057815 0.69762242 0.94587832]

here is an example of generating an array of 10


random numbers from a normal distribution with a
mean of 0 and a standard deviation of 1:
In [51]: import numpy as np

# Generate an array of 10 random numbers from a normal distribution with mean 0 and st
random_numbers = np.random.randn(10)
print(random_numbers)

[ 1.55163432 0.18293727 0.25967379 0.77985727 -1.22326414 1.06705028


-1.43402815 0.14809381 2.25785143 0.22913414]

In [ ]:

You might also like