0% found this document useful (0 votes)
21 views3 pages

Narayanan.m - Practice Exercise PDF

This document discusses solving problems using NumPy in Python. It covers matrix manipulations like solving matrix equations, adding and squaring arrays, finding max/min from arrays, matching elements between arrays, and stacking/concatenating arrays. 5 problems are presented and solved using NumPy functions like np.linalg.solve(), np.array(), np.vstack(), np.hstack(), np.where(), and array operations.

Uploaded by

Narayanan M
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)
21 views3 pages

Narayanan.m - Practice Exercise PDF

This document discusses solving problems using NumPy in Python. It covers matrix manipulations like solving matrix equations, adding and squaring arrays, finding max/min from arrays, matching elements between arrays, and stacking/concatenating arrays. 5 problems are presented and solved using NumPy functions like np.linalg.solve(), np.array(), np.vstack(), np.hstack(), np.where(), and array operations.

Uploaded by

Narayanan M
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/ 3

Assignment 1 NumPy

#Import the libraries


import numpy as np

Problem 1
Matrix Manipulations
Lets first create a matrix and perform some manipulations of it.
Using numpy's matrix data structure, define the following matricies:

[ )
3 5 9
A= 3 3 4
5 9 17

[)
2
B= 1
4

After this solve the matrix equation:


A x=B
A = np.matrix([[3,5,9], [3,3,4], [5,9,17]])
B = np.matrix([[2], [1], [4]])
x = np.linalg.solve(A, B)
print(x)

[[ 1.]
[-2.]
[ 1.]]

Problem 2
Create a result array by adding the following two NumPy arrays. Next, modify the result
array by calculating the square of each element Input:

a r r a y On e= [ 215 6 9
18 27 )
[4
a r r a y T w o= 15 33
7 1)
24

arrayOne = np.array([[5, 6, 9], [21, 18, 27]])


arrayTwo = np.array([[15, 33, 24], [4, 7, 1]])
result_array = arrayOne + arrayTwo
print("Result Array after adding:\n",result_array)
result_array = result_array ** 2
print("Result Array after Squaring:\n",result_array)

Result Array after adding:


[[20 39 33]
[25 25 28]]
Result Array after Squaring:
[[ 400 1521 1089]
[ 625 625 784]]

Problem 3
Print max from axis 0 and min from axis 1 from the following 2-D array.

[ )
34 43 73
s a m p l e A r r a y= 82 22 12
53 94 66

sampleArray = np.array([[34,43,73], [82,22,12], [53,94,66]])


print("Max from axis 0: ", sampleArray.max(axis=0))
print("Min from axis 1: ", sampleArray.min(axis=1))

Max from axis 0: [82 94 73]


Min from axis 1: [34 12 53]

Problem 4
Get the positions where elements of a and b match Input: a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8]) Desired Output: array([1, 3, 5, 7])
a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])
matching_positions = np.where(a == b)
print("Matching Positions: ", matching_positions[0])

Matching Positions: [1 3 5 7]

Problem 5
Given

a= [13 24)
b=
[57 68)
Obtain c and d

[ )
1 2
c= 3 4
5 6
7 8

d= [ 13 2 5 6
4 7 8 )
a = np.array([[1,2], [3,4]])
b = np.array([[5,6], [7,8]])
c = np.vstack((a, b))
print("c: \n", c)
d = np.hstack((a, b))
print("d: \n", d)

c:
[[1 2]
[3 4]
[5 6]
[7 8]]
d:
[[1 2 5 6]
[3 4 7 8]]

You might also like