def init_theta(X, Y):
    """
    Initialization theta vector
    :param X:
    :param Y:
    :return: Theta vector + Matrix for any class
    """
    # Split X:
    X1 = Matrix.from_list([X[i] for i in range(X.numrows) if Y[i][0] == 1])
    X2 = Matrix.from_list([X[i] for i in range(X.numrows) if Y[i][0] == 0])

    # A and B are avg data point of X1 and X2:
    A = [sum([X1[i][1] for i in range(X1.numrows)])/X1.numrows, sum([X1[i][2] for i in range(X1.numrows)])/X1.numrows]
    B = [sum([X2[i][1] for i in range(X2.numrows)])/X2.numrows, sum([X2[i][2] for i in range(X2.numrows)])/X2.numrows]

    # Vector AB:
    AB = [B[0] - A[0], B[1] - A[1]]
    # I is midpoint between A and B:
    I = [(A[0] + B[0])/2, (A[1] + B[1])/2]

    # Get equation of AB's bisection:
    theta1 = np.array([[-(AB[0]*I[0] + AB[1]*I[1])], [AB[0]], [AB[1]]])

    # Scale theta:
    div = theta1[0][0]
    for i in range(len(theta1)):
        if abs(theta1[i][0]) > abs(div):
            div = theta1[i][0]

    for i in range(len(theta1)):
        theta1[i][0] /= div

    # Return theta and matrix of any class
    return [Matrix.from_list(theta1), X1, X2]
def get_matrix(data_input):
    """
    Init X and Y matrix
    :param data_input: input file path
    :return: Matrix X and Y
    """
    # Read data to dataframe df:
    df = (pd.read_csv(data_input)).values
    # Get X and Y:
    X = Matrix.from_list([df[i][0:2] for i in range(len(df))])
    Y = Matrix.from_list([df[i][2:] for i in range(len(df))])

    # Append 1 into any row of X:
    X = Matrix.from_list([np.insert(X[i], 0, (1,)) for i in range(len(df))])
    # Return:
    return [X, Y]
예제 #3
0
def get_matrix(var):
    """
    Lay ma tran theta, X va Y
    :param var: Ma tran tham so dau vao
    :return: list 3 ma tran: theta, X va Y
    """
    # Y la cot cuoi cung cua var, con lai thuoc X
    X = [var[i] for i in range(len(var) - 1)]
    Y = var[len(var) - 1]

    # Them value 1 vao dau moi dong cua X
    X.insert(0, [1 for i in range(len(var[0]))])
    # Tao ma tran X va Y:
    mX = Matrix.from_list(X)
    mY = Matrix.from_list([Y])

    # Tra ve theta, X va Y:
    return [((mX * mX.trans()).inv()) * mX * mY.trans(), mX, mY]
def sigmoid_function(theta, X):
    """
    Cacl sigmoid value
    :param theta:
    :param X:
    :return:
    """
    z = Matrix.from_list(X) * (-theta)
    return 1.0 / (1 + np.exp(z[0][0]))
def update_equation(X, Y, theta, alpha):
    """
    Update Decision Boundary
    :param X:
    :param Y:
    :param theta:
    :param alpha:
    :return: New theta vector
    """
    return theta - Matrix.from_list([[alpha * cost_function(X, Y, theta)] for i in range(theta.numrows)])
예제 #6
0
    def __init__(self, key: list = None):
        if not key:
            key = Matrix.identity(3)

        self.key_matrix = Matrix.from_list(key)

        try:
            determinant_modulus_inverse = pow(round(self.key_matrix.det()), -1,
                                              26)
            self.key_matrix_inverse = (determinant_modulus_inverse *
                                       self.key_matrix.adjoint()) % 26
        except:
            raise Exception("Key matrix have to be modular inversible")
예제 #7
0
    def encrypt(self, plaintext: str) -> str:
        """
        Encrypt a given string, returning an encrypted string.
        Overrides StringCipher.encrypt()
        """
        no_space_plaintext = plaintext.replace(" ", "")

        if not no_space_plaintext.isalpha():
            raise Exception(
                "Hill cipher only accept alphabets with or without spaces as plaintext"
            )

        lowercase_plaintext = no_space_plaintext.lower()

        plaintext_mod_26_list = [
            ord(char) - ord('a') for char in lowercase_plaintext
        ]

        ciphertext_mod_26_list = []

        block = self.key_matrix.numcols

        offset = 0
        while offset < len(plaintext_mod_26_list):
            plaintext_slice = None
            if len(plaintext_mod_26_list) - offset < block:
                padding = [
                    0
                    for x in range(len(plaintext_mod_26_list), offset + block)
                ]
                plaintext_slice = plaintext_mod_26_list[
                    offset:len(plaintext_mod_26_list)] + padding

            else:
                plaintext_slice = plaintext_mod_26_list[offset:offset + block]

            plaintext_matrix_block = Matrix.from_list([[x]
                                                       for x in plaintext_slice
                                                       ])

            ciphertext_matrix_block = self.key_matrix * plaintext_matrix_block

            for x in ciphertext_matrix_block.elements():
                ciphertext_mod_26_list.append(x % 26)

            offset += block

        ciphertext = ''.join(
            [chr(x + ord('a')) for x in ciphertext_mod_26_list]).upper()

        return ciphertext
