Created
June 25, 2021 04:44
-
-
Save reginaldojunior/6834ddda8efc68f9ae6c57d76c4459fb to your computer and use it in GitHub Desktop.
matrix.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import numpy as np | |
import math | |
class Matrix(): | |
rows = 0 | |
cols = 0 | |
data = [] | |
def create_matrix(self, rows, cols): | |
self.data = [] | |
self.rows = rows | |
self.cols = cols | |
for i in range(rows): | |
arr = [] | |
for j in range(cols): | |
arr.append(round(random.uniform(0.1, 10.0), 4)) | |
self.data.append(arr) | |
return self | |
def array_to_matrix(self, array): | |
self.data = [] | |
self.rows = len(array) | |
self.cols = 1 | |
for i in range(len(array)): | |
arr = [] | |
for j in range(1): | |
arr.append(array[i]) | |
self.data.append(arr) | |
if len(self.data) > 1: | |
self.data = self.data[0] | |
return self | |
def add(self, A, B, modify = False): | |
new_matrix = [] | |
if modify is True: | |
A.data = list([A.data]) | |
for i in range(B.rows): | |
arr = [] | |
for j in range(B.cols): | |
sum = A.data[i][j] + B.data[i][j] | |
arr.append(sum) | |
new_matrix.append(arr) | |
return new_matrix | |
def add_output(self, A, B, modify = False): | |
new_matrix = [] | |
if modify is True: | |
A.data = list([A.data]) | |
for i in range(1): | |
arr = [] | |
for j in range(1): | |
sum = A.data[i][j] + B.data[i][j] | |
arr.append(sum) | |
new_matrix.append(arr) | |
return new_matrix | |
def get_linha(self, mat, n): | |
return [i for i in mat.data[n]] | |
def get_coluna(self, mat, n): | |
return [i[n] for i in mat.data] | |
def multiply(self, A, B): | |
self.data = list(np.dot(A.data, B.data)) | |
return self | |
def sigmoid(self, x): | |
return 1 / (1 + math.exp(-x)) | |
def function_activate(self, A): | |
matrix = [] | |
for i in range(A.rows): | |
arr = [] | |
for j in range(A.cols): | |
arr.append(self.sigmoid(A.data[i][j])) | |
matrix.append(arr) | |
return matrix | |
# a = Matrix(2, 2) | |
# b = Matrix(2, 2) | |
# print(a.data) | |
# print(b.data) | |
# print(a.add(a, b)) | |
# # a.randomize() | |
# print(a.multiply(a, b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment