-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpweek3.py
More file actions
88 lines (68 loc) · 2.65 KB
/
Copy pathpweek3.py
File metadata and controls
88 lines (68 loc) · 2.65 KB
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
# Ameen Rahman
import numpy as np
def Normalize(A):
# grab the input and convert everything to floats
inputMatrix = A
floatMatrix = inputMatrix.astype(float)
matrixShape = floatMatrix.shape
numRows = matrixShape[0]
# setup empty matrix for normalized values
numCols = matrixShape[1]
normalizedMatrix = np.zeros((numRows, numCols), dtype=float)
for columnIndex in range(numCols):
# pull out each column and sum it up
currentColumn = floatMatrix[:, columnIndex]
columnSum = np.sum(currentColumn)
if columnSum != 0:
# divide by sum so column adds to 1
normalizedColumn = currentColumn / columnSum
for rowIndex in range(numRows):
normalizedMatrix[rowIndex,
columnIndex] = normalizedColumn[rowIndex]
else:
# keep zeros as is if column is empty
for rowIndex in range(numRows):
normalizedMatrix[rowIndex,
columnIndex] = currentColumn[rowIndex]
return normalizedMatrix
def PageRank(L, iter=100):
# figure out how big the matrix is
matrixShape = L.shape
matrixSize = matrixShape[0]
# start everyone with equal rank
initialValue = 1.0 / matrixSize
rankVector = np.full((matrixSize, 1), initialValue, dtype=float)
rankVectorList = [rankVector]
# keep multiplying to spread the rank around
iterationCounter = 1
while iterationCounter < iter:
nextRankVector = np.dot(L, rankVector)
rankVectorList.append(nextRankVector)
# update for next round
rankVector = nextRankVector
iterationCounter = iterationCounter + 1
return rankVectorList
def SearchResults(r):
# flatten to 1D and make tuple pairs
flattenedRankVector = r.flatten()
numElements = len(flattenedRankVector)
unsortedTupleList = []
# pair each index with its rank value
for rowIndex in range(numElements):
currentValue = flattenedRankVector[rowIndex]
floatValue = float(currentValue)
tuplePair = (rowIndex, floatValue)
unsortedTupleList.append(tuplePair)
# sort indices by rank highest to lowest
sortedIndices = np.argsort(flattenedRankVector)
reversedIndices = sortedIndices[::-1]
sortedTupleList = []
# build final sorted list with proper types
for idx in reversedIndices:
integerIndex = int(idx)
valueAtIndex = flattenedRankVector[idx]
floatValue = float(valueAtIndex)
# pack it up and ship it
sortedTuple = (integerIndex, floatValue)
sortedTupleList.append(sortedTuple)
return sortedTupleList