有关海华AI比赛

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 .

3YLMx1.png

The specific steps:

import:

1
2
3
4
5
%matplotlib inline
import numpy as np
import csv #read the data
import matplotlib.pyplot as plt #plot the graph
import random
1
2
3
4
5
6
7
8
9
# Declare lists for storing trainset of population and profit
population = np.array([])
profit = np.array([])

# Load trainset from csv file, add the data to the lists
reader = csv.reader(open('./dataForCEO.csv','r'))
for row in reader:
population = np.append(population, float(row[0])) #append:add value into list
profit = np.append(profit, float(row[1]))

Plot the diagram

1
2
3
4
5
6
7
8
9
10
11
12
def plotData(x, y, t0=None, t1=None):
plt.figure(figsize=(6,4)) #plot the background
plt.scatter(x.tolist(), y.tolist(), marker='x') #plot the scatter graph
if (t0 != None) and (t1 != None):
tempx = np.linspace(5, 22.5, 50)
tempy = t0 + tempx * t1
plt.plot(tempx, tempy, color='red', linestyle='--') #find the trend
plt.xlabel('Population of City in 10000')
plt.ylabel('Profit in $10000')
plt.show()

plotData(population, profit)
1
2
def h(x, theta0, theta1):
return theta0 + theta1 * x

The formula:

1
2
3
4
def computeCost(x, y, theta0, theta1):
m = len(y)
J = 1/(2*m) * sum((h(x,theta0,theta1) - y) ** 2)
return J
1
2
3
4
5
6
def gradientDescent(x, y, theta0, theta1, alpha, iterTimes):
m = len(y)
temp0 = theta0 - alpha*(1/m) * sum((h(x, theta0, theta1) - y));
temp1 = theta1 - alpha*(1/m) * sum((h(x, theta0, theta1) - y) * x);
iterTimes = iterTimes + 1
return temp0, temp1, iterTimes
1
2
3
4
theta0 = random.uniform(-10,10)
theta1 = random.uniform(-10,10)
alpha = 0.001 #the smaller the alpha, the more accurate the result can achieve
iterTimes = 0 # times of iteration
1
2
3
4
5
6
7
8
9
10
for i in range(1000):  #repeat for 1000 within one click to make the process faster
theta0, theta1, iterTimes = gradientDescent(population, profit, theta0, theta1, alpha, iterTimes)
print("After %d iterations:"%(iterTimes))
print("h(x) = %f + %fx"%((theta0, theta1)))
print("Cost is %f now."%(computeCost(population, profit, theta0, theta1)))
plotData(population, profit, theta0, theta1)

#Out(the first time):After 0 iterations: (times)
# h(x) = -9.955209 + 2.422265x (relationship)
# Cost is 23.558595 now. (difference to the real value)

3Yjdz9.png

final result: ℎ(𝑥) = −3.895781 + 1.193034𝑥

the cost: 𝐶𝑜𝑠𝑡 = 4.476971 (the difference between the real value and the predicted value)


Digital image processing

3YvVyR.png

3YvG6A.png

Some simple examples of digital image processing

1
2
3
4
5
6
# import packages
%matplotlib inline
import numpy as np
import cv2 #read the picture
import matplotlib
import matplotlib.pyplot as plt
1
2
3
4
pictureRaw = cv2.imread("/picture.jpg")
print(type(pictureRaw))

#Out:<class 'numpy.ndarray'>
1
2
3
print(pictureRaw.shape)

#Out:(995, 1079, 3) (the graph is 995x1079 and has 3 color channels)
1
plt.imshow(cv2.cvtColor(pictureRaw, cv2.COLOR_BGR2RGB))#plot the picture

3YxvV0.png

1. Grayscale image:

1
2
3
4
pictureRaw2 = cv2.imread("/picture.jpg", cv2.IMREAD_GRAYSCALE)
print(pictureRaw2.shape)

#Out:(995, 1079) #The picture only contains black and white so the shape is 2-dimensional
1
plt.imshow(cv2.cvtColor(pictureRaw2, cv2.COLOR_GRAY2RGB))

3tptgg.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fig1 = pictureRaw # This line is a WRONG operation. 
# If you directly assign an image to a new variable like above, you will unable to modify them seperately.

# You should create a copy and assign it to fig1 variable, like the following line.
fig1 = pictureRaw.copy()

# Slicing fig1 and assign the sliced part to fig2.
fig2 = fig1[ 200:900,350:700, :]
# Slicing fig2 and assign the sliced part to fig3.
fig3 = fig2[50:150, 140:230, :]

# Draw yellow rectangles in copies of fig1 and fig2 to show the part you sliced.
fig1 = cv2.rectangle(fig1.copy(),(350,200),(700,900), color=(0,255,255),thickness = 2)
fig2 = cv2.rectangle(fig2.copy(),(140,50),(230,140), color=(0,255,255),thickness = 1)

