Building Your First Machine Learning Model with Python
Building Your First Machine Learning Model with Python
In this lesson, you will build a simple classification model using Python and scikit-learn.
The Iris dataset contains measurements of flowers and labels identifying their species. It is commonly used to demonstrate basic machine learning workflows.
Install the Required Library
Install scikit-learn in your Python environment:
pip install scikit-learn
Load the Dataset
Scikit-learn provides a built-in Iris dataset, so you do not need to download a separate data file.
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
print("Feature shape:", X.shape)
print("Target shape:", y.shape)
print("Feature names:", iris.feature_names)The variable X contains the input measurements, while y contains the corresponding class labels.
The dataset contains 150 observations, four numerical features, and three target classes.
Split the Dataset
The model should be evaluated on observations that were not used to fit it.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42,
stratify=y
)
print("Training examples:", len(X_train))
print("Testing examples:", len(X_test))The code allocates 80% of the observations to training and 20% to testing. stratify=y helps preserve the class proportions in each subset, and random_state=42 makes the split reproducible.
Train a Classification Model
Next, create and train a logistic regression model.
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(max_iter=200)
model.fit(X_train, y_train)The fit() method learns model parameters from the training examples.
The max_iter parameter allows the optimization procedure more iterations to converge if necessary.
Make Predictions
Use the trained model to predict the classes of the test observations.
predictions = model.predict(X_test)
print("Predictions:", predictions[:10])
print("Actual labels:", y_test[:10])The predicted values can now be compared with the actual labels.
Evaluate the Model
Accuracy measures the proportion of predictions that are correct.
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, predictions)
print("Accuracy:", round(accuracy, 3))Accuracy is useful when the classes and error costs make it an appropriate metric. For imbalanced datasets, additional measures such as precision, recall, F1-score, and a confusion matrix may provide a more informative evaluation.
The exact accuracy can vary with the data split, model settings, and library version. A high score on this small educational dataset does not guarantee similar performance on a different dataset or in a real-world application.
Make a Prediction for a New Example
You can pass a new flower's measurements to the trained model.
new_flower = [[5.1, 3.5, 1.4, 0.2]]
prediction = model.predict(new_flower)
print("Predicted species:", iris.target_names[prediction[0]])
The four values represent the flower's sepal length, sepal width, petal length, and petal width, in the dataset's measurement units.
The model returns a predicted class based on the patterns learned during training.
What Did You Build?
You created a basic supervised machine learning pipeline:
- Loaded a labeled dataset.
- Separated the features and target.
- Split the observations into training and testing sets.
- Trained a classification model.
- Evaluated its predictions.
- Used the model to predict a new example.
This workflow provides a foundation for experimenting with more complex datasets and algorithms.
Artificial Intelligence Tutorial
Vishalini A








