Professional Documents
Culture Documents
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.
# 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
Out[4] [1, 2, 3]
In [5] np.array(my_list)
In [7] np.array(my_matrix)
In [8] np.arange(0,10)
In [9] np.arange(0,11,2)
In [10] np.zeros(3)
In [11] np.zeros((5,5))
In [12] np.ones(3)
In [13] np.ones((3,3))
In [14] np.linspace(0,10,3)
In [15] np.linspace(0,5,20)
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. ])
In [17] np.eye(4)
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)
In [19] np.random.rand(5,5)
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)
In [21] np.random.randn(5,5)
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)
In [26] arr
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)
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()
Out[32] 2
In [33] ranarr.argmin()
Out[33] 9
In [34] # Vector
arr.shape
Out[34] (25,)
In [36] arr.reshape(1,25).shape
In [37] arr.reshape(25,1)
[17],
[18],
[19],
[20],
[21],
[22],
[23],
[24]])
In [38] arr.reshape(25,1).shape
Out[38] (25, 1)