This is the link to this lecture’s Colab notebook
This notebook has its setup cells merged to simplify the initial run process!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np
np.random.seed(123)
N = 4 # set matrix and vector dimension
M = np.random.randint(5, size = (N, N))
V = np.random.randint(5, size = N)
P = np.matmul(M, V)
# Print the matrices
print(f"Matrix M:\n {M}")
print(f"Vector V:\n {V}")
print(f"Dot Product P:\n {P}")
mspark = sc.parallelize(M)
print(f"Parallelize M inside Spark Context:\n {mspark.take(4)}")
mspark = sc.parallelize(M).zipWithIndex()
print(f"Parallelize M inside Spark Context with Index:\n {mspark.take(4)}")
mspark = (sc.parallelize(M).zipWithIndex().map(lambda item: item[0].dot(V)))
print(f"MapReduce Dot Product:\n {np.array(mspark.take(4))}")
print(f"Serial Dot Product P for comparison purposes:\n {P}")
parallelize into RDD objects on the cluster. mspark and perform a dot product between the element and Vector V.In this scenario, V no longer fits into memory and we also need to parallelize V in Spark.
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
import numpy as np
np.random.seed(123)
N = 8
M = np.random.randint(5, size = (N, N))
V = np.random.randint(5, size = N)
P = np.matmul(M, V)
print(f"Matrix M:\n {M}")
print(f"Vector V:\n {V}")
print(f"Dot Product P:\n {P}")
mspark = sc.parallelize(M).zipWithIndex().map(lambda item: (item[1],item[0].tolist()))
print(f"Parallelize M inside Spark Context with Index: {mspark.collect()}")
vspark = sc.parallelize(V).zipWithIndex().map(lambda item: (item[1],item[0].tolist()))
print(f"Parallelize V inside Spark Context with Index: {vspark.collect()}")
def getPos(t):
res = []
for idx,x in np.ndenumerate(t[1]):
res.append((idx[0], (t[0], x.item()))) # (row, (column, cell value))
return res
def arrayMul(t):
res = []
vectorVal = None
for x in t[1]:
if type(x) != tuple:
vectorVal = x
for x in t[1]:
if isinstance(x, tuple):
row_index = x[0]
matrix_value = x[1]
res.append((row_index, matrix_value * vectorVal))
return res
pspark = (mspark.flatMap(getPos)
.union(vspark)
.groupByKey()
.mapValues(list)
.flatMap(arrayMul)
.reduceByKey(lambda a, b: a + b))
print(f"MapReduce Dot Product: {pspark.collect()}")
print(f"Serial Dot Product P for comparison purposes:\n {P}")
map is so that we flip the indices and use them as keysgetPos union,groupBy,list type instead of Spark’s iterables, groupBy is a key/value pair: reduceByKey.
1
2
!wget https://files.grouplens.org/datasets/movielens/ml-32m.zip
!unzip ml-32m.zip
1
!ls -lh ml-32m/
1
2
3
ratings = sc.textFile(working_dir + "/ml-32m/ratings.csv")
print(ratings.take(5))
ratings.cache()
1
2
%%time
ratings.count()
CPU times: user 252 ms, sys: 28.2 ms, total: 280 ms
Wall time: 1min 40s
32000205
1
2
%%time
ratings.count()
CPU times: user 147 ms, sys: 19.8 ms, total: 167 ms
Wall time: 58.4 s
32000205
1
2
%%time
ratings.count()
CPU times: user 147 ms, sys: 19.8 ms, total: 167 ms
Wall time: 58.4 s
32000205