Setup and First ML Model
Before writing any ML code, set up your environment. This chapter shows you how to create a clean workspace and run your first scikit‑learn example.
Step 1: Install Python and Create a Virtual Environment (Recommended)
# Create and activate a virtual environment
python -m venv ml-env
# On Windows:
ml-envScriptsactivate
# On Mac/Linux:
source ml-env/bin/activateStep 2: Install Required Libraries
pip install numpy pandas scikit-learn matplotlibStep 3: Run Your First ML Model (Linear Regression)
Create a file `first_model.py`:
import numpy as np
from sklearn.linear_model import LinearRegression
# Sample data: years of experience → salary
X = np.array([[1], [2], [3], [4]]) # features (must be 2D)
y = np.array([30000, 35000, 40000, 45000])
model = LinearRegression()
model.fit(X, y)
pred = model.predict([[5]])
print(f"Predicted salary for 5 years: {pred[0]:.0f}")Run it:python first_model.pyYou should see a predicted salary (e.g., 50000).What Just Happened?
Xis input (years of experience).yis target (salary).LinearRegression().fit(X, y)learns a line that best fits the data.predictuses that line to estimate salary for 5 years.
Two Minute Drill
- Use a virtual environment to keep dependencies isolated.
- Install
numpy,pandas,scikit‑learn,matplotlib. - First model: linear regression on toy data.
- Congratulations – you have run your first ML code!
Need more clarification?
Drop us an email at career@quipoinfotech.com
