Matrix In Python
Matrix in Python
A Python matrix is a specialized two-dimensional rectangular array of data stored in rows and columns. The data in a matrix can be numbers, strings, expressions, symbols, etc. Matrix is one of the important data structures that can be used in mathematical and scientific computations.Matrix is a special case of two dimensional array where each data element is of rigorously same size. So every matrix is also a two dimensional array but not vice versa.
Matrices are veritably important data structures for numerous fine and scientific computations. As we've formerly bandied two dimnsional array data structure in the former chapter we will be fastening on data structure operations specific to matrices in this chapter.
We also be using the numpy package for matrix data manipulation.
Matrix Example
Consider the case of recording temprature for 1 week measured in the morning,mid-day, evening andmid-night. It can be presented as a 7X5 matrix using an array and the reshape system available in numpy.
from numpy import *
a = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
. m = reshape (a, ())
. print (m)
Affair
The below data can be represented as a two dimensional array as below −
(
('Mon''18''20''22''17')
('Tue''11''18''21''18')
('Wed''15''21''20''19')
('Thu''11''20''22''21')
('Fri''18''17''23''22')
('Sat''12''22''20''18')
('Sun''13''15''19''16')
)
Penetrating Values
The data rudiments in a matrix can be penetrated by using the indicators. The access system is same as the way data is penetrated in Two dimensional array.
Illustration
from numpy import *
m = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
.
# Publish data for Wednesday
print (m (2))
# Publish data for friday evening
print (m (4) (3))
Affair
When the below law is executed, it produces the following result −
.
('Wed', 15, 21, 20, 19)
.23
Adding a row
Use the below mentioned law to add a row in a matrix.
Illustration
from numpy import *
m = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
.m_r = tack (m, ( ('Avg',)),)
.
print (m_r)
Affair
When the below law is executed, it produces the following result −
.
(
('Mon''18''20''22''17')
('Tue''11''18''21''18')
('Wed''15''21''20''19')
('Thu''11''20''22''21')
('Fri''18''17''23''22')
('Sat''12''22''20''18')
('Sun''13''15''19''16')
('Avg''12''15''13''11')
)
Adding a column
We can add column to a matrix using the insert () system. then we've to mention the indicator where we want to add the column and a array containing the new values of the columnsadded.In the below illustration we add t a new column at the fifth position from the morning.
Illustration
from numpy import *
m = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
.m_c = insert (m, (5), ( (1), (2), (3), (4), (5), (6), (7)),)
.
print (m_c)
Affair
When the below law is executed, it produces the following result −
.
(
('Mon''18''20''22''17''1')
('Tue''11''18''21''18''2')
('Wed''15''21''20''19''3')
('Thu''11''20''22''21''4')
('Fri''18''17''23''22''5')
('Sat''12''22''20''18''6')
('Sun''13''15''19''16''7')
)
Cancel a row
We can cancel a row from a matrix using the delete () system. We've to specify the indicator of the row and also the axis value which is 0 for a row and 1 for a column.
Illustration
from numpy import *
m = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
. m = delete (m, (2),)
.
print (m)
Affair
When the below law is executed, it produces the following result −
.
(
('Mon''18''20''22''17')
('Tue''11''18''21''18')
('Thu''11''20''22''21')
('Fri''18''17''23''22')
('Sat''12''22''20''18')
('Sun''13''15''19''16')
)
Cancel a column
We can cancel a column from a matrix using the delete () system. We've to specify the indicator of the column and also the axis value which is 0 for a row and 1 for a column.
Illustration
from numpy import *
m = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
. m = delete (m, s, (2),)
.
print (m)
Affair
When the below law is executed, it produces the following result −
.
(
('Mon''18''22''17')
('Tue''11''21''18')
('Wed''15''20''19')
('Thu''11''22''21')
('Fri''18''23''22')
('Sat''12''20''18')
('Sun''13''19''16')
)
Update a row
To modernize the values in the row of a matrix we simplyre-assign the values at the indicator of the row. In the below illustration all the values for thrusday's data is marked as zero. The indicator for this row is 3.
Illustration
from numpy import *
m = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
. m (3) = ('Thu',)
.
print (m)
Affair
When the below law is executed, it produces the following result −
.
(
('Mon''18''20''22''17')
('Tue''11''18''21''18')
('Wed''15''21''20''19')
('Thu''0''0''0''0')
('Fri''18''17''23''22')
('Sat''12''22''20''18')
('Sun''13''15''19''16')
)
Matrices are veritably important data structures for numerous fine and scientific computations. As we've formerly bandied two dimnsional array data structure in the former chapter we will be fastening on data structure operations specific to matrices in this chapter.
We also be using the numpy package for matrix data manipulation.
Matrix Example
Consider the case of recording temprature for 1 week measured in the morning,mid-day, evening andmid-night. It can be presented as a 7X5 matrix using an array and the reshape system available in numpy.
from numpy import *
a = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
. m = reshape (a, ())
. print (m)
Affair
The below data can be represented as a two dimensional array as below −
(
('Mon''18''20''22''17')
('Tue''11''18''21''18')
('Wed''15''21''20''19')
('Thu''11''20''22''21')
('Fri''18''17''23''22')
('Sat''12''22''20''18')
('Sun''13''15''19''16')
)
Penetrating Values
The data rudiments in a matrix can be penetrated by using the indicators. The access system is same as the way data is penetrated in Two dimensional array.
Illustration
from numpy import *
m = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
.
# Publish data for Wednesday
print (m (2))
# Publish data for friday evening
print (m (4) (3))
Affair
When the below law is executed, it produces the following result −
.
('Wed', 15, 21, 20, 19)
.23
Adding a row
Use the below mentioned law to add a row in a matrix.
Illustration
from numpy import *
m = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
.m_r = tack (m, ( ('Avg',)),)
.
print (m_r)
Affair
When the below law is executed, it produces the following result −
.
(
('Mon''18''20''22''17')
('Tue''11''18''21''18')
('Wed''15''21''20''19')
('Thu''11''20''22''21')
('Fri''18''17''23''22')
('Sat''12''22''20''18')
('Sun''13''15''19''16')
('Avg''12''15''13''11')
)
Adding a column
We can add column to a matrix using the insert () system. then we've to mention the indicator where we want to add the column and a array containing the new values of the columnsadded.In the below illustration we add t a new column at the fifth position from the morning.
Illustration
from numpy import *
m = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
.m_c = insert (m, (5), ( (1), (2), (3), (4), (5), (6), (7)),)
.
print (m_c)
Affair
When the below law is executed, it produces the following result −
.
(
('Mon''18''20''22''17''1')
('Tue''11''18''21''18''2')
('Wed''15''21''20''19''3')
('Thu''11''20''22''21''4')
('Fri''18''17''23''22''5')
('Sat''12''22''20''18''6')
('Sun''13''15''19''16''7')
)
Cancel a row
We can cancel a row from a matrix using the delete () system. We've to specify the indicator of the row and also the axis value which is 0 for a row and 1 for a column.
Illustration
from numpy import *
m = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
. m = delete (m, (2),)
.
print (m)
Affair
When the below law is executed, it produces the following result −
.
(
('Mon''18''20''22''17')
('Tue''11''18''21''18')
('Thu''11''20''22''21')
('Fri''18''17''23''22')
('Sat''12''22''20''18')
('Sun''13''15''19''16')
)
Cancel a column
We can cancel a column from a matrix using the delete () system. We've to specify the indicator of the column and also the axis value which is 0 for a row and 1 for a column.
Illustration
from numpy import *
m = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
. m = delete (m, s, (2),)
.
print (m)
Affair
When the below law is executed, it produces the following result −
.
(
('Mon''18''22''17')
('Tue''11''21''18')
('Wed''15''20''19')
('Thu''11''22''21')
('Fri''18''23''22')
('Sat''12''20''18')
('Sun''13''19''16')
)
Update a row
To modernize the values in the row of a matrix we simplyre-assign the values at the indicator of the row. In the below illustration all the values for thrusday's data is marked as zero. The indicator for this row is 3.
Illustration
from numpy import *
m = array ( ( ('Mon',), ('Tue',),
. ('Wed',), ('Thu',),
. ('Fri',), ('Sat',),
. ('Sun',)))
. m (3) = ('Thu',)
.
print (m)
Affair
When the below law is executed, it produces the following result −
.
(
('Mon''18''20''22''17')
('Tue''11''18''21''18')
('Wed''15''21''20''19')
('Thu''0''0''0''0')
('Fri''18''17''23''22')
('Sat''12''22''20''18')
('Sun''13''15''19''16')
)
Deep Vidhya Is The Platform Where You Can Learn Each And Everything About Python & Machine Learning.
#Artificial Intelligence
#Machine Learning
#Python
Comments
Post a Comment