Train a Linear Regression
A complete end-to-end example.
Train a Linear Regression is a free Data Science Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A Line Through Data
Linear regression fits a straight-line relationship between your features and a numeric target. It is the classic first model for a reason. 📈
Import the Model
The estimator lives in the linear_model module. You import it once and reuse it for any regression task you meet.
from sklearn.linear_model import LinearRegressionCreate an Instance
Build a fresh model by calling the class. This blank estimator holds default settings and has not seen any data yet.
model = LinearRegression()Fit on Training Data
Now teach it with your features and target. During fit, the model finds the best line through your training points.
model.fit(X_train, y_train)Read the Slope
Each feature gets a coefficient stored in coef_. It tells you how much the prediction moves when that feature rises by one.
model.coef_Read the Intercept
The intercept is the prediction when every feature is zero. It anchors the line and is stored in intercept_ after fitting.
model.intercept_Make Predictions
Hand new feature rows to predict and you get numeric estimates back, one per row, computed straight from the fitted line.
y_pred = model.predict(X_test)The Equation Behind It
Under the hood, each prediction is just features times coefficients plus the intercept. Simple math, surprisingly powerful results.
One Feature or Many
The same call handles a single feature or dozens. With many inputs it fits a hyperplane, but your code stays exactly the same.
Linear Means a Straight Fit
Linear regression assumes a roughly straight relationship. If the pattern curves sharply, this model will underfit and miss it.
The Full Workflow
Import, create, fit, predict: four steps and you have a working regressor. This same rhythm repeats for every model you learn next.
Quick Check
Which attribute holds the per-feature weights after fitting?
Recap
You imported, created, fit, and predicted with a linear regression. Its coef_ and intercept_ even reveal what it learned. 🚀
Frequently asked questions
Is the “Train a Linear Regression” lesson free?
Yes — the full text of “Train a Linear Regression” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “Train a Linear Regression”?
A complete end-to-end example. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Train a Linear Regression” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Data Science Academy lesson?
Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The fit and predict Contract
- Features X and Target y
- Train a Linear Regression
- Score Your First Model