I037 - Manas Patel Experiment08

You might also like

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

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

PRACTICAL 8

Problem Statement: Write Python program to


1. a. Create a numpy array with all ones
b. Generate random integers from 5 to 10 and arrange them into 3 * 5 numpy array.
c. Create 4 * 4 identity matrix using numpy function and find its mean value.
d. Create 3 * 3 matrix and pad (fill) the borders with 0’s.
2. Create a 5 * 6 array between 10 to 370 such that the difference between the elements are
12 and they are only even numbers. Print the even rows and odd columns from the array.
3. Take user input for dimension of matrix and create the following pattern. Below is for 8 *
8 pattern.

4. Write a NumPy program to convert the values of Centigrade degrees into Fahrenheit
degrees and vice versa. Values are stored into a NumPy array.

PRACTICAL 8

Roll No.: I037 Name: Manas Hiteshbhai Patel


Prog/Yr/Sem: MBA Tech. IT 2nd Yr Sem-III Batch: A2
Date of Experiment: 17-09-2022 Date of Submission:

1. Questions based on Experiment Scenario:


a. Describe how numpy array is different from arrays.
Ans. NumPy arrays have a fixed size at creation. Changing the size of ndarray will create a
new array and delete the original. Elements in a NumPy array are all of same data type and of
same size in memory. NumPy modules generally used for matrix and array computation while
array module is used to create array.

b. Describe with example problems how axis 0 and axis 1 are used in numpy.
Ans. In numpy axis 0 refers to horizontal axis or rows and axis 1 refers to vertical axis or
columns. While performing action, if axis is 0 then action is performed on rows that satisfy the
condition. When axis is 1 then action will be performed on columns that satisfy the conditions.

1 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

For example
Let data frame (df) be
team points assists Rebound
0 A 25 5 11
1 A 12 7 8
2 B 15 8 10
3 B 14 5 9

For Axis=0
Code:
df.mean(axis=0)

Output:
0 13.666
1 9.0000
2 11.000
3 9.3333
dtype: float64

For Axis=1
Code:
df.mean(axis=1)

Output:
Points 16.5
Assists 6.25
Rebound 9.50
dtype: float64

2 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III
c. Multiple Choice

1. What will be output of the code?


import numpy as np
a=np.array([1,2,3], dtype=complex)
print(a)
d. [1.+0.j, 2.+0.j, 3.+0.j]
a. [[1.+0.j,2.+0.j,3.+0.j]]
b. [1.+0.j]
c. Error
d. [1.+0.j,2.+0.j,3.+0.j]
2. If a dimension is given as ------ in a reshaping
operation, the other dimension are automatically
calculated.
A: Zero c. Negative one
B. One
C. Negative one
D. Infinite
3. What will be output for the following code?
import numpy as np
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print a['age'] b. [10 20 30]
A. [[10 20 30]]
B. [10 20 30]
C. [10]
D. Error
4. eye() function in numpy returns
c. A Diagonal Matrix

3 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

a. An identity matrix
b. A symmetric matrix with only 1s and 0s
c. A diagonal matrix
d. A null matrix

5. import numpy as np
a= np.array([7,10,2,4,13,16])
test=np.percentile(a,50)
print(test)
c. 8.5
a. 11.5
b. 10
c. 8.5
d. 7

2. Program Code along with Sample Output:


Q.1
CODE:
#CREATE A NUMPY ARRAY WITH ALL 1's
import numpy as np

#1-D
arr1=np.zeros(5)
print('Array 1:',arr1)

#2-D
arr2=np.ones((4,3),dtype=int)
print('Array 2:',arr2)

4 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

print('<-------------------------------------------------->')

#generate random integer from 5 to 10 and arrange in 3*5 array


arr3=np.random.randint(5,11,size=(3,5))
print('Array 3:',arr3)
print('Shape: ',arr3.shape)
print('Reshape: ',arr3.reshape(5,3))

print('<-------------------------------------------------->')

#Create 4X4 identity matrix and find mean value


arr4=np.identity(4)
print('Array 4:',arr4)
print('\nMean:',np.mean(arr4))

print('<-------------------------------------------------->')

#Create 3x3 matrix with pad(fill) with border with 0's


arr5=np.ones((3,3))
print('Array 5:\n',arr5)
print('Shape: ',arr5.shape)
arr5=np.pad(arr5,pad_width=1, mode='constant',constant_values=0)
print('Array 5:\n',arr5)
print('Shape: ',arr5.shape)

OUTPUT:

5 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

Q.2
CODE:
#create 5x6 array between 10 and 370 with difference between element are 12 and even
number in even row and odd column
import numpy as np

arr=np.arange(10,370,12)
arr=arr.reshape(5,6)
print('Array:\n',arr)

6 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

#Print even row and odd column


print('Printing Even row and odd column:\n',arr[0::2,1::2])

#Print top left corner


print('Printing Top left Corner:\n',arr[0:3:1,0:3:1])

#Print bottom right corner


print('Printing Top left Corner:\n',arr[-3::,-3::])

OUTPUT:

Q.3
CODE:
#Create 8 by 8 with given pattern
import numpy as np

x=np.zeros((8,8),dtype=int)

7 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

print('All Zeros:\n',x)

x[::2,1::2]=1
print('Pattern Even Row and Odd Coumns:\n',x)

x[1::2,::2]=2
print('Pattern Odd Row and Even Coumns:\n',x)

OUTPUT:

Q.4
CODE:
#convert celcius to farenheit in numpy
import numpy as np

values=[25,34,35.5,23.4]

8 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

c=np.array(values)
print('Celcius:',c)

f=np.round((9*c/5+32),2)
print('farenheit:',f)

OUTPUT:

3. Conclusion (Learning Outcomes): The learning gains from the practical was to learn
about library NumPy which is used to create matrices and array and manipulating it to
make patterns or to make table like structure using arrays data.

For Installing numpy package:

• Windows + R
• Cmd
• Cd c:\Users\mpstme.student\AppData\Local\Programs\Python\Python310
• Python.exe -m pip install --upgrade pip
• Cd scripts
• Pip install numpy
• Pip install pandas
• Pip install matplotlib

9 | Page

You might also like