Salary Regression with Python and scikit-learn
A beginner-friendly machine learning example in Python: a scikit-learn linear regression pipeline that predicts salary from years of experience, with a separate training script that saves the model and scaler with joblib, and an inference script that loads them to make predictions.
Python projects · Machine learning and regression
Start here
Read the requirements and any configuration below, then follow the usage example. Source paths below are relative to the repository root; run commands from the working directory stated in Usage. This example’s source is in ML/Salary/Model Builder/. Install any third-party packages in an isolated Python environment.
Overview
This project has two scripts:
Model Builder/builder.py— cleans data, trains the model, evaluates it, and saves it to diskPrediction/prediction.py— loads the saved model and predicts salary from user input
Prerequisites and installation
pandas
scikit-learn
joblib
numpy
Install dependencies:
python -m pip install pandas scikit-learn joblib numpy
Scripts
Model Builder (builder.py)
A teaching pipeline that processes a CSV dataset and conditionally saves a trained model; see the evaluation limitations below.
Pipeline Steps
| Step | Action |
|---|---|
| 1 | Load dataset from Source/Salary.csv |
| 2 | Remove duplicate rows |
| 3 | Keep columns with at least int(0.5 * row_count) non-null values; fill numeric nulls with column mean |
| 4 | Scale years_experience using StandardScaler |
| 5 | Split into 80% train / 20% test sets |
| 6 | Train LinearRegression model |
| 7 | Evaluate with R² score and Mean Squared Error |
| 8 | Save model and scaler if R² > 0.95 |
Usage
Both scripts open files with paths relative to the working directory (./Source/...), so run them from the Salary folder:
python "Model Builder/builder.py"
Output format:
R² Score: <score to 4 decimal places>
Mean Squared Error: <mse to 2 decimal places>
Model and scaler saved!
If R² ≤ 0.95, the model is not saved and Model not saved as R² score is below 95%. is printed instead.
Salary Predictor (prediction.py)
Loads the saved model and scaler, accepts user input, and outputs a predicted salary.
Usage
From the Salary folder:
python "Prediction/prediction.py"
Session format:
Enter Years of Experience: 7
Predicted Salary for 7.0 years of experience: $<predicted amount>
Architecture
Salary/
├── Model Builder/
│ └── builder.py # Training pipeline
├── Prediction/
│ └── prediction.py # Inference script
└── Source/
├── Salary.csv # Training dataset (205 rows: years_experience, salary)
├── salary.pkl # Saved model (included; regenerated by training)
└── scaler.pkl # Saved scaler (included; regenerated by training)
Troubleshooting and limitations
- Only load model files you trust:
joblib.load()can execute code from serialized artifacts. Prefer retraining from the inspected source and CSV, and retain matching package versions. No dependency lockfile identifies the versions used for the bundled artifacts. - A trained
salary.pklandscaler.pklare already included, soprediction.pyworks without training first. Runbuilder.pyto retrain them. If your scikit-learn version differs from the one that created the files, retraining avoids version-mismatch warnings when loading them. - The dataset must contain
years_experienceandsalarycolumns. - The model is only saved when the test-set R² exceeds 0.95. This is a demo’s save condition, not a guarantee of prediction quality on new data.
- The current script fills missing values and fits its scaler before the train/test split. For an independent evaluation, split first and fit preprocessing only on the training data, then transform the held-out data with those fitted steps. The current order allows held-out data to influence preprocessing.
The inference prompt converts input to float without validating finite or non-negative experience. Its error handling does not cover invalid input, missing/incompatible artifacts or prediction failures. The included joblib files contain a LinearRegression model and StandardScaler; rebuilding them with your selected dependency versions is preferable to assuming cross-version compatibility.
Related documentation
- COVID-19 time series forecasting
- Machine-learning examples overview
Salary.csvand other practice datasets- Back to all Python projects
Documentation links
Read all Python Projects and Code Examples guides online · Project overview · All developer projects