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

numpy_lab_1-5

Lab manual

Uploaded by

smce.ramu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

numpy_lab_1-5

Lab manual

Uploaded by

smce.ramu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

ELURU COLLEGE OF ENGINEERING & TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE - NUMPY LAB MANUAL

1.Create 1-D array called zeros having 10 elements and all the elements are
set to zero.
import numpy as np
array1=np.zeros((10,))
print(array1)
Output:
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

1a. Find the dimensions, shape, size, data type of the items and itemsize of
array zeros
import numpy as np
array1=np.zeros((10,))
print(array1)
print(array1.ndim)
print(array1.shape)
print(array1.dtype)
print(array1.itemsize)
print(array1.size)
Output:
===========
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
1
(10,)
float64
8
10
1b. Split the array zeros at array index 2, 5, 7, 8 and store the resulting
arrays in zerosA, zerosB, zerosC and zerosD and print them.
import numpy as np
X=np.zeros([10])
a,b,c,d,e=np.split(x,[2,5,7,8])
print(a)
print(b)
print(c)
print(d)
print(e)
Output:
=================
[0. 0.]

[0. 0. 0.]

[0. 0.]

[0.]

[0. 0.]

2. Create 1-D array called vowels having the elements ‘a’,‘e’, ‘i’, ‘o’ and ‘u’.
import numpy as np
array2=np.array(['a','e','i','o','u'])
print(array2)
Output:
['a' 'e' 'i' 'o' 'u']

2a. Reverse the array of vowels.


import numpy as np
array2=np.array(['a','e','i','o','u'])
print(array2[::-1])
Output:
['u' 'o' 'i' 'e' 'a']

2b. Display the 2nd and 3rd element of the array vowels.
import numpy as np
array2=np.array(['a','e','i','o','u'])
print(array2[1:3])
Output:
['e' 'i']

2c. Find the dimensions, shape, size, data type of the items and itemsize of
array vowels.
import numpy as np
array2=np.array(['a','e','i','o','u'])
print(array2)
print(array2.ndim)
print(array2.shape)
print(array2.size)
print(array2.dtype)
print(array2.itemsize)
Output:
===========
['a' 'e' 'i' 'o' 'u']
1
(5,)
5
<U1
4

3. Create 2-D array called ones having 2 rows and 5 columns and all the
elements are set to 1 and dtype as int.
import numpy as np
array3=np.ones([2,5],dtype=int)
print(array3)
output:
===========
[[1 1 1 1 1]
[1 1 1 1 1]]

3a. Reshape the array ones to have all the 10 elements in a single row.
import numpy as np
array3=np.ones([2,5],dtype=int)
print(array3)
print(array3.reshape(1,10))
Output:
[[1 1 1 1 1]
[1 1 1 1 1]]
[[1 1 1 1 1 1 1 1 1 1]]

3b. Find the dimensions, shape, size, data type of the items and itemsize of
array ones.
import numpy as np
array3=np.ones([2,5],dtype=int)
print(array3)
print(array3.ndim)
print(array3.shape)
print(array3.size)
print(array3.dtype)
Output:
[[1 1 1 1 1]
[1 1 1 1 1]]
2
(2, 5)
10
int32

3c. Divide all elements of array ones by 3


import numpy as np
array3=np.ones([2,5],dtype=int)
print(array3/3)
Output:
===========
[[0.33333333 0.33333333 0.33333333 0.33333333 0.33333333]
[0.33333333 0.33333333 0.33333333 0.33333333 0.33333333]]

3d. Find the transpose of array ones.


import numpy as np
array3=np.ones([2,5],dtype=int)
print(array3.transpose())
Output:
[[1 1]
[1 1]
[1 1]
[1 1]
[1 1]]

4. Use nested Python lists to create a 2-D array called myarray1 having 3
rows and 3 columns and store
the following data:
2.7 -2 -19
0 3.4 99.9
10.6 0 13
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print(my_array1)
Output:
===========
[[ 2.7 -2. -19. ]
[ 0. 3.4 99.9]
[ 10.6 0. 13. ]]
4a. Display the elements in the 1st column of the 2nd and 3rd row of the array
myarray1.
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print(my_array1[1:2,0:1])
print(my_array1[2:3,0:1])
Output:
[[0.]]
[[10.6]]

