Building Your First Machine Learning Model in Python
Anitha Rajesh
Python | AI & ML | Cloud DevOps Trainer | Career & Branding Strategist | Helping Businesses & Individuals Grow with Tech & Innovation
Machine learning (ML) is revolutionizing industries by enabling computers to learn from data and make predictions. If you're new to ML, Python is the perfect language to start with due to its simplicity and powerful libraries. In this guide, you'll learn how to build your first machine learning model using Python.
Step 1: Install Necessary Libraries
Before building a machine learning model, ensure you have the required Python libraries installed. You can install them using:
python
pip install numpy pandas scikit-learn matplotlib seaborn
These libraries help with data handling, model training, and visualization.
Step 2: Import Essential Libraries
Start by importing the necessary Python libraries:
python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error
Step 3: Load and Explore the Dataset
For this tutorial, we’ll use a sample dataset from scikit-learn:
python
from sklearn.datasets import load_boston
boston = load_boston()
df = pd.DataFrame(boston.data, columns=boston.feature_names)
df['PRICE'] = boston.target
Check the first few rows of the dataset:
python
print(df.head())
Step 4: Preprocess the Data
Splitting the dataset into training and testing sets:
python
X = df.drop(columns=['PRICE'])
y = df['PRICE'] X_train,
X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Step 5: Train a Machine Learning Model
We'll use Linear Regression, a fundamental supervised learning algorithm:
python
model = LinearRegression()
model.fit(X_train, y_train)
Step 6: Make Predictions
After training, test the model on new data:
python
y_pred = model.predict(X_test)
Step 7: Evaluate the Model
Assess model performance using common metrics:
python
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Absolute Error: {mae}")
print(f"Mean Squared Error: {mse}")
Step 8: Visualize Results
Plot actual vs. predicted prices:
python
plt.scatter(y_test, y_pred)
plt.xlabel("Actual Prices")
plt.ylabel("Predicted Prices")
plt.title("Actual vs. Predicted Prices")
plt.show()
Conclusion
Congratulations! You've built your first machine learning model using Python. By following these steps, you have learned how to:
As you advance, explore more algorithms like Decision Trees, Random Forests, and Neural Networks to enhance your models.
If you have any python related queries please feel free to reach out Anitha Rajesh .
?? Passionate Math Educator | Expert Tutor from Junior to Advanced Levels ?? | Data Science | Python |Transforming Learning & Inspiring Success! ???? #MathEducation
2 周Interesting Anitha Rajesh
?? Aspiring Cloud DevOps Engineer | ?? Azure cloud |?? Ansible | ??? Terraform | ?? Docker | ?? Kubernetes | ?? Jenkins |?? C/C++
2 周Very informative
Python | AI & ML | Cloud DevOps Trainer | Career & Branding Strategist | Helping Businesses & Individuals Grow with Tech & Innovation
2 周#connections