Python essentials for Machine learning
NumPy for Data Analytics
Mr. Vyankatesh Emche
Linear Algebra Functions
• np.linalg.solve is a NumPy function used to solve systems
of linear equations of the form:
𝐴𝑋=B
Where:
• 𝐴 is a square matrix (coefficients of equations).
• 𝑋 is the unknown variable matrix (solutions).
• B is the result matrix (constants on the right side).
• Solving a system of equations like
3𝑥+2𝑦=5
4x−y=2
To find the values of x and y
Example
• Scenario: Predicting Ad Performance
• We have 3 types of ad spend affecting total sales:
• TV Ads Spend
• Social Media Ads Spend
• YouTube Ads Spend
Given Past Data:
• 500𝑥1+200𝑥2+100𝑥3=700(Total Sales for Campaign 1)
• 400𝑥1+300𝑥2+150𝑥3=600(Total Sales for Campaign 2)
• 200𝑥1+400𝑥2+300𝑥3=500(Total Sales for Campaign 3)
• Here:
• 𝑥1,𝑥2,𝑥3 represent ad efficiency factors for TV, Social, and
YouTube Ads.
• We need to find 𝑥1,𝑥2,𝑥3
• 300𝑥1+200𝑥2+250𝑥3= ? (Total Sales for Campaign 4)
Np.dot
• The dot product is a simple multiplication of two vectors (1D
Arrays).
• .This function performs the dot product of two arrays.
• It is mainly used for:Matrix-vector multiplication (e.g., predictions in
linear regression)
• Vector-vector multiplication (e.g., weighted scores)
• Matrix-matrix multiplication (when one of them is a 2D array).
=
Students Data
• Consider dataset with 40 rows of student marks.
• We can use np.linalg.solve to calculate weights for
trim1_marks, trim2_marks, and trim3_marks to predict
trim4_marks.
• Then we can use np.dot to predict the trim4_marks for 10 new
students.
• Getting Square Matrix and relevant dependent
variable(multiplying both sides) value for passing it to
np.linalg.solve
Np.dot
Np.dot and np.matmul
• Both np.dot and np.matmul are used for matrix multiplication in NumPy,
but they behave differently in case of 3d Matrices(Tensors).
Any Queries?