Something about the machine learning
(code by karbon)
Introduction
Learning with supervision:
Input and output are known. The machine finds the relation between the input and the output, then predicts what the next input would be. e.g. classification, regression
Learning without supervision:
The machine finds the structure from the data. e.g. cluster(the difference between classification and cluster is: classification needs a definition while cluster requires the machine to find the features themselves. )
An example of Linear regression with one variable
use the method of Gradient Descent
Imagine you are the CEO of a restaurant chain and you are considering opening a store in a new city. Suppose that your group’s data analysis department finds that there is a strong correlation between the amount of profit made in a city and the population of that city, so you get data on the amount of profit made in the cities where you have set up shop and the population of those cities .
The specific steps:
import:
1 | %matplotlib inline |
1 | # Declare lists for storing trainset of population and profit |
Plot the diagram
1 | def plotData(x, y, t0=None, t1=None): |
1 | def h(x, theta0, theta1): |
The formula:
1 | def computeCost(x, y, theta0, theta1): |
1 | def gradientDescent(x, y, theta0, theta1, alpha, iterTimes): |
1 | theta0 = random.uniform(-10,10) |
1 | for i in range(1000): #repeat for 1000 within one click to make the process faster |
final result: ℎ(𝑥) = −3.895781 + 1.193034𝑥
the cost: 𝐶𝑜𝑠𝑡 = 4.476971 (the difference between the real value and the predicted value)
Digital image processing
Some simple examples of digital image processing
1 | # import packages |
1 | pictureRaw = cv2.imread("/picture.jpg") |
1 | print(pictureRaw.shape) |
1 | plt.imshow(cv2.cvtColor(pictureRaw, cv2.COLOR_BGR2RGB))#plot the picture |
1. Grayscale image:
1 | pictureRaw2 = cv2.imread("/picture.jpg", cv2.IMREAD_GRAYSCALE) |
1 | plt.imshow(cv2.cvtColor(pictureRaw2, cv2.COLOR_GRAY2RGB)) |
1 | fig1 = pictureRaw # This line is a WRONG operation. |
2. Print the matrix of image
1 | print(fig3.shape) |
3. Binary image
1 | # import image |
traditional way:
1 | # Common loop |
faster way:
1 | # Vectorization: A faster method |
plot the result:
1 | plt.figure(figsize=(10, 6)) |
4. Edge detection
One implementation of edge detection is through convolution.
Convolution is a mathematical operation. The following is an example of convolution in 2D image.
Convolution A∗BA*B can be described as the following gif image.
example:
1 | raw = cv2.imread("./images/NanjingShizhangJiangdaqiao.jpg", cv2.IMREAD_COLOR) |