4b. Display the elements in the 1st and 2nd column of the array myarray1.
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print(my_array1[0:1,0:2])
print(my_array1[1:2,0:2])
print(my_array1[2:3,0:2])
Output:
===========
[[ 2.7 -2. ]]
[[0. 3.4]]
[[10.6 0. ]]

4c. Display all elements in the 2nd and 3rd row of the array myarray1.
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print(my_array1[1:3,0:3])
Output:
===========
[[ 0. 3.4 99.9]
[10.6 0. 13. ]]

4d. Find the dimensions, shape, size, data type of the items and itemsize of
array myarray1
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print(my_array1.ndim)
print(my_array1.size)
print(my_array1.dtype)
print(my_array1.shape)
print(my_array1.itemsize)
Output:`
===========
2
9
float64
(3, 3)
8

4e. Find the cube of all elements of myarray1 and divide the resulting array
by 2
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print((my_array1**3)/2)
Output:
===========
[[ 9.841500e+00 -4.000000e+00 -3.429500e+03]
[ 0.000000e+00 1.965200e+01 4.985015e+05]
[ 5.955080e+02 0.000000e+00 1.098500e+03]]

4f. Sort the myarray1 such that it brings the lowest value of the column in the
first row and soon
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print(np.sort(my_array1,axis=0))
Output:
===========
[[ 0. -2. -19. ]
[ 2.7 0. 13. ]
[ 10.6 3.4 99.9]]

5. A 2-D array called myarray2 using arange() having 3 rows and 5 columns
with start value = 4, step size 4and dtype as float.
import numpy as np
myarray2=np.arange(4,61,4)
a=myarray2.reshape(3,5)
a=a.astype(float)
print(a)
Output:
[[ 4. 8. 12. 16. 20.]
[24. 28. 32. 36. 40.]
[44. 48. 52. 56. 60.]]
5a. Find the dimensions, shape, size, data type of the items and itemsize of
array myarray2.

import numpy as np
myarray2=np.arange(4,61,4)
a=myarray2.reshape(3,5)
a=a.astype(float)
print(a.ndim)
print(a.shape)
print(a.size)
print(a.dtype)
print(a.itemsize)
Output:
2
(3, 5)
15
float64
8

5b. Find the square root of all elements of myarray2 and divide the resulting
array by 2. The result should be rounded to two places of decimals.
import numpy as np
myarray2=np.arange(4,61,4)
a=myarray2.reshape(3,5)
a=a.astype(float)
b=np.sqrt(a)
c=b/2
d=np.round(c,2)
print(d)
Output:
===========
[[1. 1.41 1.73 2. 2.24]
[2.45 2.65 2.83 3. 3.16]
[3.32 3.46 3.61 3.74 3.87]]

5c. Find the transpose of the myarray2.


import numpy as np
myarray2=np.arange(4,61,4)
a=myarray2.reshape(3,5)
a=a.astype(float)
print(a.transpose())
Output:
===========
[[ 4. 24. 44.]
[ 8. 28. 48.]
[12. 32. 52.]
[16. 36. 56.]
[20. 40. 60.]]

5d. Use NumPy. split() to split the array myarray2 into 5 arrays columnwise.
Store your resulting arrays in myarray2A, myarray2B, myarray2C,
myarray2D and myarray2E. Print the arraysmyarray2A, myarray2B,
myarray2C, myarray2D and myarray2E
import numpy as np
myarray2=np.arange(4,61,4)
x=myarray2.reshape(3,5)
x=x.astype(float)
print(x)
a,b,c,d,e=np.split(x,5,axis=1)
print(a)
print(b)
print(c)
print(d)
print(e)
Output:
===========
[[ 4. 8. 12. 16. 20.]
[24. 28. 32. 36. 40.]
[44. 48. 52. 56. 60.]]
[[ 4.]
[24.]
[44.]]
[[ 8.]
[28.]
[48.]]
[[12.]
[32.]
[52.]]
[[16.]
[36.]
[56.]]
[[20.]
[40.]
[60.]]

5e. Concatenate the arrays myarray2A, myarray2B and myarray2C into an


array having 3 rows and 3 columns.
import numpy as np
myarray2=np.arange(4,61,4)
x=myarray2.reshape(3,5)
x=x.astype(float)
print(x)
a,b,c,d,e=np.split(x,5,axis=1)
print(a+b+c)
Output:
===========
[[ 4. 8. 12. 16. 20.]
[24. 28. 32. 36. 40.]
[44. 48. 52. 56. 60.]]
[[ 24.]
[ 84.]
[144.]]

You might also like