Yr12 Journal 1

In the first 3 weeks, we learned about using matplotlib to sketch different graphs, the Big-O and probabilities.

MatPlotLib

As I already know how to sketch graphs using matplotlib, my focus was on how to save the generated plot, how to clear the previous plot and what is a loglogplot.

Generate Data

As we need to generate different plots, I created a function that will return different name of the functions and the y-values by providing x-values:

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
def getFunc(i, x):
if i == 0:
return "y=log(x)", [log(a) for a in x]
if i == 1:
return "y=log2(x)", [log(a, 2) for a in x]
if i == 2:
return "y=xlog(x)", [a * log(a) for a in x]
if i == 3:
return "y=x", [a for a in x]
if i == 4:
return "y=x^2", [a ** 2 for a in x]
if i == 5:
return "y=x^3", [a ** 3 for a in x]
if i == 6:
return "y=2^x", [2 ** a for a in x]
if i == 7:
return "y=1", [1 for a in x]


def log(x, b=None):
if b is None:
b = math.e
try:
return math.log(x, b)
except:
return 0

Different values of ‘i’ stands for different maths functions

Save/Clear/LogLogPlot

Since I have the name of the function and the values, I can easily plot them and save multiple graphs at ones:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def plot(loglog=False):
for i in range(8):
xvals = range(0, 100)
f = getFunc(i, xvals)
yvals = f[1]
# create the plot
plt.clf()
# plt.axis([0, 100, 0, 100])
plt.title("An example graph of " + f[0])
plt.xlabel("x")
plt.ylabel("y")
if loglog:
plt.loglog(yvals, label=f[0])
else:
plt.plot(yvals, '-b', label=f[0])
plt.legend(loc='upper right')
# display the plot
# plt.show()
plt.savefig(f"plots/{'loglog_' if loglog else 'normal_'}graph_{i}.png")
  • Without calling plt.clf(), the previous graph will stay on the sketch and this is not what I want for this scenario, therefore this function is called eachtime before sketching a graph
  • If loglog is enabled in this function, it will generate a loglog graph otherwise just a normal graph.
  • To save a graph, it is easy just with one line of code: plt.savefig(path), and the path is depend on whether loglog is enabled or not and the ‘i’ value.

Big-O

The Big-O is wisely used in estimating the Time and Space Complexity for algorithms, it usually stands for the worst case proceessing time or the worst memory usage.
In these weeks we mainly studied on different sorting methods and different searching methods:

  • Destructive Selection Sort
  • Merge Sort
  • Linear Search
  • Binary Search
    At the same time we compared the complexity of each:

Probability

We learned about sample places, different events and expected values. And I will implement something relates to this topic in next week’s work.

Why we learning these

I think the reason we learning these topics is because if we wanna build an AI in the future we need to make high-efficiency algorithms, at the same time we need to illustrate graphs, last but not least probability takes a great role in AI area.