0% found this document useful (0 votes)
16 views6 pages

MP2 Exercise 01 - Numpy Arrays

Uploaded by

llyd mpa
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)
16 views6 pages

MP2 Exercise 01 - Numpy Arrays

Uploaded by

llyd mpa
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/ 6

Name: Mapa, Lloyd Anthony M.

Score:

Section: A7 Date: 1/16/25

Objectives:
• Understand the programming fundamentals and the Python language.
• Write Python programs that utilize variables, data types, and operators.

Instructions:
• 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.

• 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] [1, 2, 3]

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]])

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.

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.4914928 , 0.06125694])

In [19] np.random.rand(5,5)
Out[19] array([[0.29537837, 0.16339224, 0.59793495, 0.38228157, 0.93862317],
[0.86542741, 0.56025539, 0.60747526, 0.52678553, 0.20216955],
[0.92727019, 0.76571228, 0.06236871, 0.11645002, 0.91879567],
[0.6572731 , 0.29414536, 0.79339663, 0.01188047, 0.71930624],
[0.11032016, 0.08385352, 0.34212527, 0.98366575, 0.43508595]])

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.16076298, -0.28661525])

In [21] np.random.randn(5,5)
Out[21] array([[ 0.10445628, 0.09510795, -0.13119626, 0.29239654, -0.91719437],
[ 0.49023644, 0.65082592, 1.21614727, -0.79914071, -0.3023583 ],
[-0.24007265, -0.52833285, 0.84553018, -0.78817278, -0.50737923],
[ 1.04441143, -0.51825166, 0.2305683 , -0.90721625, -0.70225508],
[ 0.26726489, -1.16267529, 1.86960602, -1.40240834, 0.56923605]])

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

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

In [23] np.random.randint(1,100,10)
Out[23] array([74, 54, 33, 73, 53, 61, 52, 23, 51, 9], dtype=int32)

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:

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], dtype=int32)

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], dtype=int32)

In [30] ranarr.max()
Out[30] np.int32(39)

In [31] ranarr.argmax()
Out[31] np.int64(7)

In [32] ranarr.min()
Out[32] np.int32(2)

In [33] ranarr.argmin()
Out[33] np.int64(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],
[17],
[18],
[19],
[20],
[21],
[22],
[23],
[24]])

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

You might also like