Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
113 views

Practical6 Python Programming

The document discusses NumPy, a Python package for working with multidimensional arrays and matrices. It contains 3 parts: Part A provides an overview of NumPy and introduces key concepts like ndarray, dtype, shape, size, and methods for creating, manipulating, and accessing NumPy arrays. Common NumPy operations are demonstrated like universal functions and changing array shapes. Part B contains 3 programming problems for students to solve involving creating NumPy arrays with certain properties and manipulating 2D arrays. Part C is for students to paste their code, input, and output for the 3 problems in Part B and submit their work. The overall document serves as a reference for students learning NumPy through examples and practice problems. It covers important topics

Uploaded by

Aayush Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

Practical6 Python Programming

The document discusses NumPy, a Python package for working with multidimensional arrays and matrices. It contains 3 parts: Part A provides an overview of NumPy and introduces key concepts like ndarray, dtype, shape, size, and methods for creating, manipulating, and accessing NumPy arrays. Common NumPy operations are demonstrated like universal functions and changing array shapes. Part B contains 3 programming problems for students to solve involving creating NumPy arrays with certain properties and manipulating 2D arrays. Part C is for students to paste their code, input, and output for the 3 problems in Part B and submit their work. The overall document serves as a reference for students learning NumPy through examples and practice problems. It covers important topics

Uploaded by

Aayush Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering


Course: Python Programming 
PROGRAMME: B.Tech/MBATech. 
First Year AY 2022-2023 Semester: II

PRACTICAL 6
Part A (To be referred by students)

Built-in Package: Numpy (1-d, 2-d array)


SAVE THE FILE AND UPLOAD AS (RollNo_Name_Exp6)

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 1000 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.

Topic covered:.Numpy Package

Learning Objective: Learner would be able to


1. Learn how to import packages in python
2. Features and available functions of Numpy
3. Create multi-dimensional data structures and manipulate them

Theory:
● NumPy is 'Numerical Python package consisting of multidimensional array (ndarray)
objects and a collection of routines for processing of array.

#Numpy with one dimension # more than one dimensions


import numpy as np

1 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming 
PROGRAMME: B.Tech/MBATech. 
First Year AY 2022-2023 Semester: II

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


a = np.array([1,2,3]) print(a)
print(a) O/P - [[1, 2]
O/P - [1, 2, 3] [3, 4]]

NumPy’s array class is called ndarray. The important attributes of an ndarray object are:
ndarray.ndim - the number of axes (dimensions) of the array.
ndarray.shape - the dimensions of the array. This is a tuple of integers indicating the
size of the array in each dimension. For a matrix with n rows and m columns, shape will
be (n,m). The length of the shape tuple is therefore the number of axes, ndim.
ndarray.size -the total number of elements of the array. This is equal to the product of
the elements of shape.
ndarray.dtype -an object describing the type of the elements in the array. One can create
or specify dtype’s using standard Python types. Additionally, NumPy provides types of
its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.
ndarray.itemsize - the size in bytes of each element of the array. For example, an array
of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has
itemsize 4 (=32/8). It is equivalent to ndarray.dtype. itemsize.
ndarray.data - the buffer containing the actual elements of the array. Normally, we
won’t need to use this attribute because we will access the elements in an array using
indexing facilities.
Example –
import numpy as np print( a.itemsize)
a = np.arange(15).reshape(3, 5) 8
print(a) print( a.size)
array([[ 0, 1, 2, 3, 4], 15
[ 5, 6, 7, 8, 9], print( type(a))
[10, 11, 12, 13, 14]]) <class 'numpy.ndarray'>
print( a.shape) print( b = np.array([6, 7, 8])
(3, 5) print( b)
print(a.ndim) array([6, 7, 8])
2 print( type(b))
print( a.dtype.name) <class 'numpy.ndarray'>
'int64'

Array Creation There are several ways to create arrays. For example, you can create an
array from a regular Python list or tuple using the array function

import numpy as np print( a = np.array(1, 2, b = np.array([(1.5, 2, 3), (4, 5, 6)])


a = np.array([2, 3, 4]) 3, 4) # WRONG print( b)
print(a) Traceback (most recent array([[1.5, 2. , 3. ],
array([2, 3, 4]) call last): [4, 5 , 6 ]])
print( a.dtype) ... The type of the array can also be
dtype('int64') TypeError: array() takes explicitly specified at creation
print( b = from 1 to 2 positional time:

2 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming 
PROGRAMME: B.Tech/MBATech. 
First Year AY 2022-2023 Semester: II

np.array([1.2, 3.5, arguments but 4 were print( c = np.array([[1, 2], [3, 4]],
5.1]) given dtype=complex)
print( b.dtype) print( a = np.array([1, 2, print( c)
dtype('float64') 3, 4]) # RIGHT array([[1.+0.j, 2.+0.j],
[3.+0.j, 4.+0.j]])

To create sequences of numbers, NumPy provides the arange function which is analogous
to the Python built-in range, but returns an array.
print( np.arange(10, 30, 5))
array([10, 15, 20, 25])
print(np.arange(0, 2, 0.3)) # it accepts float arguments
array([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])

Universal Functions
NumPy provides familiar mathematical functions such as sin, cos, and exp

B = np.arange(3) print( np.sqrt(B))


print( B) array([0. , 1. , 1.41421356])
array([0, 1, 2]) print( C = np.array([2., -1., 4.])
print( np.exp(B)) print( np.add(B, C))
array([1. , 2.71828183, 7.3890561 ]) array([2., 0., 6.])

Changing the shape of an array

a = np.floor(10 * rg.random((3, 4)))


print( a)
array([[3., 7., 3., 4.],
[1., 4., 2., 2.],
[7., 2., 4., 9.]])
print( a.shape)
(3, 4)

The shape of an array can be changed with various commands. Note that the following
three commands all return a modified array, but do not change the original array:

print( a.ravel()) # returns the array, flattened


array([3., 7., 3., 4., 1., 4., 2., 2., 7., 2., 4., 9.])
print( a.reshape(6, 2)) # returns the array with a modified shape
array([[3., 7.],
[3., 4.],
[1., 4.],
[2., 2.],
[7., 2.],
[4., 9.]])
print( a.T) # returns the array, transposed

3 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming 
PROGRAMME: B.Tech/MBATech. 
First Year AY 2022-2023 Semester: II

array([[3., 1., 7.],


[7., 4., 2.],
[3., 2., 4.],
[4., 2., 9.]])
print( a.T.shape)
(4, 3)
print( a.shape)
(3, 4)

4 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming 
PROGRAMME: B.Tech/MBATech. 
First Year AY 2022-2023 Semester: II

PRACTICAL 6
Part B (to be completed by students)

Built-in Package: Numpy (1-d, 2-d array)


(Students must submit the soft copy as per the following segments. A soft copy containing
Part A and Part B answered must be uploaded on the platform specified by the Practical
Teacher. The filename should be RollNo_Name_Exp6)

Roll No.: A113 Name: Aayush Singh


Prog/Yr/Sem: 2 Batch: 2
Date of Experiment: Date of Submission:

1. Program Code along with Sample Output: (Paste your programs [1,2,3], input and
output screen shot for programs [1,2,3])
1. a) Program-

import numpy as np
arr = np.ones((3,3))
print("Numpy array with all ones:")
print(arr)

Output-

b) Program-

import numpy as np
arr = np.random.randint(5, 11, size=(3, 5))
print(arr)

Output-

5 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming 
PROGRAMME: B.Tech/MBATech. 
First Year AY 2022-2023 Semester: II

c) Program-

import numpy as np
matrix = np.identity(4)
print(matrix)
mean_value = np.mean(matrix)
print("Mean value of the matrix is: ", mean_value)

Output-

d) Program-

matrix = [[1, 2, 3],


[4, 5, 6],
[7, 8, 9]]
matrix.insert(0, [0, 0, 0])
matrix.append([0, 0, 0])
for row in matrix:
row.insert(0, 0)
row.append(0)
for row in matrix:
print(row)

Output-

6 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming 
PROGRAMME: B.Tech/MBATech. 
First Year AY 2022-2023 Semester: II

2. Program-

import numpy as np
arr = np.zeros((5, 6), dtype=int)
start = 10
for i in range(5):
for j in range(6):
arr[i][j] = start
start += 12
while start % 2 != 0:
start += 12
print(arr)
even_rows_odd_cols = arr[::2, 1::2]
print("\nEven rows and odd columns:")
print(even_rows_odd_cols)

Output-

3. Program-

import numpy as np
x = np.ones((3,3))
print("Checkerboard pattern:")
x = np.zeros((8,8),dtype=int)
x[1::2,::2] = 1
x[::2,1::2] = 1
print(x)

7 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming 
PROGRAMME: B.Tech/MBATech. 
First Year AY 2022-2023 Semester: II

Output-

2. Conclusion (Learning Outcomes): Reflect on the questions answered by you jot down
your learnings about the Topic.

8 | Page

You might also like