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

C 27 Prac 4

Uploaded by

ganeta2963
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)
12 views3 pages

C 27 Prac 4

Uploaded by

ganeta2963
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

Name: Shreyansh Bhagwat

Year and Branch: 2nd AIML

Software Lab

Practical 4

C2-27

A. Write a numpy program to test none of the element of given array is zero.
In [4]: import numpy as np
arr=np.array([1,2,3,4,5])
if(0 in arr):
print("zero exists")
else:
print("no zero")

no zero

B. Write a NumPy program to create a 3x3 matrix with values ranging from 2
to 10.
In [6]: import numpy as np
values = np.arange(2, 11)
matrix = values.reshape(3, 3)
print(matrix)

[[ 2 3 4]
[ 5 6 7]
[ 8 9 10]]

C. Write a NumPy program to extract all odd numbers from 1 D array.


In [8]: import numpy as np
arr2 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
arr3 = []
for i in arr2:
if (i % 2 != 0):
arr3.append(i)
print(arr3)

[1, 3, 5, 7, 9]

D. Write a command to perform following operations on 4x4

ndarrays namely P and Q.

i) Adding 10 to P

ii) Multiplication of arrays P & Q

iii) Divide all elements of Q by 7

iv) Calculate the reminders of all elements of P when


divided by 7.
In [12]: import numpy as np
P = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
Q = np.array([[16, 15, 14, 13], [12, 11, 10, 9], [8, 7, 6, 5], [4, 3, 2, 1]])
P_plus_10 = P + 10
print("P + 10:\n", P_plus_10)
P_times_Q = P * Q
print("P * Q:\n", P_times_Q)
Q_divided_by_7 = Q / 7
print("Q / 7:\n", Q_divided_by_7)
P_mod_7 = P % 7
print("P % 7:\n", P_mod_7)

P + 10:
[[11 12 13 14]
[15 16 17 18]
[19 20 21 22]
[23 24 25 26]]
P * Q:
[[16 30 42 52]
[60 66 70 72]
[72 70 66 60]
[52 42 30 16]]
Q / 7:
[[2.28571429 2.14285714 2. 1.85714286]
[1.71428571 1.57142857 1.42857143 1.28571429]
[1.14285714 1. 0.85714286 0.71428571]
[0.57142857 0.42857143 0.28571429 0.14285714]]
P % 7:
[[1 2 3 4]
[5 6 0 1]
[2 3 4 5]
[6 0 1 2]]

In [13]: import numpy as np


array = np.array([[1, 2, 3], [4, 5, 6]])
print("Type of array:", type(array))
print("Axes of array:", array.ndim)
print("Shape of array:", array.shape)
print("Type of elements in array:", array.dtype)

Type of array: <class 'numpy.ndarray'>


Axes of array: 2
Shape of array: (2, 3)
Type of elements in array: int32

In [14]: import numpy as np


float_array = np.array([1.0, 2.0, 3.0], dtype=float)
print("Float array:\n", float_array)
zero_array = np.zeros((3, 4))
print("3x4 zero array:\n", zero_array)
tuple_array = np.array((1, 2, 3, 4, 5, 6)).reshape(2, 3)
print("Array from tuple:\n", tuple_array)
random_array = np.random.rand(3, 4)
print("Random array:\n", random_array)

Float array:
[1. 2. 3.]
3x4 zero array:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
Array from tuple:
[[1 2 3]
[4 5 6]]
Random array:
[[0.76029967 0.26013447 0.02800854 0.62677863]
[0.43351522 0.74409579 0.5171985 0.392448 ]
[0.09724443 0.36784024 0.44662988 0.83359967]]

In [15]: import numpy as np


array_3x4 = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
reshaped_array = array_3x4.reshape(2, 2, 3)
print("Reshaped array:\n", reshaped_array)
sequence_array = np.arange(0, 30, 5)
print("Sequence array:\n", sequence_array)
flattened_array = reshaped_array.flatten()
print("Flattened array:\n", flattened_array)
complex_array = np.full((3, 4), 1 + 2j, dtype=complex)
print("Complex array:\n", complex_array)

Reshaped array:
[[[ 1 2 3]
[ 4 5 6]]

[[ 7 8 9]
[10 11 12]]]
Sequence array:
[ 0 5 10 15 20 25]
Flattened array:
[ 1 2 3 4 5 6 7 8 9 10 11 12]
Complex array:
[[1.+2.j 1.+2.j 1.+2.j 1.+2.j]
[1.+2.j 1.+2.j 1.+2.j 1.+2.j]
[1.+2.j 1.+2.j 1.+2.j 1.+2.j]]

In [ ]:

You might also like