CS158-1L: Artificial Intelligence Laboratory
MP2 Assignment 3
Name: Score
:
Sectio Date:
n:
Objectives:
● Understand the programming fundamentals and the Python language.
● Write Python programs that utilize variables, data types, and operators.
Instructions:
1. To complete this assignment, please follow the sample commands in Python provided
to you. Once you have completed the assignment, please submit the notebook file.
2. When submitting your completed assignment, please name the notebook file as
follows: "surname_firstname_MP2AS3".
3. Header Comments
Include comprehensive header comments at the beginning of your script.
# PROGRAM DESCRIPTION: [Description]
# AUTHOR INFORMATION:
# Written by: [Your Full Name]
# Date: [Current Date]
# Time: [Current Time]
# Program: [Your Program (e.g., BSIE)]
# Course: [Your Course and Section]
# School: [Your School]
Python Assignment Rubric
1. File Naming
a. File is correctly named with surname_firstname_MP2AS3.
2. Header Comments
a. Comprehensive header comments with a clear program description, author
information, and details.
3. Comments in Code
a. Thorough comments for each section of the code, including the purpose and
functionality.
4. Functionality
a. Code executes correctly and produces accurate results.
5. Correct Output
Prepared by: Raymond Sedilla, Mapua University
CS158-1L: Artificial Intelligence Laboratory
MP2 Assignment 3
Arithmetic: You can easily perform array with array arithmetic, or scalar
with array arithmetic.
In [1] import numpy as np
arr = np.arange(0,10)
arr
Out[1]
In [2] arr + arr
Out[2]
In [3] arr * arr
Out[3]
In [4] arr - arr
Out[4]
In [5] # This will raise a Warning on division by zero, but not an error!
# It just fills the spot with nan
arr/arr
Out[5]
In [6] # Also a warning (but not an error) relating to infinity
1/arr
Out[6]
In [7] arr**3
Out[7]
Universal Array Functions: NumPy comes with many universal array
functions, or ufuncs, which are essentially just mathematical operations that
can be applied across the array.
In [8] # Taking Square Roots
np.sqrt(arr)
Prepared by: Raymond Sedilla, Mapua University
CS158-1L: Artificial Intelligence Laboratory
MP2 Assignment 3
Out[8]
In [9] # Calculating exponential (e^)
np.exp(arr)
Out[9]
In [10] # Trigonometric Functions like sine
np.sin(arr)
Out[10]
In [11] # Taking the Natural Logarithm
np.log(arr)
Out[11]
Summary Statistics on Arrays: NumPy also offers common statistics like sum,
mean, and max.
In [12] arr = np.arange(0,10)
arr
Out[12]
In [13] arr.sum()
Out[13]
In [14] arr.mean()
Out[14]
In [15] arr.max()
Out[15]
Axis Logic: When working with 2-dimensional arrays (matrices) we have to
consider rows and columns. This becomes very important when we get to the
section on pandas. In array terms, axis 0 (zero) is the vertical axis (rows),
and axis 1 is the horizontal axis (columns). These values (0,1) correspond to
the order in which arr. shape values are returned.
Prepared by: Raymond Sedilla, Mapua University
CS158-1L: Artificial Intelligence Laboratory
MP2 Assignment 3
In [16] arr_2d = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
arr_2d
Out[16]
In [17] arr_2d.sum(axis=0)
Out[17]
In [18] arr_2d.shape
Out[18]
Prepared by: Raymond Sedilla, Mapua University