# Show the images.
plt.figure(figsize=(15, 10))
plt.subplot(1,3,1) #subplot:multi-graph in one page
plt.imshow(cv2.cvtColor(fig1, cv2.COLOR_BGR2RGB))
plt.subplot(1,3,2)
plt.imshow(cv2.cvtColor(fig2, cv2.COLOR_BGR2RGB))
plt.subplot(1,3,3)
plt.imshow(cv2.cvtColor(fig3, cv2.COLOR_BGR2RGB))

3t90Qe.png

2. Print the matrix of image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
print(fig3.shape)

print("\n Blue channel:")
print(fig3[:, :, 0])
print("\n Green channel:")
print(fig3[:, :, 1])
print("\n Red channel:")
print(fig3[:, :, 2])

#Out:(100, 90, 3)

# Blue channel:
#[[ 51 46 49 ... 47 48 45]
# [ 61 55 52 ... 47 51 50]
# [ 64 63 61 ... 47 52 50]
# ...
# [172 171 171 ... 238 238 232]
# [176 175 170 ... 242 237 228]
#[170 175 167 ... 239 238 233]]
#
# Green channel:
#[[ 45 40 44 ... 41 42 39]
# [ 55 50 46 ... 43 47 46]
# [ 57 57 55 ... 43 48 46]
# ...
# [170 169 170 ... 230 230 224]
# [174 173 169 ... 231 226 217]
# [168 173 166 ... 225 224 219]]
#
# Red channel:
#[[ 62 59 65 ... 46 47 44]
# [ 74 71 69 ... 49 53 52]
# [ 78 80 80 ... 49 54 52]
# ...
# [176 175 174 ... 231 231 225]
# [180 179 173 ... 233 228 219]
# [174 179 170 ... 226 225 220]]

3. Binary image

1
2
3
4
5
6
# import image
pictureRaw3 = cv2.imread("/picture.jpg")
# create a grayscale copy
picGray0 = cv2.cvtColor(picyureRaw3.copy(), cv2.COLOR_BGR2GRAY) #turn gray
# set threshold
threshold = 127 #the middle point to divide black or white

traditional way:

1
2
3
4
5
6
7
8
9
10
# Common loop
picGray1 = cv2.cvtColor(pictureRaw3.copy(), cv2.COLOR_BGR2GRAY)
height = picGray1.shape[0]
width = picGray1.shape[1]
for r in range(0, height):
for c in range(0, width):
if picGray1[r][c] > threshold:
picGray1[r][c] = 255 #if the value>127 it's black
else:
picGray1[r][c] = 0 #vice versa

faster way:

1
2
3
4
# Vectorization: A faster method
picGray2 = cv2.cvtColor(pictureRaw3.copy(), cv2.COLOR_BGR2GRAY)
picGray2[picGray2>threshold] = 255
picGray2[picGray2<=threshold] = 0

plot the result:

1
2
3
4
5
6
7
8
9
plt.figure(figsize=(10, 6))
plt.subplot(2,2,1)
plt.imshow(cv2.cvtColor(garbageRaw, cv2.COLOR_BGR2RGB))
plt.subplot(2,2,2)
plt.imshow(cv2.cvtColor(garGray0, cv2.COLOR_GRAY2RGB))
plt.subplot(2,2,3) #the traditional way
plt.imshow(cv2.cvtColor(garGray1, cv2.COLOR_GRAY2RGB))
plt.subplot(2,2,4) #the faster way
plt.imshow(cv2.cvtColor(garGray2, cv2.COLOR_GRAY2RGB))

3tmNx1.png

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.

3tn9JJ.gif

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
raw = cv2.imread("./images/NanjingShizhangJiangdaqiao.jpg", cv2.IMREAD_COLOR)

gray = cv2.cvtColor(raw, cv2.COLOR_BGR2GRAY)

# Laplace Operator
laplace = np.array(
[[0, -1, 0],
[-1, 4, -1],
[0, -1, 0]]
)
# convolution
aft1 = cv2.filter2D(gray, -1, laplace)
aft2 = cv2.filter2D(gray, -1, laplace)
aft3 = cv2.filter2D(gray, -1, laplace)

# binarize, different thresholds can get different results
def binarize(img, threshold):
b = img.copy()
b[b>threshold] = 255
b[b<=threshold] = 0
return b

aft1 = binarize(aft1, 5)
aft2 = binarize(aft2, 10)
aft3 = binarize(aft3, 25)

plt.figure(figsize=(12, 9))
plt.subplot(2,2,1)
plt.imshow(cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB))
plt.subplot(2,2,2)
plt.imshow(cv2.cvtColor(aft1, cv2.COLOR_GRAY2RGB))
plt.subplot(2,2,3)
plt.imshow(cv2.cvtColor(aft2, cv2.COLOR_GRAY2RGB))
plt.subplot(2,2,4)
plt.imshow(cv2.cvtColor(aft3, cv2.COLOR_GRAY2RGB))

3tn3OP.png