Introduction
This blog post aims to guide you through the process of building a machine learning model from scratch using Python. We will focus on a simple yet powerful algorithm, the Decision Tree, to predict housing prices in Boston.
Prerequisites
Before we dive into the code, ensure you have the following prerequisites:
1. Python installed on your machine
2. A text editor or IDE of your choice (e.g., Jupyter Notebook, PyCharm, Visual Studio Code)
3. Dataset: Boston Housing dataset available in scikit-learn library or any other suitable dataset
Step 1: Importing Libraries
“`python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error, r2_score
“`
Step 2: Loading the Dataset
“`python
data = pd.read_csv(‘boston_housing.csv’)
“`
Step 3: Preparing the Data
“`python
X = data.drop(‘PRICE’, axis=1)
y = data[‘PRICE’]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
“`
Step 4: Building the Model
“`python
model = DecisionTreeRegressor(random_state=42)
model.fit(X_train, y_train)
“`
Step 5: Making Predictions and Evaluating the Model
“`python
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f”Mean Squared Error: {mse}”)
print(f”R^2 Score: {r2}”)
“`
Conclusion
In this blog post, we built a simple machine learning model from scratch using Python and the Decision Tree algorithm. Although this example is quite basic, it showcases the power and simplicity of Python for AI tasks. As you advance in your machine learning journey, you can explore more complex models, data preprocessing techniques, and optimization methods to make accurate predictions and solve real-world problems. Happy coding!