Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Week 2 Exercise 03 - NumPy Operations

Uploaded by

ellinejcruz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Week 2 Exercise 03 - NumPy Operations

Uploaded by

ellinejcruz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CS158-1L: Artificial Intelligence Laboratory

Week 2 Exercise 03 - NumPy Operations

Name: Cruz, Elline Joyce Score


:

Sectio C5 Date: 06/17


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 exercise, please follow the sample commands in Python provided to
you. Once you have completed the assignment, please submit the IPython file and
this document to me. You have one week to complete the exercise from the assigned
date. Please let me know if you have any questions or concerns regarding the
assignment.

2. When submitting your completed assignment, please name the IPython file as
follows: "surname_firstname_MP1Exercise". Replace "surname" with your last name,
"firstname" with your first name, and "MP2Exercise" with the name of the machine
problem.

For example, if your name is John Smith and the machine problem is
"PythonExercise2", the file name should be "smith_john_PythonExercise1.ipynb".

Please adhere to this naming convention when submitting your file. This will ensure I
can quickly identify your submission and provide appropriate feedback.

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Week 2 Exercise 03 - NumPy Operations

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] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [2] arr + arr

Out[2] arr+arr
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])

In [3] arr * arr

Out[3] array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81])

In [4] arr - arr

Out[4] array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

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] array([nan, 1., 1., 1., 1., 1., 1., 1., 1., 1.])

In [6] # Also a warning (but not an error) relating to infinity


1/arr

Out[6] array([ inf, 1. , 0.5 , 0.33333333, 0.25 ,


0.2 , 0.16666667, 0.14285714, 0.125 , 0.11111111])

In [7] arr**3

Out[7] array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729], dtype=int32)

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Week 2 Exercise 03 - NumPy Operations

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)

Out[8] array([0. , 1. , 1.41421356, 1.73205081, 2. ,


2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])

In [9] # Calculating exponential (e^)


np.exp(arr)

Out[9] array([1.00000000e+00, 2.71828183e+00, 7.38905610e+00,


2.00855369e+01,
5.45981500e+01, 1.48413159e+02, 4.03428793e+02,
1.09663316e+03,
2.98095799e+03, 8.10308393e+03])

In [10] # Trigonometric Functions like sine


np.sin(arr)

Out[10] array([ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ,


-0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849])

In [11] # Taking the Natural Logarithm


np.log(arr)

Out[11] array([ -inf, 0. , 0.69314718, 1.09861229, 1.38629436,


1.60943791, 1.79175947, 1.94591015, 2.07944154, 2.19722458])

Summary Statistics on Arrays: NumPy also offers common statistics like sum,
mean, and max.

In [12] arr = np.arange(0,10)


arr

Out[12] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [13] arr.sum()

Out[13] 45

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Week 2 Exercise 03 - NumPy Operations

In [14] arr.mean()

Out[14] 4.5

In [15] arr.max()

Out[15] 9

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.

In [16] arr_2d = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])


arr_2d

Out[16] array([[ 1, 2, 3, 4],


[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])

In [17] arr_2d.sum(axis=0)

Out[17] array([15, 18, 21, 24])

In [18] arr_2d.shape

Out[18] (3, 4)

Prepared by: Raymond Sedilla, Mapua University

You might also like