1. Learning Outcomes#
या chapter नंतर तुम्हाला:
Linear System of Equationsम्हणजे काय ते समजेल.- अनेक equations एकत्र solve करण्याची गरज का पडते ते समजेल.
- equations ला matrix form Ax=b
Ax=bमध्ये कसे पाहायचे ते समजेल. coefficient matrix,unknown vectorआणिresult vectorयांची भूमिका समजेल.np.linalg.solve()वापरून square linear system solve करता येईल.Aआणिbच्या shapes कशा असाव्यात ते समजेल.- square matrix आणि full-rank requirement मागचे basic कारण समजेल.
- singular matrix मुळे
np.linalg.solve()का fail होऊ शकते ते समजेल. - मिळालेली solution
A @ xआणिnp.allclose()वापरून verify करता येईल. np.linalg.solve()हे manually inverse काढून solve करण्यापेक्षा का prefer केले जाते ते समजेल.
2. Linear System म्हणजे नेमके काय?#
समजा आपल्याला दोन unknown values शोधायच्या आहेत.
त्यांना xx आणि yy म्हणू.
आपल्याकडे दोन equations आहेत:
\begin{aligned}
2x+y &= 5 \\
x-y &= 1
\end{aligned}पहिली equation एकटी घेतली तर xx आणि yy च्या अनेक combinations मुळे उत्तर 55 येऊ शकते.
पण दुसरी equation आणखी एक condition देते.
आपल्याला अशी xx आणि yy values शोधायची आहेत ज्या दोन्ही equations एकाच वेळी satisfy करतील.
यालाच linear system किंवा system of linear equations म्हणतात.
Linear system solve करणे म्हणजे प्रत्येक equation वेगळी solve करणे नाही. सर्व equations एकत्र satisfy करणाऱ्या unknown values शोधणे होय.
आपल्या उदाहरणाचे उत्तर:
x=2,\qquad y=1कारण:
2(2)+1=5आणि:
2-1=1दोन्ही equations satisfy झाल्या.
3. Data Science मध्ये याची गरज का पडते?#
Linear algebra मध्ये अनेक problems अशा स्वरूपात येतात जिथे आपल्याकडे:
- अनेक unknown values असतात,
- त्या values मध्ये relationships असतात,
- आणि त्या relationships equations ने व्यक्त केलेल्या असतात.
अशा वेळी प्रत्येक equation हाताने manipulate करण्यापेक्षा matrix representation वापरणे सोपे होते.
NumPy आपल्याला हे matrix-based computation efficiently करण्यासाठी functions देते.
त्यातील महत्त्वाचे function म्हणजे:
np.linalg.solve()
पण ते वापरण्याआधी Ax=bAx=b ही कल्पना समजणे आवश्यक आहे.
4. Matrix Form Ax=bAx=b#
आपल्या equations पुन्हा पाहू:
\begin{aligned}
2x+y &= 5 \\
x-y &= 1
\end{aligned}यांना matrix form मध्ये असे लिहिता येते:
\begin{bmatrix}
2 & 1 \\
1 & -1
\end{bmatrix}
\begin{bmatrix}
x \\
y
\end{bmatrix}
=
\begin{bmatrix}
5 \\
1
\end{bmatrix}हेच compact form मध्ये:
Ax=bइथे तीन मुख्य गोष्टी आहेत.
| Symbol | Meaning | आपल्या उदाहरणात |
|---|---|---|
AA |
Coefficient Matrix | \begin{bmatrix}2&1\\1&-1\end{bmatrix}\begin{bmatrix}2&1\\1&-1\end{bmatrix} |
xx |
Unknown Vector | \begin{bmatrix}x\\y\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix} |
bb |
Result Vector | \begin{bmatrix}5\\1\end{bmatrix}\begin{bmatrix}5\\1\end{bmatrix} |
हा relationship visually पाहू.
Rendering diagram…
Diagram could not be rendered.
या flow मध्ये learner ने मुख्य गोष्ट अशी लक्षात ठेवायची:
आपल्याला AA माहित आहे.
आपल्याला bb माहित आहे.
आपल्याला xx शोधायचा आहे.
म्हणजे:
A \times x=bया relationship मधून unknown vector xx शोधणे हे आपले काम आहे.
5. Coefficient Matrix म्हणजे काय?#
Equation पहा:
2x+y=5इथे:
- x
xचा coefficient 22आहे. - y
yचा coefficient 11आहे.
दुसरी equation:
x-y=1इथे:
- x
xचा coefficient 11आहे. - y
yचा coefficient -1-1आहे.
हे coefficients एकत्र ठेवले की:
A=
\begin{bmatrix}
2 & 1 \\
1 & -1
\end{bmatrix}हा आपला coefficient matrix आहे.
Python मध्ये:
import numpy as np
A = np.array([
[2, 1],
[1, -1]
], dtype=float)प्रत्येक row एका equation चे coefficients represent करते.
प्रत्येक column एका unknown variable शी संबंधित असतो.
पहिला column xx साठी आहे.
दुसरा column yy साठी आहे.
6. Unknown Vector#
आपल्याला xx आणि yy शोधायचे आहेत.
म्हणून unknown vector:
x=
\begin{bmatrix}
x \\
y
\end{bmatrix}इथे थोडा naming confusion होऊ शकतो.
Mathematics मध्ये पूर्ण unknown vector ला अनेकदा xx असे नाव दिले जाते.
त्या vector मध्ये individual unknowns x_1,x_2,\ldotsx_1,x_2,\ldots किंवा आपल्या छोट्या उदाहरणात x,yx,y असू शकतात.
NumPy मध्ये solution मिळाल्यानंतर ती साधारण one-dimensional array असते.
उदाहरण:
solution = np.array([2.0, 1.0])याचा अर्थ:
- पहिली value पहिल्या unknown साठी
- दुसरी value दुसऱ्या unknown साठी
7. Result Vector bb#
Equations च्या उजव्या बाजूच्या values पाहू:
\begin{aligned}
2x+y &= 5 \\
x-y &= 1
\end{aligned}उजवीकडील values आहेत:
b=
\begin{bmatrix}
5 \\
1
\end{bmatrix}NumPy मध्ये:
b = np.array([5, 1], dtype=float)AA मध्ये unknowns चे coefficients असतात. bb मध्ये equations चे right-side results असतात. solve() आपल्यासाठी unknown vector शोधते.
8. np.linalg.solve() काय करते?#
NumPy मध्ये square linear system solve करण्यासाठी:
np.linalg.solve(A, b)वापरता येते.
याचा अर्थ साध्या भाषेत:
"
Aआणिbदिले आहेत. आता असा unknown vector शोध कीA @ xकेल्यावरbमिळेल."
General pattern:
solution = np.linalg.solve(A, b)इथे:
A→ coefficient matrixb→ result vectorsolution→ unknown values
9. 2×2 Linear System Solve करूया#
आपला system:
\begin{aligned}
2x+y &= 5 \\
x-y &= 1
\end{aligned}Python code:
import numpy as np
A = np.array([
[2, 1],
[1, -1]
], dtype=float)
b = np.array([5, 1], dtype=float)
solution = np.linalg.solve(A, b)
print(solution)Expected Output#
[2. 1.]Output मधील पहिली value xx साठी आहे.
दुसरी value yy साठी आहे.
म्हणून:
x=2,\qquad y=1आता या values equations मध्ये mentally ठेवून पाहू.
पहिली equation:
2(2)+1=5दुसरी equation:
2-1=1दोन्ही बरोबर आहेत.
म्हणून solution valid आहे.
10. solve() मध्ये प्रत्यक्षात आपण काय देतो?#
या complete process कडे एकदा एकत्र पाहू.
Rendering diagram…
Diagram could not be rendered.
Flow असा आहे:
- equations मधून coefficients वेगळे करतो.
- त्यातून
Aबनवतो. - right-side results मधून
bबनवतो. np.linalg.solve(A, b)call करतो.- solution vector मिळतो.
- solution पुन्हा original system satisfy करते का ते verify करतो.
ही शेवटची verification step खूप उपयोगी आहे.
11. Solution Matrix Multiplication ने Verify करणे#
आपल्याला माहीत आहे:
Ax=bम्हणून solve() ने मिळालेला xx खरोखर योग्य असेल तर:
A \times xकेल्यावर पुन्हा bb मिळायला हवा.
NumPy मध्ये matrix multiplication साठी @ operator वापरू शकतो.
import numpy as np
A = np.array([
[2, 1],
[1, -1]
], dtype=float)
b = np.array([5, 1], dtype=float)
solution = np.linalg.solve(A, b)
check = A @ solution
print(check)Expected Output#
[5. 1.]Original b होता:
b = np.array([5, 1], dtype=float)आणि A @ solution मधूनही [5. 1.] मिळाले.
म्हणजे solution original equations satisfy करते.
Solve करणे आणि verify करणे वेगळे steps आहेत. np.linalg.solve() unknown values शोधते. A @ solution त्या values original system satisfy करतात का हे तपासायला मदत करते.
12. मग थेट == वापरायचे का?#
Floating-point calculations मध्ये अनेकदा values mathematically equal असल्या तरी computer representation मध्ये अगदी tiny difference असू शकतो.
उदाहरणार्थ mathematically 11 असलेली value internally 0.9999999999999999 सारखी दिसू शकते.
म्हणून numerical calculations compare करताना exact equality नेहमीच योग्य नसते.
NumPy मध्ये यासाठी:
np.allclose()
खूप उपयोगी आहे.
import numpy as np
A = np.array([
[2, 1],
[1, -1]
], dtype=float)
b = np.array([5, 1], dtype=float)
solution = np.linalg.solve(A, b)
is_valid = np.allclose(A @ solution, b)
print(is_valid)Expected Output#
TrueTrue याचा अर्थ:
A @ solution आणि b मधील values numerical tolerance लक्षात घेतल्यावर sufficiently close आहेत.
म्हणून solution verify झाली.
13. Shape Requirements समजून घेऊया#
np.linalg.solve() वापरताना फक्त numbers बरोबर असणे पुरेसे नाही.
Arrays ची structure देखील योग्य असावी.
Single linear system साठी beginner level वर सर्वात महत्त्वाचा pattern:
A:\;(n,n)आणि:
b:\;(n,)उदाहरणार्थ दोन unknowns असतील तर:
print(A.shape)
print(b.shape)Expected Output#
(2, 2)
(2,)याचा अर्थ:
Aमध्ये 2 rows आणि 2 columns आहेत.bमध्ये 2 result values आहेत.
तीन unknowns असतील तर साधारण:
A:\;(3,3)आणि:
b:\;(3,)असतील.
14. A Square Matrix का असावी?#
Square matrix म्हणजे rows आणि columns ची संख्या समान.
उदाहरण:
\begin{bmatrix}
2 & 1 \\
1 & -1
\end{bmatrix}ही 2\times22\times2 square matrix आहे.
आपल्या current solve() use case मध्ये आपण अशा system वर काम करतो जिथे unknowns च्या संख्येला match करणारी square coefficient matrix असते.
उदाहरणार्थ:
- 2 unknowns → 2\times2
2\times2coefficient matrix - 3 unknowns → 3\times3
3\times3coefficient matrix - 4 unknowns → 4\times4
4\times4coefficient matrix
Non-square matrix दिल्यास हा standard square-system solving pattern लागू होत नाही.
b मध्ये दोन values आहेत म्हणून कोणतीही दोन-row matrix solve() ला चालेल असे नाही. A square असणे आवश्यक आहे.
15. Square असूनही प्रत्येक Matrix Solve होईल का?#
नाही.
हा खूप महत्त्वाचा मुद्दा आहे.
Square matrix असणे आवश्यक आहे.
पण ते एकटे पुरेसे नाही.
System कडे unique solution असणेही आवश्यक आहे.
Basic level वर याला full-rank requirement शी जोडून समजू शकतो.
n\times nn\times n square matrix साठी full rank म्हणजे तिचा rank nn असणे.
यामागचा beginner mental model असा ठेवा:
equations एकमेकांना पुरेशी independent information देत असतील तर unique solution मिळू शकते.
जर एक equation दुसऱ्या equation ची फक्त duplicate किंवा multiple असेल, तर नवीन independent information मिळत नाही.
तेव्हा matrix singular होऊ शकते.
16. Singular Matrix म्हणजे काय?#
उदाहरण:
\begin{aligned}
x+2y &= 3 \\
2x+4y &= 6
\end{aligned}दुसरी equation पहिल्या equation ची exactly दोनपट आहे.
म्हणून प्रत्यक्षात आपल्याकडे दोन स्वतंत्र conditions नाहीत.
Coefficient matrix:
A=
\begin{bmatrix}
1 & 2 \\
2 & 4
\end{bmatrix}ही matrix singular आहे.
Visual flow:
Rendering diagram…
Diagram could not be rendered.
इथे learner ने दोन gates लक्षात ठेवायचे:
पहिला gate:
A square आहे का?
दुसरा gate:
system unique solution देऊ शकतो का?
दोन्ही conditions योग्य असतील तर np.linalg.solve() वापरण्याचा standard case तयार होतो.
17. Singular Matrix Error Handle करणे#
हा code पहा:
import numpy as np
A = np.array([
[1, 2],
[2, 4]
], dtype=float)
b = np.array([3, 6], dtype=float)
try:
solution = np.linalg.solve(A, b)
print(solution)
except np.linalg.LinAlgError as error:
print(error)Expected Output#
Singular matrixइथे problem Python syntax मध्ये नाही.
Problem आपल्या linear system च्या mathematical structure मध्ये आहे.
दुसरी row पहिल्या row ची multiple आहे.
त्यामुळे independent information कमी झाली.
np.linalg.solve() unique solution देऊ शकत नाही.
LinAlgError: Singular matrix दिसल्यावर फक्त code syntax तपासू नका. A मधील equations/rows एकमेकांवर dependent आहेत का हेही तपासा.
18. Larger Square System Solve करणे#
np.linalg.solve() फक्त 2\times22\times2 system साठी नाही.
उदाहरणार्थ तीन unknowns असलेला system घेऊ.
\begin{aligned}
3x+y+z &= 8 \\
x+4y+2z &= 15 \\
2x+y+5z &= 19
\end{aligned}Coefficient matrix:
A=
\begin{bmatrix}
3 & 1 & 1 \\
1 & 4 & 2 \\
2 & 1 & 5
\end{bmatrix}Result vector:
b=
\begin{bmatrix}
8 \\
15 \\
19
\end{bmatrix}Python मध्ये:
import numpy as np
A = np.array([
[3, 1, 1],
[1, 4, 2],
[2, 1, 5]
], dtype=float)
b = np.array([8, 15, 19], dtype=float)
solution = np.linalg.solve(A, b)
print(solution)Expected Output#
[1. 2. 3.]म्हणजे:
x=1,\qquad y=2,\qquad z=3आता verify करू.
print(A @ solution)
print(np.allclose(A @ solution, b))Expected Output#
[ 8. 15. 19.]
Trueइथे 2×2 system पासून 3×3 system कडे गेलो.
पण core idea बदललेली नाही:
Ax=bआणि:
solution = np.linalg.solve(A, b)19. Larger System मध्ये Shape कशी बदलते?#
तीन unknowns असल्यामुळे:
print(A.shape)
print(b.shape)
print(solution.shape)Expected Output#
(3, 3)
(3,)
(3,)म्हणून:
A→ तीन equations आणि तीन unknown coefficientsb→ तीन equation resultssolution→ तीन unknown values
System मोठा झाला तरी mental model तोच राहतो.
20. Full-Rank Requirement — Basic Awareness#
rank हा linear algebra मधील independent information किती आहे याशी संबंधित concept आहे.
या chapter साठी एवढे समजणे पुरेसे आहे:
जर AA ही n\times nn\times n square matrix असेल आणि तिचा rank nn असेल, तर तिला full rank म्हणतात.
उदाहरण:
3\times33\times3 matrix साठी full rank म्हणजे rank 33.
Full-rank square matrix unique solution साठी आवश्यक condition देते.
जर rank कमी झाला, तर matrix singular होते आणि np.linalg.solve() unique solution देऊ शकत नाही.
"Square" आणि "full rank" हे एकच नाहीत. Matrix square असू शकते पण singular देखील असू शकते.
21. Solution Verify करण्याची Recommended Habit#
Linear system solve केल्यावर हा pattern उपयोगी आहे:
solution = np.linalg.solve(A, b)
is_valid = np.allclose(A @ solution, b)याचा अर्थ:
पहिले:
Ax=bsolve करा.
नंतर मिळालेला xx वापरून:
Axपुन्हा calculate करा.
आणि तो result bb जवळ आहे का ते np.allclose() ने तपासा.
हा छोटा verification step debugging आणि numerical work मध्ये खूप उपयोगी ठरतो.
22. np.linalg.solve() vs Manual Matrix Inversion#
Mathematically आपण कधी कधी असे लिहिलेले पाहतो:
Ax=bआणि inverse उपलब्ध असेल तर:
x=A^{-1}bयामुळे beginner ला असे वाटू शकते:
"मग आधी inverse काढून multiply का करू नये?"
NumPy मध्ये technically असे करता येते:
solution = np.linalg.inv(A) @ bपण linear system solve करण्यासाठी सामान्यतः हा preferred approach नाही.
त्याऐवजी:
solution = np.linalg.solve(A, b)prefer केले जाते.
दोन्ही approaches मधील फरक#
| Approach | Code | Practical Preference |
|---|---|---|
| Direct solve | np.linalg.solve(A, b) |
Preferred |
| Explicit inverse | np.linalg.inv(A) @ b |
सामान्य solving साठी टाळणे चांगले |
याची मुख्य कारणे:
- explicit inverse वेगळा calculate करण्याची गरज पडत नाही.
- direct solving साधारण अधिक computationally appropriate असते.
- numerical calculations मध्ये explicit inverse अनावश्यक numerical error वाढवू शकते.
- code मधून intention स्पष्ट दिसते — "हा system solve करायचा आहे."
To solve Ax=bAx=b, prefer np.linalg.solve(A, b) over explicitly computing np.linalg.inv(A) @ b when a direct solve is appropriate.
23. Common Mistakes#
Mistake 1 — Equation मधील coefficients चुकीच्या order मध्ये ठेवणे#
समजा variables ची order x,yx,y आहे.
मग प्रत्येक row मध्ये coefficients त्याच order मध्ये असले पाहिजेत.
Equation:
2x+3y=7तर row:
[2, 3]असावी.
[3, 2] केली तर system बदलून जाईल.
Mistake 2 — b मध्ये coefficients ठेवणे#
b मध्ये right-side results असतात.
Equation:
2x+y=5यासाठी:
A_row = [2, 1]
b_value = 5असे separation असते.
Mistake 3 — A non-square असणे#
Standard np.linalg.solve() square coefficient system साठी आहे.
A.shape तपासणे ही चांगली debugging habit आहे.
Mistake 4 — Square म्हणजे automatically solvable असे समजणे#
Square matrix singular असू शकते.
म्हणून unique solution guaranteed नसते.
Mistake 5 — Solution verify न करणे#
Especially larger systems मध्ये:
np.allclose(A @ solution, b)ही छोटी check खूप उपयोगी आहे.
Mistake 6 — Linear system साठी unnecessary inverse वापरणे#
फक्त A^{-1}bA^{-1}b formula माहित आहे म्हणून:
np.linalg.inv(A) @ bdefault approach करू नका.
Direct solve available असेल तर:
np.linalg.solve(A, b)prefer करा.
24. Interview Preparation#
What does np.linalg.solve(A, b) compute?Show Answer
np.linalg.solve(A, b) computes the solution vector xx for the linear system Ax=bAx=b, assuming the coefficient matrix is square and the system has a unique solution.
Key Points:
Ais the coefficient matrix.bis the right-hand-side vector.- The returned array contains the unknown values.
- A singular coefficient matrix cannot produce a unique solution through
solve().
Why interviewer asks this:
To check whether you understand the mathematical meaning of the NumPy API rather than only its syntax.
Why is np.linalg.solve(A, b) generally preferred over np.linalg.inv(A) @ b?Show Answer
For solving a linear system, np.linalg.solve(A, b) avoids explicitly computing the matrix inverse and is generally more computationally appropriate and numerically preferable.
Key Points:
- No explicit inverse is required.
- The code directly expresses the intention to solve a system.
- Explicit inversion can add unnecessary computation and numerical error.
Why interviewer asks this:
To check whether you can choose an appropriate numerical linear algebra operation instead of mechanically applying formulas.
How can you verify a solution returned by np.linalg.solve()?Show Answer
Multiply the coefficient matrix by the computed solution and compare the result with the original right-hand-side vector, preferably using np.allclose(A @ solution, b) for floating-point values.
Key Points:
- Reconstruct the left side using
A @ solution. - Compare it with
b. np.allclose()handles small floating-point differences.
Why interviewer asks this:
To check whether you validate numerical results and understand the original equation Ax=bAx=b.
25. या Chapter मधून काय लक्षात ठेवायचे?#
- अनेक linear equations एकत्र solve करायच्या असतील तर system ला Ax=b
Ax=bया matrix form मध्ये पाहू शकतो. - A
Aम्हणजे coefficient matrix. - x
xम्हणजे unknown vector. - b
bम्हणजे result vector. - NumPy मध्ये square system solve करण्यासाठी
np.linalg.solve(A, b)वापरता येते. - single right-side vector साठी beginner pattern म्हणजे
A.shape == (n, n)आणिb.shape == (n,). - square matrix असणे आवश्यक आहे, पण तेवढेच पुरेसे नाही.
- unique solution साठी coefficient matrix singular नसावी; square full-rank matrix हा standard valid case आहे.
- singular matrix असल्यास
np.linalg.LinAlgErrorयेऊ शकतो. - solution मिळाल्यानंतर
A @ solutionने original result reconstruct करता येतो. - floating-point verification साठी
np.allclose(A @ solution, b)उपयोगी आहे. - linear system solve करताना explicit inverse काढण्यापेक्षा
np.linalg.solve()सामान्यतः prefer करावे.