예제 #8
0
    def infer(self, data_in):
        """Push data_in through network and give results."""

        l3 = Matrix.from_list([data_in]) * self.l1 + self.b1
        # l3 = NeuralNetwork._logistic(l3)

        l3 = l3 * self.l2 + self.b2
        # l3 = NeuralNetwork._logistic(l3)

        e = [math.exp(elem) for elem in l3[0]]
        s = sum(e)

        odds = [elem / s for elem in e]

        return self.argmax(odds)
def cost_function(X, Y, theta):
    """
    Cacl avg cost value
    :param X:
    :param Y:
    :param theta:
    :return: Cost value
    """
    m = X.numrows  # Data length
    # Cacl total cost:
    cost = ([(-Y[i][0] * np.log(sigmoid_function(theta, [X[i]]))
              - (1 - Y[i][0]) * np.log(1 - sigmoid_function(theta, [X[i]])))
             * Matrix.from_list([[X[i][0]]]) for i in range(m)])

    # Return avg cost:
    return -sum([cost[i][0][0] for i in range(len(cost))])/m
예제 #10
0
    def decrypt(self, ciphertext: str) -> str:
        """
        Decrypt a given string, returning a plaintext string.
        Overrides StringCipher.decrypt()
        """

        if not ciphertext.isalpha():
            raise Exception("Hill cipher only accept alphabets as ciphertext")

        lowercase_ciphertext = ciphertext.lower()

        ciphertext_mod_26_list = [
            ord(char) - ord('a') for char in lowercase_ciphertext
        ]

        plaintext_mod_26_list = []

        block = self.key_matrix.numcols

        offset = 0
        while offset < len(ciphertext_mod_26_list):
            ciphertext_slice = None

            if len(ciphertext_mod_26_list) - offset < block:
                padding = [
                    0
                    for x in range(len(ciphertext_mod_26_list), offset + block)
                ]
                ciphertext_slice = ciphertext_mod_26_list[
                    offset:len(ciphertext_mod_26_list)] + padding
            else:
                ciphertext_slice = ciphertext_mod_26_list[offset:offset +
                                                          block]

            ciphertext_matrix_block = Matrix.from_list(
                [[x] for x in ciphertext_slice])

            plaintext_matrix_block = self.key_matrix_inverse * ciphertext_matrix_block

            for x in plaintext_matrix_block.elements():
                plaintext_mod_26_list.append(round(x) % 26)

            offset += block

        plaintext = ''.join([chr(x + ord('a')) for x in plaintext_mod_26_list])

        return plaintext
def update_equation(X, Y, theta, alpha, _lambda):
    """
    Update Decision Boundary
    :param X:
    :param Y:
    :param theta:
    :param alpha:
    :return: New theta vector
    """
    temp = theta[0][0]  # Save theta0 value

    # Update theta(included theta0):
    theta = theta - Matrix.from_list(
        [[alpha * cost_function(X, Y, theta, _lambda)]
         for i in range(theta.numrows)])

    # Update theta0 by special way:
    theta[0][0] = temp - alpha * cost_function(X, Y, theta, 0)

    return theta
예제 #12
0
파일: infopy.py 프로젝트: a-robu/infopy
def colvec(l):
    return Matrix.from_list([l]).trans()
예제 #13
0
파일: infopy.py 프로젝트: a-robu/infopy
 def test_from_matrix(self):
     assert P(Matrix.from_list([[0.4], [0.2], [0.1]])) == P([0.4, 0.2, 0.1])
예제 #14
0
from pymatrix import Matrix
import pandas as pd
import numpy as np
from Regularization import Hx, get_matrix

if __name__ == '__main__':
    df1 = pd.read_csv("result.txt").values
    df2 = (pd.read_csv("ex2data2.txt")).values

    temp = get_matrix("ex2data2.txt")
    X = temp[0]
    Y = temp[1]
    theta = Matrix.from_list(df1)
    for i in range(len(df1)):
        np.append(df1[i], 1 - Hx(theta, X, 6))
    print df1