0% found this document useful (0 votes)
23 views

CS GE Assignment

This notebook contains examples of using NumPy to perform operations on arrays such as creating, manipulating, applying math functions and random number generation. NumPy arrays are created from lists and various functions like random, zeros, ones are used to generate sample arrays. Indexing, slicing and mathematical operations like addition, subtraction etc are demonstrated on these arrays.

Uploaded by

JALSHA LODHI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

CS GE Assignment

This notebook contains examples of using NumPy to perform operations on arrays such as creating, manipulating, applying math functions and random number generation. NumPy arrays are created from lists and various functions like random, zeros, ones are used to generate sample arrays. Indexing, slicing and mathematical operations like addition, subtraction etc are demonstrated on these arrays.

Uploaded by

JALSHA LODHI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

csgeassignment - Jupyter Notebook http://localhost:8888/notebooks/csgeassignment.

ipynb#

In [130]: a = [1,2,3]
[q*2 for q in a]

Out[130]: [2, 4, 6]

In [131]: import numpy as np

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

Out[131]: array([2, 4, 6])

In [132]: a = [1,2,3]
b = [4,5,6]
[q+r for q , r in zip(a,b)]

Out[132]: [5, 7, 9]

In [133]: a = np.array([1,2,3])
b = np.array([4,5,6])
a+b

Out[133]: array([5, 7, 9])

In [134]: b = np.zeros(3 , int)


print(b.dtype)
print(b , b.shape)

int64
[0 0 0] (3,)

In [135]: c = np.zeros_like(a)
print(c)
print(c.dtype, c.shape)

[0 0 0]
int64 (3,)

1 of 7 2/8/24, 15:08
csgeassignment - Jupyter Notebook http://localhost:8888/notebooks/csgeassignment.ipynb#

In [136]: d = np.ones(3)
print(d)
e = np.ones_like(a)
print(e)
f = np.empty(3)
print(f)
g = np.empty_like(a)
print(g)
h =np.full(3,7.)
i =np.full_like(a,7)
print(d ,e , f , g, h , i )

[1. 1. 1.]
[1 1 1]
[1. 1. 1.]
[4607182418800017408 4607182418800017408 4607182418800017408]
[1. 1. 1.] [1 1 1] [1. 1. 1.] [4607182418800017408 4607182418800017
408 4607182418800017408] [7. 7. 7.] [7 7 7]

In [137]: j = np.arange(6)
k = np.arange(2,6)
l= np.arange(1,6,2)
m= np.arange(0,0.5,6)
print(j,k,l,m)

[0 1 2 3 4 5] [2 3 4 5] [1 3 5] [0.]

In [138]: #norm
n = np.arange(0.4,0.8,0.1)
#anomaly
o= np.arange(0.5,0.8,1)
#solution1
p= np.arange(0.5,0.75,3)
#solution2
q= np.linspace(0.5,0.7,3)
#gotcha
r= np.linspace(0,1,)
print(n,o,p,q,r)

[0.4 0.5 0.6 0.7] [0.5] [0.5] [0.5 0.6 0.7] [0. 0.02040816
0.04081633 0.06122449 0.08163265 0.10204082
0.12244898 0.14285714 0.16326531 0.18367347 0.20408163 0.2244898
0.24489796 0.26530612 0.28571429 0.30612245 0.32653061 0.34693878
0.36734694 0.3877551 0.40816327 0.42857143 0.44897959 0.46938776
0.48979592 0.51020408 0.53061224 0.55102041 0.57142857 0.59183673
0.6122449 0.63265306 0.65306122 0.67346939 0.69387755 0.71428571
0.73469388 0.75510204 0.7755102 0.79591837 0.81632653 0.83673469
0.85714286 0.87755102 0.89795918 0.91836735 0.93877551 0.95918367
0.97959184 1. ]

In [139]: s = np.random.randint(0,10,3)
t= np.random.rand(3)
u=np.random.uniform(1,10,3)
print(s,t,u)

[7 7 2] [0.06063817 0.22751096 0.91637852] [8.04241271 1.63928137


6.36868053]

2 of 7 2/8/24, 15:08
csgeassignment - Jupyter Notebook http://localhost:8888/notebooks/csgeassignment.ipynb#

In [140]: rng = np.random.default_rng()


a = rng.integers(0,10,3)
print(a)
b = rng.random(3)
c=rng.uniform(1,10,3)

d= rng.integers(0,10,3, endpoint = True)


e = rng.standard_normal(3)
f = rng.normal(5,2,3)
print(a,b,c,d,e,f)

[6 3 8]
[6 3 8] [0.49837745 0.20520989 0.77587682] [4.03911938 5.72251653
7.20986941] [4 3 6] [-0.18021185 1.06447602 -1.01089949] [3.659628
43 8.23659441 4.19628659]

In [141]: a = np.arange(1,6)
print(a[1],a[2:4],a[-2:], a[::2],a[[1,3,4]])

2 [3 4] [4 5] [1 3 5] [2 4 5]

In [142]: #copying of arrays


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

b=a
c=a[:]
d=a.copy()
print(a,b,c,d)

[1 2 3] [1 2 3] [1 2 3] [1 2 3]

In [143]: a = [1,2,3]
a[1:2]=[5,6]
print(type(a))

<class 'list'>

In [144]: #maths operations


a = np.array([2,3])
print(a**2 , np.sqrt(a) , np.exp(a) , np.log(a))

[4 9] [1.41421356 1.73205081] [ 7.3890561 20.08553692] [0.69314718


1.09861229]

In [145]: print(np.dot(a,a), np.cross(a,a))

13 0

In [146]: a = np.sin([np.pi , np.pi/2])


print(a)

[1.2246468e-16 1.0000000e+00]

