Description
This lab aims to help the students refresh the basics of python, particularly, NumPy. Please write the report, including the codes, and screen-shot the results, and send to Yikai, by 17:00 April 1st, 2020.
- Write a Python function to sum all the numbers in a list.
- Write a Python function that takes a list and returns a new list with unique elements of the first list.
e.g.,
Input:[1, 2, 3, 3, 3, 3, 4, 5]. Output: [1, 2, 3, 4, 5].
- Write a Python function that checks whether a passed string is palindrome or not. A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.
- Write a NumPy program to find the real and imaginary parts of an array of complex numbers. e.g.,
Input: array [ 1.00000000+0.j 0.70710678+0.70710678j] Output: array [[1, 0], [0.70710678, 0.70710678]]
- Write a Python program to add two binary numbers. e.g.,
Input : (’11’, ’1’) Output : 100
- You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself.
e.g.,
Input: (2->4->3)+(5->6->4) Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807. Linked list is defined as follow
# Definition for singly-linked list.
1
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
- Implement bubble sort
- Implement merge sort
- Implement quick sort
- Implement shell sort
- Implement linear regression model and use autograd to optimize it by Pytorch.
- Implement logistic regression model and use autograd to optimize it by Pytorch.
- Implement linear SVM model for binary classification task and use autograd to optimize it by Pytorch. Hint: you may use the loss of max[0,1−y(wx+b)]
- Add a Frobenius norm penalty for the weight w in your SVM model by two different ways.. 1
Hint: Frobenius norm of a matrix A is ∥A∥F = ni=1 mj=1 |aij|2 2 .
- Learn how to use linear regression1, logistic regression2, and SVM3 by scikit-learn.
- Download CIFAR-10 dataset4 and visualize some of its images.
- Write a dataset class for loading CIFAR-10. Make sure it could be transferred to Pytorch Dataloader. Hint: If you find this part a little hard, check the official code5 and make sure you understand each part.
- Read6 and learn how to use torchvision.transforms to transform images.
- Run one epoch for loading CIFAR-10 with Pytorch Dataloader and test the loading time of different batch_size
(1, 4, 64, 1024), different num_workers (0,1,4,16), and whether use pin_memory or not.
- Calculate the mean and std of CIFAR-10’ training set within each RGB channel.
- Image to character painting
(a) Target
Converting the RGB color image to character painting with Python code• Character painting is a combination of a series of characters. We can think of characters as relatively large pixels. A character can represent a color. The more types of characters, the more colors can be represented, and the picture will be more hierarchical sense
1https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html#sklearn.linear_model.LinearRegression 2https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression 3https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html
4 https://www.cs.toronto.edu/~kriz/cifar.html
5 https://pytorch.org/docs/stable/_modules/torchvision/datasets/cifar.html#CIFAR10 6 https://pytorch.org/docs/stable/torchvision/transforms.html
2
(b) Requirements
• Python 3.5 • pillow 5.1.0
(c) Method
- Use PIL (pillow) to get the input picture
- Use the following formula to map RGB values to gray values (note that this formula is not a real algorithm, but a simplified sRGB IEC61966-2.1 formula)
• gray=0.2126*r+0.7152*g+0.0722*b
- Create a character list (length and content are customized)
- Map the gray value to characters and save the result with a string (note the corresponding picture
size, add line breaks)
- Export character painting to a .txt file
(d) Result
- Numpy exercises
- Consider a random 10×2 matrix representing cartesian coordinates, convert them to polar coordinates.
- Create a 2D array subclass such that Z[i, j] == Z[j, i].
- Consider 2 sets of points P0, P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])?
- Bilinear Interpolation
Please implement the bilinear interpolation algorithm using python. Check this for an introduction to bilinear interpolation.
Test samples:
A=
((110, 120, 130), (210, 220, 230),
3
(310, 320, 330))
BilinearInterpolation(A, (1, 1)) == 110 BilinearInterpolation(A, (2.5, 2.5)) == 275
- Cartesian product
Given an arbitrary number of vectors, build the cartesian product (every combinations of every item).e.g. [1,2,3],[4,5],[6,7]==>[[146][147][156][157][246][247][256][257][346][347][356] [3 5 7]]
- Extracting a subpart of an array
Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on agiven element (pad with a fill value when necessary) e.g.
In:- >> Z = np.random.randint(0, 10, (5, 5))
- >> shape = (4, 4)
- >> fill = 0
- >> position = (1,1)
- >> Z
[[3 6 8 5 9] [4 9 0 0 9] [6 1 4 0 8] [9 1 2 0 9] [4 1 7 5 0]] Out:
[[0 0 0 0] [0 3 6 8] [0 4 9 0] [0 6 1 4]]
- Matrix operations
Please implement following matrix (just 2D) operations without numpy:
• add
• subtract
• scalar multiply • multiply
• identity
• transpose
4
• inverse
Test samples:
In:
>> matrix_a = [[12, 10], [3, 9]]
>> matrix_b = [[3, 4], [7, 4]]
>> matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
>> matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
Out:
add(matrix_a, matrix_b) == [[15, 14], [10, 13]]
subtract(matrix_a, matrix_b) == [[9, 6], [-4, 5]]
scalar_multiply(matrix_b, 3) == [[9, 12], [21, 12]]
multiply(matrix_a, matrix_b) == [[106, 88], [72, 48]]
identity(3) == [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
transpose(matrix_c) == [[11, 21, 31, 41], [12, 22, 32, 42], [13, 23, 33, 43], [14, 24, 34, 44]] inverse(matrix_d) == [[0.2, 0.2, 0.0], [-0.2, 0.30000000000000004, 1.0], [0.2, -0.30000000000000004, 0.0]]
- Greatest common divisor
Find the greatest common divisor(gcd) of two integers. Test samples:• GCD(3, 5) = 1 • GCD(6, 3) = 3 • GCD(-2, 6) = 2 • GCD(0, 3) = 3
- Find all consecutive positive number sequences whose sum is N
e.g. 18+19…+22 = 9+10+…+16 = 100
Find all consecutive positive number sequences whose sum is 1000, and report your results.
- Password checking
A website requires the users to input username and password to register. Write a program to check the validity of password input by users. Following are the criteria for checking the password:
• At least 1 letter between [a-z]
• At least 1 number between [0-9]
• At least 1 letter between [A-Z]
• At least 1 character from [$#@]
• Minimum length of transaction password: 6 • Maximum length of transaction password: 125
Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma.
e.g.
If the following passwords are given as input to the program: ABd1234@1,a F1#,2w3E*,2We3345 Then, the output of the program should be: ABd1234@1