Skip to content

Commit

Permalink
Fixed matrix construction logic to create row vectors from 1D lists
Browse files Browse the repository at this point in the history
  • Loading branch information
jerela committed Nov 28, 2023
1 parent 6f6ddf2 commit 7a8cdd3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
9 changes: 7 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@

# TEST GAUSS-NEWTON ITERATION
h = Matrix([lambda a,x: pow(a,x)])
independents = Matrix([1, 2, 3])
y = Matrix([2, 4, 8])
independents = Matrix([1, 2, 3]).get_transpose()
y = Matrix([2, 4, 8]).get_transpose()
# let J be the Jacobian of h(x)
J = Matrix([lambda a,x: x*pow(a,x-1)])

Expand All @@ -141,3 +141,8 @@
print(centers)
assert(utils.equals_approx(centers,Matrix([[-0.6667, 1.0],[20.75, 0.0]]),precision = 1e-4))


# TEST SUBTRACTIVE CLUSTERING
density_points = Matrix([[1,2], [0.5,1.5], [0,1], [0,0.5], [0,0], [0,-0.5], [0,-1], [0.5,-1.5], [1,-2], [2,0], [2.5,-0.5], [3,-1], [3,-1.5], [3,-2], [3,-2.5], [3,-3], [2.5,-3.5], [2,-4]])
mountaintops = clustering.find_density_clusters(data=density_points, num_centers=2, beta = 0.25, sigma = 0.25)
print(mountaintops)
6 changes: 2 additions & 4 deletions mola/clustering.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from asyncio.windows_events import INFINITE
from json.encoder import INFINITY
from mola.matrix import Matrix
from mola.utils import zeros, get_mean, uniques, randoms, norm
from random import random
Expand Down Expand Up @@ -107,7 +105,7 @@ def find_k_means(data: Matrix, num_centers = 2, max_iterations = 100, distance_f
if closest_center[row] == i:
points_in_cluster.append(data.get_row(row))
if len(points_in_cluster) > 0:
centers[i,:] = get_mean(points_in_cluster).get_transpose()
centers[i,:] = get_mean(points_in_cluster)

# if the centers remained the same as in previous iteration, break out of the loop
if centers == previous_centers:
Expand Down Expand Up @@ -260,7 +258,7 @@ def find_density_clusters(data: Matrix, num_centers = 2, beta = 0.5, sigma = 0.5


# save cluster centers
c_subtractive[k,:] = data[peak_i,:].get_transpose()
c_subtractive[k,:] = data[peak_i,:]

# destruct mountain functions
for i in range(n_samples):
Expand Down
2 changes: 1 addition & 1 deletion mola/decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def power_method(A: Matrix) -> Matrix:
"""

# initialize the vector with random values using list comprehension
vector = Matrix([random() for x in range(A.get_height())])
vector = Matrix([random() for x in range(A.get_height())]).get_transpose()

# do the iteration to (hopefully) converge the vector towards its dominant eigenvector
for i in range(100):
Expand Down
16 changes: 6 additions & 10 deletions mola/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,11 @@ def __construct_from_lists(self,lists):
row = lists[j]
col.append(row)
self.data = col
# if the input parameter was not a nested list, assume it was a simple 1D list and construct a matrix with a single column
# if the input parameter was not a nested list, assume it was a simple 1D list and construct a Matrix that is actually a row vector
elif isinstance(lists,list):
self.n_rows = len(lists)
self.n_cols = 1
col = []
for j in range(self.n_rows):
row = lists[j]
col.append([row])
self.data = col
self.n_rows = 1
self.n_cols = len(lists)
self.data = [lists]

def __abs__(self):
"""
Expand Down Expand Up @@ -123,7 +119,7 @@ def __getitem__(self,idx):
# rows is slice and cols is int (several values from a single column are queried)
elif isinstance(rows,slice) and isinstance(cols,int):
#sliced_data = Matrix([r[cols] for r in self.data[rows]])
return Matrix([r[cols] for r in self.data[rows]])
return Matrix([r[cols] for r in self.data[rows]]).get_transpose()
# if both are slices (a submatrix is queried)
elif isinstance(rows,slice) and isinstance(cols,slice):
#newlist = [r[cols] for r in self.data[rows]]
Expand Down Expand Up @@ -327,7 +323,7 @@ def __sub__(self,other):
output = Matrix(self.n_rows,self.n_cols,0)
# first, ensure that the dimensions of the matrices match
if self.n_rows != other.n_rows or self.n_cols != other.n_cols:
raise Exception("Matrix dimensions must match for elementwise addition or subtraction!")
raise Exception("Matrix dimensions must match for elementwise addition or subtraction! Left side is " + str(self.n_rows) + "x" + str(self.n_cols) + ", right side is " + str(other.n_rows) + "x" + str(other.n_cols))
# if we passed the dimension check, we can subtract the matrices elementwise
for i in range(self.n_rows):
for j in range(self.n_cols):
Expand Down

0 comments on commit 7a8cdd3

Please sign in to comment.