[{"data":1,"prerenderedAt":732},["ShallowReactive",2],{"$PLKUzqzy7V":3},{"id":4,"title":5,"body":6,"description":84,"extension":725,"meta":726,"navigation":727,"path":728,"seo":729,"stem":730,"__hash__":731},"content/modules/regression/linear-regression/content.md","Linear Regression",{"type":7,"value":8,"toc":690},"minimark",[9,13,18,22,25,29,32,51,55,66,71,74,85,88,115,119,122,128,131,135,142,151,155,158,162,165,171,175,182,188,191,199,203,206,210,213,219,230,234,241,245,253,257,260,280,283,286,289,308,311,315,324,327,330,335,349,353,356,362,377,381,384,390,392,400,405,431,435,438,444,458,462,465,498,502,505,522,526,529,546,550,582,586,589,627,631,634,648,651,655,658],[10,11,5],"h1",{"id":12},"linear-regression",[14,15,17],"h2",{"id":16},"introduction","Introduction",[19,20,21],"p",{},"Linear regression is one of the fundamental algorithms in machine learning and statistics. It's used to model the relationship between a dependent variable (what we want to predict) and one or more independent variables (the features we use to make predictions).",[19,23,24],{},"The beauty of linear regression lies in its simplicity and interpretability. Unlike complex \"black box\" models, you can easily understand how each feature contributes to the prediction.",[14,26,28],{"id":27},"what-youll-learn","What You'll Learn",[19,30,31],{},"By the end of this module, you will:",[33,34,35,39,42,45,48],"ul",{},[36,37,38],"li",{},"Understand how linear regression models relationships in data",[36,40,41],{},"Learn how gradient descent optimizes model parameters",[36,43,44],{},"Interpret key regression metrics (MSE, RMSE, R², MAE)",[36,46,47],{},"Recognize the impact of learning rate on model training",[36,49,50],{},"Visualize the relationship between data and the fitted line",[14,52,54],{"id":53},"the-linear-model","The Linear Model",[19,56,57,62],{},[58,59],"img",{"alt":60,"src":61},"Linear Regression Visualization","https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Linear_regression.svg/500px-Linear_regression.svg.png",[63,64,65],"em",{},"Linear regression fits a line through data points to minimize prediction error",[67,68,70],"h3",{"id":69},"simple-linear-regression","Simple Linear Regression",[19,72,73],{},"For the simplest case with one feature, the linear regression model is:",[75,76,81],"pre",{"className":77,"code":79,"language":80},[78],"language-text","y = w₁x + w₀\n","text",[82,83,79],"code",{"__ignoreMap":84},"",[19,86,87],{},"Where:",[33,89,90,97,103,109],{},[36,91,92,96],{},[93,94,95],"strong",{},"y"," is the predicted value (dependent variable)",[36,98,99,102],{},[93,100,101],{},"x"," is the input feature (independent variable)",[36,104,105,108],{},[93,106,107],{},"w₁"," is the weight or slope (how much y changes when x changes)",[36,110,111,114],{},[93,112,113],{},"w₀"," is the bias or intercept (the value of y when x = 0)",[67,116,118],{"id":117},"multiple-linear-regression","Multiple Linear Regression",[19,120,121],{},"When we have multiple features, the model extends to:",[75,123,126],{"className":124,"code":125,"language":80},[78],"y = w₁x₁ + w₂x₂ + ... + wₙxₙ + w₀\n",[82,127,125],{"__ignoreMap":84},[19,129,130],{},"Each feature has its own weight that determines its contribution to the prediction.",[14,132,134],{"id":133},"how-it-works-gradient-descent","How It Works: Gradient Descent",[19,136,137,138,141],{},"Linear regression uses an optimization algorithm called ",[93,139,140],{},"gradient descent"," to find the best weights. Here's how it works:",[19,143,144,148],{},[58,145],{"alt":146,"src":147},"Gradient Descent Visualization","https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Gradient_descent.svg/500px-Gradient_descent.svg.png",[63,149,150],{},"Gradient descent iteratively moves toward the minimum of the loss function",[67,152,154],{"id":153},"step-1-initialize-parameters","Step 1: Initialize Parameters",[19,156,157],{},"Start with random (or zero) values for all weights and the bias.",[67,159,161],{"id":160},"step-2-make-predictions","Step 2: Make Predictions",[19,163,164],{},"For each data point, calculate the predicted value using the current weights:",[75,166,169],{"className":167,"code":168,"language":80},[78],"ŷᵢ = w₁x₁ᵢ + w₂x₂ᵢ + ... + wₙxₙᵢ + w₀\n",[82,170,168],{"__ignoreMap":84},[67,172,174],{"id":173},"step-3-calculate-error","Step 3: Calculate Error",[19,176,177,178,181],{},"Measure how far off our predictions are using the ",[93,179,180],{},"Mean Squared Error (MSE)",":",[75,183,186],{"className":184,"code":185,"language":80},[78],"MSE = (1/m) Σ(yᵢ - ŷᵢ)²\n",[82,187,185],{"__ignoreMap":84},[19,189,190],{},"Where m is the number of data points. We square the errors to:",[33,192,193,196],{},[36,194,195],{},"Make all errors positive",[36,197,198],{},"Penalize larger errors more heavily",[67,200,202],{"id":201},"step-4-compute-gradients","Step 4: Compute Gradients",[19,204,205],{},"Calculate how much each weight should change to reduce the error. This involves computing the derivative of the loss function with respect to each parameter.",[67,207,209],{"id":208},"step-5-update-parameters","Step 5: Update Parameters",[19,211,212],{},"Adjust the weights in the direction that reduces the error:",[75,214,217],{"className":215,"code":216,"language":80},[78],"w₁ = w₁ - α × gradient_w₁\nw₀ = w₀ - α × gradient_w₀\n",[82,218,216],{"__ignoreMap":84},[19,220,221,222,225,226,229],{},"Where ",[93,223,224],{},"α"," (alpha) is the ",[93,227,228],{},"learning rate"," - a hyperparameter that controls how big each step is.",[67,231,233],{"id":232},"step-6-repeat","Step 6: Repeat",[19,235,236,237,240],{},"Continue steps 2-5 for a specified number of ",[93,238,239],{},"epochs"," (iterations through the entire dataset) or until the model converges.",[14,242,244],{"id":243},"key-hyperparameters","Key Hyperparameters",[19,246,247,250],{},[58,248],{"alt":249,"src":147},"Learning Rate Impact",[63,251,252],{},"Effect of different learning rates on convergence",[67,254,256],{"id":255},"learning-rate-α","Learning Rate (α)",[19,258,259],{},"The learning rate controls how quickly the model learns:",[33,261,262,268,274],{},[36,263,264,267],{},[93,265,266],{},"Too small",": Training will be very slow, requiring many epochs to converge",[36,269,270,273],{},[93,271,272],{},"Too large",": The model may overshoot the optimal solution and fail to converge",[36,275,276,279],{},[93,277,278],{},"Just right",": The model converges efficiently to a good solution",[19,281,282],{},"Typical values: 0.001 to 0.1",[67,284,285],{"id":239},"Epochs",[19,287,288],{},"The number of times the algorithm processes the entire dataset:",[33,290,291,297,303],{},[36,292,293,296],{},[93,294,295],{},"Too few",": The model may not have learned the pattern fully (underfitting)",[36,298,299,302],{},[93,300,301],{},"Too many",": Wastes computation time once the model has converged",[36,304,305,307],{},[93,306,278],{},": Enough iterations for the loss to stabilize",[19,309,310],{},"Typical values: 50 to 500 for simple problems",[14,312,314],{"id":313},"performance-metrics","Performance Metrics",[19,316,317,321],{},[58,318],{"alt":319,"src":320},"Regression Metrics Visualization","https://upload.wikimedia.org/wikipedia/commons/thumb/8/86/Coefficient_of_Determination.svg/500px-Coefficient_of_Determination.svg.png",[63,322,323],{},"Visualization of residuals and model fit quality",[67,325,180],{"id":326},"mean-squared-error-mse",[19,328,329],{},"The average of squared differences between predictions and actual values:",[75,331,333],{"className":332,"code":185,"language":80},[78],[82,334,185],{"__ignoreMap":84},[33,336,337,343,346],{},[36,338,339,342],{},[93,340,341],{},"Lower is better"," (0 is perfect)",[36,344,345],{},"Sensitive to outliers due to squaring",[36,347,348],{},"Units are squared (e.g., if predicting price in dollars, MSE is in dollars²)",[67,350,352],{"id":351},"root-mean-squared-error-rmse","Root Mean Squared Error (RMSE)",[19,354,355],{},"The square root of MSE:",[75,357,360],{"className":358,"code":359,"language":80},[78],"RMSE = √MSE\n",[82,361,359],{"__ignoreMap":84},[33,363,364,368,371,374],{},[36,365,366,342],{},[93,367,341],{},[36,369,370],{},"Same units as the target variable",[36,372,373],{},"More interpretable than MSE",[36,375,376],{},"Still sensitive to outliers",[67,378,380],{"id":379},"r-score-coefficient-of-determination","R² Score (Coefficient of Determination)",[19,382,383],{},"Measures the proportion of variance in the target variable explained by the model:",[75,385,388],{"className":386,"code":387,"language":80},[78],"R² = 1 - (SS_residual / SS_total)\n",[82,389,387],{"__ignoreMap":84},[19,391,87],{},[33,393,394,397],{},[36,395,396],{},"SS_residual = Σ(yᵢ - ŷᵢ)² (sum of squared residuals)",[36,398,399],{},"SS_total = Σ(yᵢ - ȳ)² (total sum of squares)",[19,401,402],{},[93,403,404],{},"Interpretation:",[33,406,407,413,419,425],{},[36,408,409,412],{},[93,410,411],{},"R² = 1.0",": Perfect predictions",[36,414,415,418],{},[93,416,417],{},"R² = 0.0",": Model is no better than predicting the mean",[36,420,421,424],{},[93,422,423],{},"R² \u003C 0.0",": Model is worse than predicting the mean",[36,426,427,430],{},[93,428,429],{},"R² = 0.7",": Model explains 70% of the variance",[67,432,434],{"id":433},"mean-absolute-error-mae","Mean Absolute Error (MAE)",[19,436,437],{},"The average of absolute differences between predictions and actual values:",[75,439,442],{"className":440,"code":441,"language":80},[78],"MAE = (1/m) Σ|yᵢ - ŷᵢ|\n",[82,443,441],{"__ignoreMap":84},[33,445,446,450,452,455],{},[36,447,448,342],{},[93,449,341],{},[36,451,370],{},[36,453,454],{},"Less sensitive to outliers than MSE/RMSE",[36,456,457],{},"All errors weighted equally",[14,459,461],{"id":460},"assumptions-of-linear-regression","Assumptions of Linear Regression",[19,463,464],{},"For linear regression to work well, several assumptions should hold:",[466,467,468,474,480,486,492],"ol",{},[36,469,470,473],{},[93,471,472],{},"Linearity",": The relationship between features and target is linear",[36,475,476,479],{},[93,477,478],{},"Independence",": Observations are independent of each other",[36,481,482,485],{},[93,483,484],{},"Homoscedasticity",": The variance of errors is constant across all levels of features",[36,487,488,491],{},[93,489,490],{},"Normality",": The residuals (errors) are normally distributed",[36,493,494,497],{},[93,495,496],{},"No multicollinearity",": Features are not highly correlated with each other (for multiple regression)",[14,499,501],{"id":500},"when-to-use-linear-regression","When to Use Linear Regression",[19,503,504],{},"Linear regression is ideal when:",[33,506,507,510,513,516,519],{},[36,508,509],{},"You need an interpretable model",[36,511,512],{},"The relationship between features and target is approximately linear",[36,514,515],{},"You want to understand feature importance",[36,517,518],{},"You need fast training and prediction",[36,520,521],{},"You have continuous numerical targets",[14,523,525],{"id":524},"limitations","Limitations",[19,527,528],{},"Linear regression may not work well when:",[33,530,531,534,537,540,543],{},[36,532,533],{},"The relationship is highly non-linear",[36,535,536],{},"There are complex interactions between features",[36,538,539],{},"The data has many outliers",[36,541,542],{},"Features have very different scales (solution: normalize/standardize)",[36,544,545],{},"There's significant multicollinearity",[14,547,549],{"id":548},"tips-for-better-results","Tips for Better Results",[466,551,552,558,564,570,576],{},[36,553,554,557],{},[93,555,556],{},"Feature Scaling",": Normalize or standardize features to similar ranges",[36,559,560,563],{},[93,561,562],{},"Feature Engineering",": Create polynomial features for non-linear relationships",[36,565,566,569],{},[93,567,568],{},"Outlier Handling",": Remove or transform extreme outliers",[36,571,572,575],{},[93,573,574],{},"Regularization",": Use Ridge (L2) or Lasso (L1) regression to prevent overfitting",[36,577,578,581],{},[93,579,580],{},"Cross-Validation",": Validate performance on unseen data",[14,583,585],{"id":584},"real-world-applications","Real-World Applications",[19,587,588],{},"Linear regression is used in many domains:",[33,590,591,597,603,609,615,621],{},[36,592,593,596],{},[93,594,595],{},"Economics",": Predicting GDP, inflation, stock prices",[36,598,599,602],{},[93,600,601],{},"Healthcare",": Modeling disease progression, drug dosage",[36,604,605,608],{},[93,606,607],{},"Marketing",": Sales forecasting, customer lifetime value",[36,610,611,614],{},[93,612,613],{},"Real Estate",": House price prediction",[36,616,617,620],{},[93,618,619],{},"Climate Science",": Temperature trends, sea level rise",[36,622,623,626],{},[93,624,625],{},"Sports",": Player performance prediction",[14,628,630],{"id":629},"summary","Summary",[19,632,633],{},"Linear regression is a powerful yet simple algorithm that:",[33,635,636,639,642,645],{},[36,637,638],{},"Models linear relationships between features and targets",[36,640,641],{},"Uses gradient descent to optimize parameters",[36,643,644],{},"Provides interpretable coefficients",[36,646,647],{},"Serves as a foundation for more complex algorithms",[19,649,650],{},"Understanding linear regression is essential for any machine learning practitioner, as it introduces key concepts like loss functions, optimization, and model evaluation that apply to more advanced algorithms.",[14,652,654],{"id":653},"next-steps","Next Steps",[19,656,657],{},"After mastering linear regression, you can explore:",[33,659,660,666,672,678,684],{},[36,661,662,665],{},[93,663,664],{},"Polynomial Regression",": Fitting non-linear relationships",[36,667,668,671],{},[93,669,670],{},"Regularized Regression",": Ridge and Lasso for preventing overfitting",[36,673,674,677],{},[93,675,676],{},"Logistic Regression",": Classification using a similar approach",[36,679,680,683],{},[93,681,682],{},"Multiple Regression",": Working with many features",[36,685,686,689],{},[93,687,688],{},"Time Series Regression",": Predicting sequential data",{"title":84,"searchDepth":691,"depth":692,"links":693},2,3,[694,695,696,700,708,712,718,719,720,721,722,723,724],{"id":16,"depth":691,"text":17},{"id":27,"depth":691,"text":28},{"id":53,"depth":691,"text":54,"children":697},[698,699],{"id":69,"depth":692,"text":70},{"id":117,"depth":692,"text":118},{"id":133,"depth":691,"text":134,"children":701},[702,703,704,705,706,707],{"id":153,"depth":692,"text":154},{"id":160,"depth":692,"text":161},{"id":173,"depth":692,"text":174},{"id":201,"depth":692,"text":202},{"id":208,"depth":692,"text":209},{"id":232,"depth":692,"text":233},{"id":243,"depth":691,"text":244,"children":709},[710,711],{"id":255,"depth":692,"text":256},{"id":239,"depth":692,"text":285},{"id":313,"depth":691,"text":314,"children":713},[714,715,716,717],{"id":326,"depth":692,"text":180},{"id":351,"depth":692,"text":352},{"id":379,"depth":692,"text":380},{"id":433,"depth":692,"text":434},{"id":460,"depth":691,"text":461},{"id":500,"depth":691,"text":501},{"id":524,"depth":691,"text":525},{"id":548,"depth":691,"text":549},{"id":584,"depth":691,"text":585},{"id":629,"depth":691,"text":630},{"id":653,"depth":691,"text":654},"md",{},true,"/modules/regression/linear-regression/content",{"title":5,"description":84},"modules/regression/linear-regression/content","ab84gGw6bFM7lT62AxOKL4zVaB1JHXawKWODOBvHugo",1792223550539]