def precision_calc(resample: np.matrix): """ Calculate precision score from matrix. Assume the first column to be y_true and the second column to be y_pred Args: resample: matrix with 2 columns [y_true, y_pred] Returns: precision score: float """ return precision_score(resample.tolist()[0], resample.tolist()[1])
def multiply_vecmat(vec: vec3d, mat: np.matrix) -> vec3d: if mat.shape[0] == 1: mat=mat.tolist()[0] else: mat=mat.tolist() return vec3d( vec.x*mat[0][0]+vec.y*mat[1][0]+vec.z*mat[2][0]+vec.w*mat[3][0], vec.x*mat[0][1]+vec.y*mat[1][1]+vec.z*mat[2][1]+vec.w*mat[3][1], vec.x*mat[0][2]+vec.y*mat[1][2]+vec.z*mat[2][2]+vec.w*mat[3][2], vec.x*mat[0][3]+vec.y*mat[1][3]+vec.z*mat[2][3]+vec.w*mat[3][3])
def get_r_squared(model: Linear_Regression, X: np.matrix, y: np.matrix) -> float: nominator: float = 0 denominator: float = 0 y_list: List[List[float]] = y.tolist() y_avg: float = np.average(y_list) for idx, x in enumerate(X.tolist()): nominator += (model.fit(x[:4]) - y_list[idx][0]) ** 2 denominator += (y_avg - y_list[idx][0]) ** 2 return 1 - (nominator/denominator)
def __plot_principal_components__(features: np.ndarray, s: np.matrix = None): plt.scatter(features[:, :1], features[:, 1:]) if s is not None: s = s.transpose() s_list = s.tolist() x = np.linspace(-2, 2, 100) m1 = s_list[0][1] / s_list[0][0] y1 = m1 * x m2 = s_list[1][1] / s_list[1][0] y2 = m2 * x plt.plot(x, y1, c='blue') plt.plot(x, y2, c='red') plt.show()
def matrixAdjugate(mat: np.matrix) -> np.matrix: """ 求伴随 :param mat: :return: """ arr = mat.tolist() row = len(arr) col = len(arr[0]) if row != col: raise Exception("row != col") retArr = np.arange(row * col).reshape(row, col) for i in range(0, row): for j in range(0, col): retArr[j][i] = ModuloUtil.matrixAlgebraicComplementDet( mat, i, j) ret = np.mat(retArr) return ret
def matrixModuloInverse(mat: np.matrix, modulo: int) -> np.matrix: """ 矩阵模逆 :param mat: :param modulo: :return: """ adjugateMatrix = ModuloUtil.matrixAdjugate(mat) det = ModuloUtil.matrixDet(mat) if det == 0: raise Exception("matrix det is 0, has not Inverse!") accArr = adjugateMatrix.tolist() arr = mat.tolist() row = len(arr) col = len(arr[0]) retMatArr = np.arange(row * col).reshape(row, col) for i in range(0, row): for j in range(0, col): retMatArr[i][j] = ModuloUtil.numFractionModulo( int(accArr[i][j]), det, modulo) ret = np.mat(retMatArr) return ret
def unit_to_scalar(unit_matrix: np.matrix): return unit_matrix.tolist()[0][0]
def calcResidual(theta: np.matrix): c = np.matrix([1.0/math.tan(*value) for value in theta.tolist()]).T A = makeA(*c.T.tolist()) omega = makeOmega(*theta.T.tolist()) C = makeC(*c.T.tolist()) return A * (C ** -1) * omega
def matrix2Array(self, mat: np.matrix) -> np.ndarray: return np.concatenate(mat.tolist())