You are on page 1of 26

Numpy

NUMERICAL COMPUTING TOOLS


• NumPy is the fundamental package for scientific computing in
Python. It is a Python library that provides a multidimensional
array object, various derived objects (such as masked arrays and
matrices), and an assortment of routines for fast operations on
arrays, including mathematical, logical, shape manipulation,
sorting, selecting, I/O, discrete Fourier transforms, basic linear
algebra, basic statistical operations, random simulation and much
more.
Np.array
• Core package in Numpy library
• This encapsulates n-dimensional arrays of homogeneous data
types, with many operations being performed in compiled code
for performance.
Numpy v.s. Python lists
• NumPy arrays have a fixed size at creation, unlike Python lists (which
can grow dynamically). Changing the size of an ndarray will create a
new array and delete the original.

• The elements in a NumPy array are all required to be of the same data
type, and thus will be the same size in memory. The exception: one can
have arrays of (Python, including NumPy) objects, thereby allowing for
arrays of different sized elements.

• NumPy arrays facilitate advanced mathematical and other types of


operations on large numbers of data. Typically, such operations are
executed more efficiently and with less code than is possible using
Python’s built-in sequences.
Coordinate → Array
• A (1,1) → np.array([1,2]) B (5,5)
• B (5,5) → np.array([5,5])
A (1,1)
Practice 1
• How to code three-dimensional coordinate with Numpy?
– For example, A (3,5,6)
A (3,5,6)

• How about four-dimensional point B (1,5,8,9)?


• How about ten-dimensional point C (1,5,8,9,7,9,1,2,0,1)?
Practice 1
• A: np.array([3,5,6])
• B: np.array([1,5,8,9])
• C: np.array ([1,5,8,9,7,9,1,2,0,1])
Arithmetic operation between arrays
• np.add()
• np.subtract()
• np.multiply()
• np.divide()
• np.power()
• np.sqrt()
Example
import numpy as np
a = np.array([1,3,6])
b = np.array([2,7,8])
print(np.add(a,b))

Result: [3,10,14]
np.power
• Np.power(x,y)
• X: The numbers in the first array serve as the bases.
• Y: The numbers in the second array serve as the exponents.
Example
np.power([1,3,9],2) → [1 9 36]
np.power([1,3,6],[2,7,8]) → [1 2187 1679616]
Practice 2
• Array A: [4,6,7,8,2,6,1,9,1]
• Array B: [7,9,0,1,5,7,3,5,1]
Get the results:
– Array A + Array B
– Array A - Array B
– Array A × Array B
– Array A ÷ Array B
– The cube of array A
– The cube of array B
– The square root of Array A × Array B
– The square root of Array A + Array B
Euclidean
Euclidean

B
��

A
��

�� ��

�ℎ� �������� ������� � ��� � =   �� − �� 2 + �� − �� 2
Calculate Euclidean with Python
import numpy as np
B(5,5)
def eucliDist(A,B): ��

return np.sqrt(sum(np.power((A - B), 2)))


A(1,1)
��
A = np.array([1,1])
�� ��
B = np.array([5,5])
print(eucliDist(A,B))

�ℎ� �������� ������� � ��� � =   �� − �� 2 + �� − �� 2
Practice 3
• Calculate the Euclidean of the following points.
– One-dimensional Point: A(1) and B(8)
– Two-dimensional Point: A(2,3) and B(3,9)
– Three-dimensional Point: A(1,5,6) and B(2,3,7)
– Six-dimensional Point: A(3, 9, 7, 2, 4, 5) and B(-5, -3, -9, 0, 6, 2)
Recommendation System
How does recommendation system work?
• https://www.bilibili.com/video/BV1TS4y197yi?spm_id_from=333.9
99.0.0
1. Import packages
2. Read the files

Read the file “movies.csv” and set 'videoId',


'title', ‘genres’ as the name of columns.
Read the file “ratings.csv” and set 'userId',
'videoId', 'rating', 'timestamp’ as the name of
Connect two dataframes via ‘videoId’ columns.
Create a new file “data. csv with columns
'userId', 'rating', 'videoId', and 'title'
3. Create new dictionary

Create a new dictionary to store the ratings of movies by each user.

Use the user ID to create the user if there is no user ID in the dictionary.
Add the user ID to the key dictionary if there is user ID.
4. Calculate Euclidean between two users

Find the movies both reviewed by two users.

Calculate the Euclidean between them.

The smaller the Euclidean, the more similar these two users are.
5. Calculate the similarity between two users
6. Create a console
Ask people to enter the user ID to the console.

If the user is not in the original dataset, the console will report
an error and ask people to re-enter.

The systems recommend top 10 new movies to the


current user in descending order, according to the
user rating record with the highest similarity to the
current user.

Output the recommended movies,


according to the user ID.
7. Result
Practice 4
• Randomly select one ID and check the recommended movies for
this ID.

You might also like