Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. import random
  2.  
  3. class Matrix:
  4.     def __init__(self, n, m):
  5.         self.rows = n
  6.         self.columns = m
  7.         self.size = n*m
  8.         self.data = [[0] * m for i in range(n)]
  9.  
  10.     # Fill every slot in the matrix with the value of n.
  11.     def fill_matrix(self, n):
  12.         for i in range(self.rows):
  13.             for j in range(self.columns):
  14.                 self.data[i][j] = n
  15.  
  16.     # Fill every slot in the matrix with a radnom value in a given range, between a and b.
  17.     def fill_matrix_random(self,a,b):
  18.         for i in range(self.rows):
  19.             for j in range(self.columns):
  20.                 self.data[i][j] = random.randint(a,b)
  21.  
  22.     # Fill vector with array
  23.     def fill_vector(self, array):
  24.         for i in range(self.rows):
  25.             self.data[i][0] = array[i]
  26.  
  27.     # Print the matrix in a neat way in the console.
  28.     def print(self):
  29.         for i in range(self.rows):
  30.             for j in range(self.columns):
  31.                 print(self.data[i][j], end=' ')
  32.             print()
  33.  
  34.     # add or subtract. input is 1 or -1 for additon or subtraction.
  35.     def sum(self, a_s, M):
  36.         o = Matrix(self.rows, self.columns)
  37.         for i in range(self.rows):
  38.             for j in range(self.columns):
  39.                 o.data[i][j] = self.data[i][j] + (M.data[i][j]*a_s)
  40.         return o
  41.  
  42.     # Right multiplyes the Matrix M with the object the method is used with.
  43.     def multiply(self, M):
  44.         o = Matrix(self.rows, M.columns)
  45.         for i in range(self.rows):
  46.             for j in range(M.columns):
  47.                 sum = 0
  48.                 for k in range(M.rows):
  49.                     sum = sum + self.data[i][k] * M.data[k][j]
  50.                 o.data[i][j] = sum
  51.         return o
  52.  
  53.     def square(self):
  54.         o = Matrix(self.rows, self.columns)
  55.         for i in range(self.rows):
  56.             for j in range(self.columns):
  57.                 o.data[i][j] = (self.data[i][j])**2
  58.         return o
  59.  
  60.     # Transpose matrix; mirror all values about the leading diagonal
  61.     def transpose(self):
  62.         o = Matrix(self.rows, self.columns)
  63.         for i in range(self.rows):
  64.             for j in range(self.columns):
  65.                 o.data[i][j] = self.data[j][i]
  66.         return o
  67.  
  68.  
  69. import Matrix
  70.  
  71.  
  72. class Data:
  73.     def __init__(self):
  74.         self.number_inputs = 0
  75.         self.input_vector = None
  76.  
  77.     def set_input(self, array):
  78.         self.number_inputs = len(array)
  79.         self.input_vector = Matrix.Matrix(len(array), 1)
  80.         self.input_vector.fill_vector(array)
  81.  
  82.  
  83. class TrainingData(Data):
  84.  
  85.     def set_output(self, array):
  86.         self.number_outputs = len(array)
  87.         self.output_vector = Matrix.Matrix(len(array), 1)
  88.         self.output_vector.fill_vector(array)
  89.  
  90.     def calculate_cost(self, prediction):
  91.         m = Matrix.Matrix(1,1)
  92.         m = self.output_vector.sum(-1, prediction).square
  93.         return m
  94.  
  95.  
  96. p = TrainingData()
  97. p.set_input([1,1,1])
  98. p.set_output([2,2,2])
  99.  
  100. pred = Matrix.Matrix(3,1)
  101. pred.fill_vector([1.5,1.5,1.5])
  102.  
  103. p.calculate_cost(pred).print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement