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

3 SC

This document contains the outputs and source code for 10 programs written using NumPy. The programs create vectors and matrices with various properties like null elements, random values, sorted values etc. and perform operations like reversing, concatenating, extracting blocks etc. on them. The last program verifies that a given 5x5 matrix is a magic square by checking that the row sums, column sums and diagonal sums are equal.

Uploaded by

SABYASACHI SAHOO
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)
20 views

3 SC

This document contains the outputs and source code for 10 programs written using NumPy. The programs create vectors and matrices with various properties like null elements, random values, sorted values etc. and perform operations like reversing, concatenating, extracting blocks etc. on them. The last program verifies that a given 5x5 matrix is a magic square by checking that the row sums, column sums and diagonal sums are equal.

Uploaded by

SABYASACHI SAHOO
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/ 12

T&T Lab.

(CS-3096), Spring 2023

Tool & Technique Laboratory (T&T Lab.)


[CS-3096]
Individual Work
Lab. No: 6 , Date:03/03/2023 , Day:Friday
Topic: Numpy
Roll Number: 2006189 Branch/Section: IT-03
Name in Capital: SABYASACHI SAHOO

Program No: 6.1

Program Title:Write a program to create a null vector of size 25 but the values from fifth
to tenth elements are all 1.

Input/Output Screenshots:
RUN-1:

Source code
import numpy as np

vector = np.zeros(25)

vector[4:10] = 1

print(vector)

Page
T&T Lab.(CS-3096), Spring 2023

Program No: 6.2

Program Title:Write a program to create a vector with values ranging from 5 to 20 &
reverse it

Input/Output Screenshots:
RUN-1:

Source code

import numpy as np

vector = np.arange(5, 21)

vector_reversed = vector[::-1]

print("Original vector: ", vector)

print("Reversed vector: ", vector_reversed)

Program No:6.3
Page
T&T Lab.(CS-3096), Spring 2023

Program Title:Write a program to create a vector with 10 random values & sort first half
ascending & second half descending.

Input/Output Screenshots:
RUN-1:

Source code
import numpy as np

vector = np.random.rand(10)

vector[:len(vector)//2] = np.sort(vector[:len(vector)//2])

vector[len(vector)//2:] = np.sort(vector[len(vector)//2:])[::-1]

print("Original vector: ", vector)

Program No:6.4

Page
T&T Lab.(CS-3096), Spring 2023

Program Title:Write a program to create a vector of size 10 with values ranging from 0
to 1, both excluded.

Input/Output Screenshots:
RUN-1:

Source code
import numpy as np

vector = np.linspace(0, 1, 12)[1:-1]

print(vector)

Program No:6.5

Program Title:Write a program to concatenate element-wise two arrays of string

Input/Output Screenshots:
RUN-1:

Page
T&T Lab.(CS-3096), Spring 2023

Source code
import numpy as np

array1 = np.array(['hello', 'world', 'numpy'])

array2 = np.array(['!', '?', 'rocks'])

concatenated = np.char.add(array1, array2)

print(concatenated)

Program No: 6.6

Program Title:Write a program to create a 4x3 array with random values & find out the
minimum & maximum values.

Input/Output Screenshots:
RUN-1:

Page
T&T Lab.(CS-3096), Spring 2023

Source code
import numpy as np

array = np.random.rand(4, 3)

min_value = np.min(array)

max_value = np.max(array)

print("Array:\n", array)

print("Minimum value:", min_value)

print("Maximum value:", max_value)

Program No:6.7

Program Title:Write a program to create a 2D array with 1 on the border and 0 inside.

Input/Output Screenshots:
RUN-1:

Page
T&T Lab.(CS-3096), Spring 2023

Source code
import numpy as np

array = np.zeros((5, 5))

array[0,:] = 1

array[-1,:] = 1

array[:,0] = 1

array[:,-1] = 1

print(array)

Program No: 6.8

Program Title:Write a program to create a 4x4 matrix with values 1,2,3,4 just below &
above the diagonal, rest zeros.

Page
T&T Lab.(CS-3096), Spring 2023

Input/Output Screenshots:
RUN-1:

Source code
import numpy as np

matrix = np.zeros((4, 4))

np.fill_diagonal(matrix[1:], [1, 2, 3])

np.fill_diagonal(matrix[:, 1:], [4, 1, 2])

print(matrix)

Program No:6.9

Program Title:Write a program to extract all the contiguous 3x3 blocks from a random
10x10 matrix

Input/Output Screenshots:
RUN-1:

Page
T&T Lab.(CS-3096), Spring 2023

Source code

import numpy as np

matrix = np.random.randint(0, 10, (10, 10))

blocks = np.lib.stride_tricks.sliding_window_view(matrix, (3, 3))

print("Shape of blocks array: ", blocks.shape)

for i in range(blocks.shape[0]):

    for j in range(blocks.shape[1]):

        print(blocks[i][j])

Program No:6.10

Program Title:A magic square is a matrix all of whose row sums, column sums and the
sums of the two diagonals are
Page
T&T Lab.(CS-3096), Spring 2023

the same. (One diagonal of a matrix goes from the top left to the bottom right, the other
diagonal goes

from top right to bottom left.) Show by direct computation that if the matrix A is given by

A=np.array([[17, 24, 1, 8, 15],

[23, 5, 7, 14, 16],

[ 4, 6, 13, 20, 22],

[10, 12, 19, 21, 3],

[11, 18, 25, 2, 9]])

The matrix A has 5 row sums (one for each row), 5 column sums (one for each column)
and two

diagonal sums. These 12 sums should all be exactly the same, and you could verify that
they are the

same by printing them and “seeing” that they are the same. It is easy to miss small
differences among so

many numbers, though. Instead, verify that A is a magic square by constructing the 5
column sums and

computing the maximum and minimum values of the column sums. Do the same for the 5
row sums,

and compute the two diagonal sums. Check that these six values are the same. If the
maximum and

minimum values are the same, the flyswatter principle says that all values are the same.

Hints: The function np.diag extracts the diagonal of a matrix, and the function np.fliplr
extracts the other

diagonal

Input/Output Screenshots:
RUN-1:

Page
T&T Lab.(CS-3096), Spring 2023

Source code

import numpy as np

A = np.array([[17, 24, 1, 8, 15], [23, 5, 7, 14, 16], [ 4, 6, 13, 20, 22], [10, 12, 19, 21, 3],
[11, 18, 25, 2, 9]])

col_sums = np.sum(A, axis=0)

row_sums = np.sum(A, axis=1)

diag1_sum = np.sum(np.diag(A))

diag2_sum = np.sum(np.diag(np.fliplr(A)))

print("Column sums: ", col_sums)

print("Row sums: ", row_sums)

print("Diagonal sums: ", diag1_sum, diag2_sum)

Page
T&T Lab.(CS-3096), Spring 2023

sums = np.concatenate((col_sums, row_sums, [diag1_sum, diag2_sum]))

if np.allclose(sums, sums[0]):

    print("A is a magic square!")

else:

    print("A is not a magic square.")

Page

You might also like