A Python 3 library of matrices
Project description
MatricesM (Alpha)
Python 3 library for creating matrices and doing matrix operations related to statistics and algebra mathematics
Install using pip:
pip install MatricesM
Import by using:
import MatricesM.matrices as mm #Use by calling : mm.MatrixName(arguments)
OR
from MatricesM.matrices import * #Use matrices directly : MatrixName(arguments)
Import and print example matrices:
from MatricesM.exampleMatrices import *
Basic syntax:
IntegerMatrix = Matrix(dim=dimension,#Required, int | list as [rows,cols]
listed=elements, #Optional, list of numbers | list of lists containing numbers | string. If no argument is passed matrix is filled depending on the randomFill
directory=directory, #Optional, string. Path to the dataset. listed parameter shouldn't get any value if directory is given
ranged=intervalToPickFrom, #Optional, list as [lowerBound,upperBound]. Default is [-5,5]
randomFill=fillRandomly, #Optional, boolean. Default is 1. Doesn't affect the matrix if "listed" or "directory" is given
header=hasHeader, #Optional, boolean. Default is 0. Wheter or not the dataset in the "directory" has a header row
features=columnNames #Optional, list of strings. If no argument given, columns get named "Col {}".format(colNumber)
seed=randomSeed #Optional, int|float|complex|str . Seed to generate the random numbers from, doesn't affect anything if numbers are provided.
)
FloatMatrix = FMatrix(#Same parameters from the Matrix class
decimal=decimalsToRoundOnPrinting, #Optional, default is 4.
)
IdentityMatrix = Identity(dim=dimension #Required, int
)
ComplexMatrix = CMatrix(dim=dimension, #Required, int | list as [rows,cols]
ranged=intervalToPickFrom, #Optional, list as [lowerBound,upperBound]. Default is [0,1]
randomFill=fillRandomly, #Optional, boolean. Default is 1. Doesn't affect the matrix if "listed" or "directory" is given
features=columnNames #Optional, list of strings. If no argument given, columns get named "Col {}".format(colNumber)
)
-matrices.py contains Matrix class and FMatrix, CMatrix and Identity sub-classes
-exampleMatrices.py contains example matrices
-Check the project tab to see the progress
Some examples:
Create matrices filled with random integers
A=Matrix(4) #Creates a 4x4 matrix filled with random integers from the default range which is [-5,5]
B=Matrix([3,5],ranged=[10,25]) #Creates a 3x5 matrix with elements ranged between 10 and 25
Create matrices filled with random float numbers
E=FMatrix(6) #Create a 6x6 square matrix filled with random float values in the default range
F=FMatrix(dim=[2,5],randomFill=0) #Fill the matrix with zeros
Create identity matrices
i=Identity(3) #3x3 identity matrix
i.addDim(2) #Add 2 dimensions to get a 5x5 identity matrix
Create matrices filled with random complex numbers (Not 100% functional, check https://github.com/MathStuff/MatricesM/issues )
Cm1=CMatrix(5) #Create a square matrix filled with random float values in the default range
Give list of numbers to create matrices
filled_rows=[[1,2,3],[4,5,6],[7,8,9]]
C=Matrix(3,filled_rows) #Creates a 3x3 matrix with the given list of numbers
C1=FMatrix(3,"1 0 -1 4 5 5 1 2 2") #Creates a 3x3 matrix from the given string
C2=Matrix([2,4],"5 -2 -3 2 1 0 0 4") #Creates a 2x4 matrix from the given string
Generate randomly filled matrices
randomData1=FMatrix([10000,4],ranged={"height":[10,100],"weight":[200,500],"cost":[200,1000],"quality":[0,10]})
randomData2=Matrix([10000,4],ranged={"feature1":[0,25],"feature2":[100,200],"feature3":[1000,10000],"feature4":[1,100]})
Give a string filled with data and use the numbers in it to create a matrix
data="""1,K,60,69900,6325
2,K,30,79000,5200
3,E,52,85500,7825
4,E,57,17100,8375
5,E,55,5500,5450
6,E,68,27200,8550
7,E,41,20500,4500
8,E,20,69000,5050
9,K,33,13200,8325
10,E,37,31800,5975"""
#Creat an "integer matrix" from the given string, dimension is *required* as [dataAmount,features]
D = Matrix(dim=[10,4],
listed=data,
features=["id","age","num1","num2"]
)
OR
Read data from files (Only tested on CSV and TXT files)
header: boolean value, True if data file has a header as the first row, default is False
If bool(header) is True, feature names automatically get picked up from the first row
data_directory="Example\Directory\DATAFILE"
data_dim=[data_amount,feature_amount]
data_matrix=FMatrix(dim=data_dim,directory=data_directory,header=1) #Create a float matrix from a table of data
Use your matrix's methods and properties
Basics
C.grid #Prints the matrix's elements as a grid
C.p #Print the type, dimension, column names and the grid
C.decimal #Returns the chosen amount of decimals while printing. Can be used to set it's value
C.directory #Returns the directory of the matrix if there is any given
C.matrix #Returns the matrix's rows as lists in a list
C.dim #Returns the dimension of the matrix; can be used to change the dimensions, ex: [4,8] can be set to [1,32] where rows carry over as columns in order from left to right
C.string #Returns the string form of the matrix's elements
C.col(n,as_matrix) #Returns nth column of the matrix as a list or matrix, set as_matrix to True to get the list as a matrix
C.row(n,as_matrix) #Returns nth row of the matrix as a list or matrix, set as_matrix to True to get the list as a matrix
C.subM(rowStart,rowEnd,columnStart,columnEnd) #Returns a sub-matrix by using the parameters as corners. Check exampleMatrices.py
C.concat(matrix,concat_as) #Merges a matrix to itself. concat_as is set to "row" by default; if concatenation required is as columns, give "col" as the argument
C.copy #Returns a copy of the matrix
C.obj #Returns the string form of the object
C.seed #Returns the seed used to generate the random numbers in the matrix, returns None if matrix wasn't filled randomly. Seed can be changed to refill the matrix in-place
C.intForm #Returns integer form of the matrix
C.floatForm #Returns integer form of the matrix
C.ceilForm #Returns a matrix of all the elements' ceiling value
C.floorForm #Returns the same matrix as "intForm"
C.roundForm(n) #Returns a matrix of elements' rounded up to n decimal digits
#Available arithmetic operators : "@", "+", "-", "*", "/", "//", "**", "%"
#Available comparison operators : "<" ,"<=", ">", ">=", "==", "!="
Algebric properties
C.uptri #Returns the upper triangular form of the matrix
C.lowtri #Returns the lower triangular form of the matrix
C.sym #Returns the symmetric part of the matrix
C.anti #Returns the antisymmetric part of the matrix
C.det #Returns the determinant of the matrix
C.perma #Returns the permanent of the matrix
C.t #Returns the transposed matrix
C.ht #Returns the hermitian-transpose of the matrix
C.conj #Returns the conjugated forms of the elements in a matrix
C.minor(m,n) #Returns the mth row's nth element's minor matrix
C.adj #Returns the adjoint matrix
C.inv #Returns the inversed matrix
C.pseudoinv #Returns the pseudo inverse of the matrix
C.trace #Returns the trace of the matrix
C.rank #Returns the rank of the matrix
C.rrechelon #Returns the reduced row echelon form of the matrix
C.eigenvalues #Returns the eigenvalues (CHECK ISSUE#22 : https://github.com/MathStuff/MatricesM/issues/22)
C.isSquare #Returns True if the matrix is a square matrix
C.isSymmetric #Returns True if the matrix is a symmetric matrix
C.isAntiSymmetric #Returns True if the matrix is an antisymmetric matrix
C.isHermitian #Returns True if the matrix is a hermitian matrix
C.isTriangular #Returns True if the matrix is a triangular matrix
C.isUpperTri #Returns True if the matrix is a upper-trianguar matrix
C.isLowerTri #Returns True if the matrix is a lower-triangular matrix
C.isDiagonal #Returns True if the matrix is a diagonal matrix
C.isIdempotent #Returns True if the matrix is an idempotent matrix
C.isOrthogonal #Returns True if the matrix is an orthogonal matrix
Statistical properties
C.head(n) #Returns the first n rows (if there are less than n rows it returns all the rows)
C.tail(n) #Returns the last n rows (if there are less than n rows it returns all the rows)
C.find(element,indexStart) #Returns a list of the element's indeces as tuples. Returns None if element not in matrix
C.mean(n) #Returns the nth column's average, give None as argument to get the all columns' averages
C.ranged(n) #Returns the nth column's range, give None as argument to get the all columns' ranges
C.median(n) #Returns the nth column's median, give None to get all columns' medians
C.freq(n) #Returns the nth column's elements frequency as a dictionary where elements are keys and how often they repeat as values. If called without arguments, returns every column"s frequencies
C.mode(n) #Returns the nth column's mode, give None to get all columns' modes
C.iqr(n,as_quartiles) #Returns the nth column's iqr, give None to get all columns' iqr values. If first,second and third quartiles is desired, give as_quartiles parameter bool(True)
C.sdev(n,population) #Returns the nth column's standard deviation, if None is given as an argument returns all columns' standard deviations. Give population parameter True if calculation is not for samples
C.variance(n,population) #Returns the nth column's variance, if None is given as an argument returns all columns' variance. Give population parameter True if calculation is not for samples
C.z(row,col) #Returns the z-scores of the desired row and/or column, call without arguments to get the all z-scores as a matrix
C.corr(column_1,column_2) #Returns linear correlation of 2 columns chosen from the matrix. If no argument given, returns the correlation matrix
C.features #Returns the column names if given, can also be used to set column names
C.setFeatures() #Can be used to fix column naming issues, sets the column names to defaults
Add or remove rows/columns and operate on them
E.add(r=3,lis=[1.0 ,2.5 ,52,242 ,-9883,212, 0.000001, -555,554]) #Make the list given the 3rd row
A.remove(c=2) #Remove the second column
F*=[2]+[1]*F.dim[1]-1 #Multiplies the first column with 2 and the rest with 1
B @ B.t #Matrix multiplication example
All calculations below returns a matrix filled with 1's where the condition is True, otherwise 0
A**2 == A*A
A*2 == A+A
A.t.t == A
A @ Identity(A.dim[0]) == A #A assumed to be a square matrix
A.adj.t[1][2]==A.minor(2,3).det*-1
#roundForm() call is currently required for the next examples due to ~%1e-5 error rate on some calculations
(A.lowtri@A.uptri).roundForm(4)==A.roundForm(4)
(A.inv.inv).roundForm(4)==A.roundForm(4)
(B @ B.inv).roundForm() == Identity(B.dim[0])
More examples can be found in exampleMatrices.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
MatricesM-0.9a8.post1.tar.gz
(34.0 kB
view hashes)
Built Distribution
Close
Hashes for MatricesM-0.9a8.post1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0c4f6a02534a20bbc7edbc8d99ebbf48f32a39804910c5ef2e8955a27a28aae |
|
MD5 | 55278cf6bf99b33f0bb16713f4d2ea9e |
|
BLAKE2b-256 | b7b04bdb60422c2de4301bc08366b584f107e1bf9e231f3ab27dda231972ef0b |