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

Numpy

Uploaded by

trishasag7604
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Numpy

Uploaded by

trishasag7604
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Aim: Python Program on Numpy Arrays

Description: NumPy is a Python library used for working with arrays. NumPy stands for
Numerical Python. NumPy aims to provide an array object that is up to 50x faster than
traditional Python lists.
Program:
A) Using a numpy module create an array and check the following:
1. Type of array
2. Axes of array
3. Shape of array
4. Type of elements in array
import numpy as np
arr=np.array([[1,2,3],[4,2,5]])
print("Array is of type:",type(arr))
print("no.of dimensions:",arr.ndim)
print("Shape of array:",arr.shape)
print("Size of array:",arr.size)
print("Array stores elements of type:",arr.dtype)
output:

Array is of type: <class 'numpy.ndarray'>


no.of dimensions: 2
Shape of array: (2, 3)
Size of array: 6
Array stores elements of type: int64

B) Using a numpy module create array and check the following:

1. List with type float

2. 3*4 array with all zeros

3. From tuple

4. Random values

import numpy as np

1 # Creating array from list with floating-point type


a = np.array([[1, 2, 3], [4, 5, 6]], dtype = 'float')
print ("Array created from list:\n", a)
output:
Array created from list:
[[1. 2. 3.]
[4. 5. 6.]]
2.# Creating 3X4 arrays with all zeros and all ones separate
ly

c = np.zeros((3, 4))
d = np.ones((3, 4))
print ("\nAn array initialized with all zeros:\n", c)
print ("\nAn array initialized with all ones:\n", d)
output:

An array initialized with all zeros:


[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

An array initialized with all ones:


[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
3. # Creating array from tuple

b = np.array((2, 4, 6, 8))
print ("\nArray created from tuple:\n", b)
output:

Array created from tuple:


[2 4 6 8]

4. # Create an array with random values

f = np.random.random((3, 3))
print ("\nA random array:\n", f)
output:

A random array:
[[0.94004498 0.18001717 0.62266526]
[0.57897408 0.55372992 0.44950854]
[0.94546098 0.77436751 0.04605927]]
C) Using a numpy module create array and check the following:

1. Reshape 3X4 array to 2X2X3 array

2. Sequence of integers from 0 to 200 with steps of 10

3. Flatten array

4. Constant value array of complex type

Program:

1. # Reshaping 3X4 array to 2X2X3 array


arr = np.array([[1, 2, 3, 4],
[5, 2, 4, 2],
[1, 2, 0, 1]])

newarr = arr.reshape(2, 2, 3)

print ("\nOriginal array:\n", arr)


print ("Reshaped array:\n", newarr)
Output:
Original array:
[[1 2 3 4]
[5 2 4 2]
[1 2 0 1]]
Reshaped array:
[[[1 2 3]
[4 5 2]]

[[4 2 1]
[2 0 1]]]
2.#Create a sequence of integers from 0 to 200 with steps
of 10
import numpy as np
f = np.arange(0, 200, 10)
print ("\nA sequential array with steps of 10:\n", f)
output:
A sequential array with steps of 10:
[ 0 10 20 30 40 50 60 70 80 90 100 110 120 130
140 150 160 170 180 190]

3.# Flatten array


arr = np.array([[1, 2, 3], [4, 5, 6]])
flarr = arr.flatten()

print ("\nOriginal array:\n", arr)


print ("Fattened array:\n", flarr)
output:

Original array:
[[1 2 3]
[4 5 6]]
Fattened array:
[1 2 3 4 5 6]

4. # Create a constant value array of complex type


d = np.full((4, 4), 8, dtype = 'complex')
print ("\nAn array initialized with all 8s."
"Array type is complex:\n", d)
Output:

An array initialized with all 8s.Array type is complex:


[[8.+0.j 8.+0.j 8.+0.j 8.+0.j]
[8.+0.j 8.+0.j 8.+0.j 8.+0.j]
[8.+0.j 8.+0.j 8.+0.j 8.+0.j]
[8.+0.j 8.+0.j 8.+0.j 8.+0.j]]

D) Write a NumPy program to compute the multiplication of


two given matrixes.
Program:
import numpy as np
p = [[1, 0], [0, 1]]
q = [[1, 2], [3, 4]]
print("original matrix:")
print(p)
print(q)
result1 = np.dot(p, q)
print("Result of the said matrix multiplication:")
print(result1)
output:

original matrix:
[[1, 0], [0, 1]]
[[1, 2], [3, 4]]
Result of the said matrix multiplication:
[[1 2]
[3 4]]

E) Write a NumPy program to compute the outer product of


two given vectors.
import numpy as np
p = [[1, 0], [0, 1]]
q = [[1, 2], [3, 4]]
print("original matrix:")
print(p)
print(q)
result = np.outer(p, q)
print("Outer product of the said two vectors:")
print(result)

output:
original matrix:
[[1, 0], [0, 1]]
[[1, 2], [3, 4]]
Outer product of the said two vectors:
[[1 2 3 4]
[0 0 0 0]
[0 0 0 0]
[1 2 3 4]]

You might also like