0% found this document useful (0 votes)
27 views5 pages

Practical 3

The document outlines an experiment involving the use of various NumPy vector functions such as arange(), reshape(), linspace(), randint(), and dot() for array manipulation and mathematical computations in Python. It details the objectives, theory, procedure, and safety precautions for using these functions, emphasizing their importance in scientific computing and data analysis. Additionally, it includes a quiz to assess understanding of the NumPy library and its functionalities.

Uploaded by

xasim42398
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)
27 views5 pages

Practical 3

The document outlines an experiment involving the use of various NumPy vector functions such as arange(), reshape(), linspace(), randint(), and dot() for array manipulation and mathematical computations in Python. It details the objectives, theory, procedure, and safety precautions for using these functions, emphasizing their importance in scientific computing and data analysis. Additionally, it includes a quiz to assess understanding of the NumPy library and its functionalities.

Uploaded by

xasim42398
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/ 5

Enrollment No.

230210107079

Experiment No: 3

Develop a program that shows usage of following NumPy library vector


functions. a) arrange() b) reshape() c) linspace() d) randint() e) dot()
Date:

Competency and Practical Skills:

Competency skills:

• Basic knowledge of computer systems, operating systems, and file systems.


• Familiarity with command-line interfaces (CLI) and graphical user interfaces (GUI).
• Understanding of programming languages, syntax, and logic.

Practical skills:

• Understanding of Data Structures


• Knowledge of Programming Languages
• Familiarity with Data Manipulation Numpy Libraries
• Proficiency in Using IDEs and Text Editors
• Problem Solving and Troubleshooting Skills

Relevant CO: CO2

Objectives: (a) To provide efficient and powerful tools for working with large arrays and
matrices in Python, along with a wide range of mathematical and scientific functions for
manipulating and analyzing these arrays.

Equipment/Instruments: Personal Computer, Internet, Python

Theory:

Here is a brief theory for each of the NumPy vector functions:

a) arrange(): This function is used to create a one-dimensional array with evenly spaced
values within a specified range. The function takes in three arguments: start (optional), stop,
and step (optional). The start argument is the starting value of the sequence (inclusive), the stop
argument is the ending value of the sequence (exclusive), and the step argument is the step size
between values. For example, np.arange(0, 10, 2) creates an array with values [0, 2, 4, 6, 8].

b) reshape(): This function is used to reshape an array into a new shape without changing
its data. The function takes in one argument: the new shape of the array, specified as a tuple of
integers. For example, np.reshape(my_array, (3, 4)) reshapes the array my_array into a 3x4
matrix.

c) linspace(): This function is used to create a one-dimensional array with evenly spaced
values between a specified range. The function takes in three arguments: start, stop, and num
Enrollment No. 230210107079

(optional). The start argument is the starting value of the sequence, the stop argument is the
ending value of the sequence, and the num argument is the number of values to generate. For
example, np.linspace(0, 1, 5) creates an array with values [0., 0.25, 0.5, 0.75, 1.].

d) randint(): This function is used to generate an array of random integers within a


specified range. The function takes in three arguments: low (optional), high, and size (optional).
The low argument is the lower bound of the range (inclusive), the high argument is the upper
bound of the range (exclusive), and the size argument is the shape of the output array. For
example, np.random.randint(0, 10, size=(2, 3)) generates a 2x3 array of random integers
between 0 and 10.

e) dot(): This function is used to perform matrix multiplication between two arrays. The
function takes in two arguments: the two arrays to be multiplied. The arrays must have
compatible shapes for matrix multiplication. For example, if A is a 2x3 array and B is a 3x2
array, np.dot(A, B) performs matrix multiplication between A and B and returns a 2x2 array.

Overall, these NumPy vector functions are commonly used for manipulating and analyzing
arrays in scientific computing and data analysis. By using these functions in a program, you
can efficiently perform operations on large arrays and matrices in Python.

Safety and necessary Precautions:

1. Install NumPy from a trusted source.


2. Keep NumPy updated.
3. Understand data types.
4. Avoid modifying arrays in place.
5. Use vectorized operations.
6. Handle exceptions and errors.

Procedure:

1. Import the NumPy library: Begin your program by importing the NumPy library using
the import statement.

• import numpy as np

2. Create an array: Create an array using one of the NumPy functions such as arrange() or
linspace(). You can also create an array from an existing data source such as a CSV file.

• arr1 = np.arange(0, 10, 2)


print("Array using arange():", arr1)

# Another array using linspace() arr2


= np.linspace(1, 5, 5)
print("Array using linspace(1 to 5, 5 values):", arr2)
Enrollment No. 230210107079

3. Reshape the array: Use the reshape() function to reshape the array to the desired shape.
For example, you can reshape a one-dimensional array into a two-dimensional array.

• reshaped_arr = np.arange(12).reshape(3, 4)
print("\nReshaped array (3x4):\n", reshaped_arr)

4. Generate random numbers: Use the randint() function to generate an array of random
integers within a specified range.

• rand_arr = np.random.randint(0, 20, size=(2, 3))


print("\nRandom integer array (2x3):\n", rand_arr)

5. Perform matrix multiplication: Use the dot() function to perform matrix multiplication
between two arrays.

• A = np.array([[1, 2], [3, 4]])


B = np.array([[5, 6], [7, 8]])
dot_product = np.dot(A, B)

6. Print the results: Print the resulting arrays to the console using the print() function.

• print("\nMatrix A:\n", A)
print("Matrix B:\n", B)
print("Dot Product of A and B:\n", dot_product)

Observations:

1 & 2.

3.

4.
Enrollment No. 230210107079

5 & 6.

Conclusion:

In this experiment, we explored the usage of important NumPy vector functions such as
arange(), reshape(), linspace(), randint(), and dot(). These functions enabled us to create arrays
with evenly spaced values, reshape arrays into different dimensions without altering their data,
generate evenly spaced numbers within a given range, produce random integer arrays for
testing and simulation purposes, and perform efficient matrix multiplication using the dot
product. Through these operations, we observed how NumPy provides powerful and efficient
tools for array creation, manipulation, and mathematical computations, making it an essential
library for scientific computing, data analysis, and machine learning applications.

Quiz:

1. What is the purpose of the NumPy library?


Ans: NumPy provides efficient tools for working with arrays and matrices, along with mathematical
and scientific functions for data manipulation and analysis.

2. Which NumPy function can be used to create an array with evenly spaced values?
Ans: np.arange() or np.linspace() (depending on whether you want step-based or evenly divided
values).

3. Which NumPy function can be used to generate an array of random integers within a
specified range?
Ans: np.random.randint().

4. How can you perform matrix multiplication between two arrays in NumPy?
Ans: By using np.dot(A, B) or the @ operator.

5. What is the purpose of the reshape() function in NumPy?


Ans: reshape() changes the shape (rows × columns) of an array without altering its data.
Enrollment No. 230210107079

Suggested Reference:
1. https://numpy.org/doc/stable/
2. https://numpy.org/doc/stable/user/index.html
3. https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Numpy_Python_
Cheat_Sheet.pdf
4. https://numpy.org/devdocs/user/quickstart.html
5. https://www.datacamp.com/community/tutorials/python-numpy-tutorial

References used by the students: (Sufficient space to be provided)

Rubric wise marks obtained:

You might also like