You are on page 1of 8

CS158-1L: Artificial Intelligence Laboratory

Machine Problem #2: Exercises

Name: PIMENTEL EMMANUEL JAMES P Score:

Section: A5 Date:

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.

Numpy: NumPy is a powerful linear algebra library for Python. It is essential that
almost all of the libraries in the PyData ecosystem (pandas, scipy, sci-kit-learn,
etc.) rely on NumPy as one of their main building blocks. We will use it to generate
data for our analysis examples later!

In [1] # The first thing to know is that when we're using numpy, we must import it.

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Machine Problem #2: Exercises

# It should be easy just to install Conda, install Numpy or Pip Numpy where you run
#that at your command line
# once you've already installed it, go ahead and type out import numpy and all
#lowercase as
# and then by convention we import numpy as np.

import numpy as np

In [2] # For example, if I say my list is equal to a standard python list of one, two, three,
# and then I check the type of my list, it's currently just a list type to
# transform something into a numpy. I can say np array and then pass in
# what I want to transform, and now you'll notice the output looks
# different and our outputs, it is showing that it's a numpy.

my_list = [1,2,3]
my_array = np.array([1,2,3])

In [3] type(my_list)

Out[3] list

Creating NumPy Arrays from Objects


From a Python List: We can create an array directly converting a list or list of lists

In [4] my_list = [1,2,3]


my_list

Out[4] [1, 2, 3]

In [5] np.array(my_list)

Out[5] array([1, 2, 3])

In [6] my_matrix = [[1,2,3],[4,5,6],[7,8,9]]


my_matrix

Out[6] [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In [7] np.array(my_matrix)

Out[7] array([[1, 2, 3],


[4, 5, 6],
[7, 8, 9]])

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Machine Problem #2: Exercises

Built-in Methods to create arrays


arange: Return evenly spaced values within a given interval

In [8] np.arange(0,10)

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

In [9] np.arange(0,11,2)

Out[9] array([ 0, 2, 4, 6, 8, 10])

zeros and ones: Generate arrays of zeros or ones

In [10] np.zeros(3)

Out[10] array([0., 0., 0.])

In [11] np.zeros((5,5))

Out[11] array([[0., 0., 0., 0., 0.],


[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])

In [12] np.ones(3)

Out[12] array([1., 1., 1.])

In [13] np.ones((3,3))

Out[13] array([[1., 1., 1.],


[1., 1., 1.],
[1., 1., 1.]])

linspace: Return evenly spaced numbers over a specified interval.

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Machine Problem #2: Exercises

In [14] np.linspace(0,10,3)

Out[14] array([ 0., 5., 10.])

In [15] np.linspace(0,5,20)

Out[15] array([0. , 0.26315789, 0.52631579, 0.78947368, 1.05263158,


1.31578947, 1.57894737, 1.84210526, 2.10526316, 2.36842105,
2.63157895, 2.89473684, 3.15789474, 3.42105263, 3.68421053,
3.94736842, 4.21052632, 4.47368421, 4.73684211, 5. ])

In [16] np.linspace(0,5,21)

Out[16] array([0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. , 2.25, 2.5 ,
2.75, 3. , 3.25, 3.5 , 3.75, 4. , 4.25, 4.5 , 4.75, 5. ])

eye: Creates an identity matrix

In [17] np.eye(4)

Out[17] array([[1., 0., 0., 0.],


[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])

Random: Numpy also has lots of ways to create random number arrays:
rand: Creates an array of the given shape and populates it with random samples
from a uniform distribution over [0, 1)

In [18] np.random.rand(2)

Out[18] array([0.22364441, 0.93218782])

In [19] np.random.rand(5,5)

Out[19] array([[0.09054403, 0.59348172, 0.20190332, 0.10594516, 0.61052257],


[0.19516589, 0.90622805, 0.60528979, 0.91728956, 0.812276 ],
[0.10421094, 0.63885083, 0.40927573, 0.62851867, 0.66410273],
[0.08933187, 0.85178466, 0.21696504, 0.98166696, 0.95682855],

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Machine Problem #2: Exercises

[0.50874629, 0.96723559, 0.84542779, 0.37959355, 0.96433086]])

randn: Returns a sample (or samples) from the "standard normal" distribution [σ =
1]. Unlike rand which is uniform, values closer to zero are more likely to appear.

In [20] np.random.randn(2)

Out[20] array([-0.02101315, -0.02924498])

In [21] np.random.randn(5,5)

Out[21] array([[-1.12147257, 1.77825391, -1.49312453, 1.62496088, 0.30949358],


[-0.00406559, -0.07786659, 0.67442012, 1.06463074, -2.09323755],
[-1.46147417, 0.60230483, 0.37839592, -0.78622365, 2.09046231],
[-0.06152646, 1.3316148 , 2.1897486 , 1.02042582, -1.2954735 ],
[-0.63751347, -1.13993305, -0.37417797, 0.44409007, -0.38431511]])

randint: Returns random integers from low(inclusive) to high (exclusive).

In [22] np.random.randint(1,100)

Out[22] 96

In [23] np.random.randint(1,100,10)

Out[23] array([25, 66, 65, 5, 26, 40, 13, 26, 78, 38])

seed: Can be used to set the random state, so that the same "random" results can
be reproduced.

In [24] np.random.seed(42)
np.random.rand(4)

Out[24] array([0.37454012, 0.95071431, 0.73199394, 0.59865848])

Array Attributes and Methods:

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Machine Problem #2: Exercises

In [25] arr = np.arange(25)


ranarr = np.random.randint(0,50,10)

In [26] arr

Out[26] array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,


17, 18, 19, 20, 21, 22, 23, 24])

In [27] ranarr

Out[27] array([38, 18, 22, 10, 10, 23, 35, 39, 23, 2])

Reshape: Returns an array containing the same data with a new shape.

In [28] arr.reshape(5,5)

Out[28] array([[ 0, 1, 2, 3, 4],


[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])

max, min, argmax, argmin: These are useful methods for finding max or min
values. Or to find their index locations using argmin or argmax.

In [29] ranarr

Out[29] array([38, 18, 22, 10, 10, 23, 35, 39, 23, 2])

In [30] ranarr.max()

Out[30] 39

In [31] ranarr.argmax()

Out[31] 7

In [32] ranarr.min()

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Machine Problem #2: Exercises

Out[32] 2

In [33] ranarr.argmin()

Out[33] 9

Shape: Shape is an attribute that arrays have (not a method).

In [34] # Vector
arr.shape

Out[34] (25,)

In [35] # Notice the two sets of brackets


arr.reshape(1,25)

Out[35] array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,


16, 17, 18, 19, 20, 21, 22, 23, 24]])

In [36] arr.reshape(1,25).shape

Out[36] (1, 25)

In [37] arr.reshape(25,1)

Out[37] array([[ 0],


[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12],
[13],
[14],
[15],
[16],

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Machine Problem #2: Exercises

[17],
[18],
[19],
[20],
[21],
[22],
[23],
[24]])

In [38] arr.reshape(25,1).shape

Out[38] (25, 1)

Prepared by: Raymond Sedilla, Mapua University

You might also like