-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcit_assignment_07_03.py
184 lines (140 loc) · 4.18 KB
/
cit_assignment_07_03.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
1. create a 2-D array and grab 3 numbers in the second column
"""
from timeit import repeat
import numpy as np
def grab_three_numbers(array, col):
"""
Grab three numbers from the second column of a 2-D array
"""
return array[:, col]
array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(grab_three_numbers(array, 1))
"""
2. create a list called company_data, using a function, a company needs to
know the total average, the variance and how spread out the data is
"""
company_data = np.array([45, 52, 67, 90, 23, 45, 67, 89, 90, 45])
def total_average(array):
"""
Calculate the total average of a list
"""
return np.mean(array)
def variance(array):
"""
Calculate the variance of a list
"""
return np.var(array)
def spread(array):
"""
Calculate the spread of a list
"""
return np.std(array)
print(f"The total average is: {total_average(company_data)}")
print(f"The variance is: {variance(company_data)}")
print(f"The spread is: {spread(company_data)}")
"""
3. create 2 arrays showing the x axis from 20 to 100 and the y axis from 120 to 200
(use matplotlib to visualize arrays x and y)
"""
import matplotlib.pyplot as plt
x = np.linspace(20, 100, num=10)
y = np.linspace(120, 200, num=10)
def visualize_arrays(x, y):
"""
Visualize two arrays
"""
plt.bar(x, y, align="edge")
# show x and y legends
plt.xlabel("X Array")
plt.ylabel("Y Array")
# show the plot
plt.show()
visualize_arrays(x, y)
"""
4. Create a 2-d array and multiple both columns.
"""
two_d_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
def multiply_both_columns(array):
"""
Multiply both columns of a 2-D array
"""
return array[:, 0] * array[:, 1]
print(multiply_both_columns(two_d_array))
"""
5.Create any sorting algorithm and show us the animation using matplotlib.
"""
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from mpl_toolkits.mplot3d import axes3d
import matplotlib as mp
import numpy as np
import random
# quicksort function
def quicksort(array, start, end):
if start >= end:
return
x = array[start]
j = start
for i in range(start + 1, end + 1):
if array[i] <= x:
j += 1
array[j], array[i] = array[i], array[j]
yield array
array[start], array[j]= array[j], array[start]
yield array
# yield from statement used to yield
# the array after dividing
yield from quicksort(array, start, j-1)
yield from quicksort(array, j + 1, end)
# function to plot bars
def visualize_quick_sort():
# for random unique values
n = int(input("enter array size\n"))
array = [i for i in range(1, n + 1)]
random.shuffle(array)
datasetName ='Random'
# generator object returned by the function
generator = quicksort(array, 0, n-1)
algoName = 'Quick Sort'
# style of the chart
plt.style.use('fivethirtyeight')
# set colors of the bars
data_normalizer = mp.colors.Normalize()
color_map = mp.colors.LinearSegmentedColormap(
"my_map",
{
"red": [(0, 1.0, 1.0),
(1.0, .5, .5)],
"green": [(0, 0.5, 0.5),
(1.0, 0, 0)],
"blue": [(0, 0.50, 0.5),
(1.0, 0, 0)]
}
)
fig, ax = plt.subplots()
# bar container
bar_rects = ax.bar(range(len(array)), array, align ="edge",
color = color_map(data_normalizer(range(n))))
# setting the limits of x and y axes
ax.set_xlim(0, len(array))
ax.set_ylim(0, int(1.1 * len(array)))
ax.set_title("ALGORITHM : "+ algoName + "\n" + "DATA SET : " +
datasetName, fontdict = {'fontsize': 13, 'fontweight':
'medium', 'color' : '#E4365D'})
# the text to be shown on the upper left indicating the number of iterations
# transform indicates the position with relevance to the axes coordinates.
text = ax.text(0.01, 0.95, "", transform = ax.transAxes, color = "#E4365D")
iteration = [0]
def animate(A, rects, iteration):
for rect, val in zip(rects, A):
# setting the size of each bar equal to the value of the elements
rect.set_height(val)
iteration[0] += 1
text.set_text("iterations : {}".format(iteration[0]))
# call animate function repeatedly
anim = FuncAnimation(fig, func = animate,
fargs = (bar_rects, iteration), frames = generator, interval = 50,
repeat = False)
plt.show()
visualize_quick_sort()