Numy Session 1 Code

Numy Session 1 Code For Video Click Fahad Hussain CS


import numpy as np

arr1 = np.array([[1,2,3],

                 [4,5,6]])

print(type(arr1))

print(arr1.ndim)

print(arr1.shape)

print(arr1.size)

print(arr1.dtype)


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

                 [4,5,6]], dtype = 'float')


print(a.dtype)


b = np.array(((1,2,3),

                 (4,5,6)))

print(b)


c = np.zeros((3,4))

print(c)


d = np.full((3,3), 6, dtype='complex')

print(d)


e = np.random.random((2,2))

print(e)


f = np.arange(0,30,5)

print(f)


g = np.linspace(0,5,10)

print(g)


arr = np.array([[2,3,12,7],

                [4,5,6,54],

                [6,5,34,3],])


newarr = arr.reshape(2,2,3)


print(arr)

print(newarr)


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

flarr = arr.flatten()


print(arr)

print(flarr)


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

                [4,5,6,7],

                [8,7,6,5]])


temp = arr[:2,:2]

print(arr)

print(temp)


arr


print(arr+1)

print(arr-2)

print(arr*2)

print(a**2)


z = np.array([[2,3,12],

                [4,5,6],

                [6,5,34]])

print(z)

print(z.T)


print(z.max()


print(z)

print(z.max(axis=1))


print(z)

print(z.max(axis=0))


print(z.sum())


print(z.cumsum(axis=1))


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

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


print(a+b)

print(a*b)

print(a**b)

print(a.dot(b))


print(np.sin(a))

print(np.cos(a))

print(np.exp(a))

print(np.sqrt(a))


# Python program to demonstrate sorting in numpy 

import numpy as np 


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

[3, 4, 6], 

[0, -1, 5]]) 


# sorted array 

print ("Array elements in sorted order:\n", 

np.sort(a, axis = None)) 


print(a)

# sort array row-wise 

print ("Row-wise sorted array:\n", 

np.sort(a, axis = 1)) 


print(a)

# specify sort algorithm 

print ("Column wise sort by applying merge-sort:\n", 

np.sort(a, axis = 0, kind = 'mergesort')) 




# Example to show sorting of structured array 

# set alias names for dtypes 

dtypes = [('name', 'S10'), ('grad_year', int), ('cgpa', float)] 


# Values to be put in array 

values = [('Hrithik', 2009, 8.5), ('Ajay', 2008, 8.7), 

('Pankaj', 2008, 7.9), ('Aakash', 2009, 9.0)] 

# Creating array 

arr = np.array(values, dtype = dtypes) 

print ("\nArray sorted by names:\n", 

np.sort(arr, order = 'name')) 

print ("Array sorted by grauation year and then cgpa:\n", 

np.sort(arr, order = ['grad_year', 'cgpa'])) 


No comments:

Post a Comment

Fell free to write your query in comment. Your Comments will be fully encouraged.