3 of 7 2/8/24, 15:08
csgeassignment - Jupyter Notebook http://localhost:8888/notebooks/csgeassignment.ipynb#

In [147]: a = np.arcsin([0. , 1.])


print(a)

[0. 1.57079633]

In [148]: print(np.floor([1.1,1.5,1.9,2.5]))
print(np.ceil([1.1,1.5,1.9,2.5]))
print(np.round([1.1,1.5,1.9,2.5]))

[1. 1. 1. 2.]
[2. 2. 2. 3.]
[1. 2. 2. 2.]

In [149]: a = np.array([1,2,3,4])
print(np.max(a), np.min(a) , np.argmax(a) , np.argmin(a), np.sum(a) ,

4 1 3 0 10 1.25 2.5 1.118033988749895

In [150]: a = np.array([234,32432,123213,213213,23214,566656,56565])
print(a.sort(), np.sort(a))

None [ 234 23214 32432 56565 123213 213213 566656]

In [151]: #2 D array
a = np.array([[1,2,3],[4,5,6]])
print(a.dtype,a.shape, np.zeros((3,2)), np.full((3,2),7), np.empty((3

int64 (2, 3) [[0. 0.]


[0. 0.]
[0. 0.]] [[7 7]
[7 7]
[7 7]] [[4.4e-323 4.0e-323]
[0.0e+000 3.0e-323]
[2.0e-323 1.5e-323]] [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

In [152]: a = np.random.randint(0,10,(3,2))
b = np.random.rand(3,2)
c = np.random.uniform(1,10,(3,2))
d = np.random.randn(3,2)
e = np.random.normal(10,2,(3,2))
print(a,b,c,d,e)

[[5 8]
[4 4]
[0 6]] [[0.37927519 0.24821296]
[0.2046899 0.17662401]
[0.87675123 0.49074327]] [[5.41561655 8.32045231]
[2.60797837 2.05486454]
[8.31669213 5.15317897]] [[ 1.45726316 0.44056271]
[ 1.34137991 -1.14901579]
[-0.36731541 1.60874201]] [[10.58180251 7.93044078]
[13.34371556 12.58771318]
[ 6.856148 13.51709236]]

4 of 7 2/8/24, 15:08
csgeassignment - Jupyter Notebook http://localhost:8888/notebooks/csgeassignment.ipynb#

In [153]: rng = np.random.default_rng()


print(rng.integers(0,10,3),rng.random(3), rng.uniform(1,10,3),

[3 1 5] [0.6409918 0.52767767 0.15276207] [4.31114143 3.56533897


6.0379432 ] [ 0.14837007 -0.26138477 1.7739326 ] [4.90144013 4.466
52672 4.3474097 ]

In [154]: a = np.array([[1,2],[3,4]])
b = np.array([[2,3],[4,5]])
print(a+b)
print(a-b)
#broadcasting
print(a+3,
a*3,
a/2,
a-1,
a@a)
#TRANSPOSE
print(a.T)
#reshaping
z = np.array([1,2,3,4,5,6,7,8,9]).reshape(3,3)
print(z)

[[3 5]
[7 9]]
[[-1 -1]
[-1 -1]]
[[4 5]
[6 7]] [[ 3 6]
[ 9 12]] [[0.5 1. ]
[1.5 2. ]] [[0 1]
[2 3]] [[ 7 10]
[15 22]]
[[1 3]
[2 4]]
[[1 2 3]
[4 5 6]
[7 8 9]]

In [155]: #insert
h = np.array([[1,3,5],[6,8,10],[11,13,15]])
print(np.insert(h,[1,2],0,axis=1))
#delete
print(np.delete(h,[1,2],axis = 1))

[[ 1 0 3 0 5]
[ 6 0 8 0 10]
[11 0 13 0 15]]
[[ 1]
[ 6]
[11]]

5 of 7 2/8/24, 15:08
csgeassignment - Jupyter Notebook http://localhost:8888/notebooks/csgeassignment.ipynb#

In [156]: #any
a=np.array([[1,2,3],[4,5,6]])
b =np.any(a>5)
c =np.any(a>5,axis=0)
d = np.any(a>5,axis = 1)
print(b,c,d)

True [False False True] [False True]

In [157]: #matrix sorting


z=np.array([[4,8,5],[9,3,1]])
a =z.sort(axis= 0)
b = z.sort(axis= 1)
print(a,b)

None None

In [158]: #3d and above


a = np.arange(1,9).reshape(2,2,2)
print(a)

[[[1 2]
[3 4]]

[[5 6]
[7 8]]]

In [159]: n= np.array([[[0,0,0,0],[1,1,1,1]],[[2,2,2,2],[3,3,3,3]],[[4,4,4,4],[
print(n)
print(n.reshape(3,2,4))
print(n.ndim , n.size)

[[[0 0 0 0]
[1 1 1 1]]

[[2 2 2 2]
[3 3 3 3]]

[[4 4 4 4]
[5 5 5 5]]]
[[[0 0 0 0]
[1 1 1 1]]

[[2 2 2 2]
[3 3 3 3]]

[[4 4 4 4]
[5 5 5 5]]]
3 24

6 of 7 2/8/24, 15:08
csgeassignment - Jupyter Notebook http://localhost:8888/notebooks/csgeassignment.ipynb#

In [160]: #concatenate
a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(a)
c = np.array([[1,2,3,7],[4,5,6,8]])
print(np.concatenate((a,c),axis = 0))
#swapaxes
print(np.swapaxes(a,0,1), np.swapaxes(a,0,1))

[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[ 1 2 3 7]
[ 4 5 6 8]]
[[ 1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]] [[ 1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]]

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

7 of 7 2/8/24, 15:08

You might also like