0% found this document useful (0 votes)
6 views4 pages

Task 2

The document outlines a procedure to implement a Linear Regression model in Python to predict a person's age based on attributes such as Height, Weight, and Gender. It includes steps for data collection, preprocessing, visualization, model training, and evaluation, along with sample code. The results demonstrate the model's performance metrics and provide a prediction for a new input data point.

Uploaded by

John Mesia Dhas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Task 2

The document outlines a procedure to implement a Linear Regression model in Python to predict a person's age based on attributes such as Height, Weight, and Gender. It includes steps for data collection, preprocessing, visualization, model training, and evaluation, along with sample code. The results demonstrate the model's performance metrics and provide a prediction for a new input data point.

Uploaded by

John Mesia Dhas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Task 2: Implement the Linear Regression model to predict the age of the person.

Consider the
dataset with the following attributes
i)Height
ii)Weight
iii)Gender
Tool: Python

Aim
To implement a Linear Regression model that predicts a person's Age based on the attributes Height,
Weight, and Gender, and visualize the data using scatter plots and a line graph for better
understanding.

Procedure

1. Data Collection: We'll create a sample dataset with the attributes Height, Weight,
Gender, and Age.
2. Data Preprocessing: We'll handle categorical data (e.g., Gender) using encoding
techniques.
3. Data Visualization: We'll plot scatter plots for visualizing the relationships between
the features (Height, Weight, Gender) and the target (Age).
4. Linear Regression Model: We'll train a linear regression model to predict Age.
5. Visualization of Predictions: We'll use a line graph to show how the predicted age
changes with the input features.

Program

# Import necessary libraries


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error,
r2_score
from sklearn.preprocessing import LabelEncoder

# Step 1: Create a sample dataset (you can replace this with your own
dataset)
data = {
'Height': [160, 170, 180, 165, 155, 190, 175, 168],
'Weight': [60, 70, 80, 65, 55, 90, 75, 68],
'Gender': ['Male', 'Female', 'Female', 'Male', 'Female', 'Male',
'Female', 'Male'],
'Age': [25, 30, 35, 28, 22, 40, 33, 27]
}
# Convert data into a pandas DataFrame
df = pd.DataFrame(data)

# Step 2: Data Preprocessing


# Encode 'Gender' column (Male=1, Female=0) using LabelEncoder
label_encoder = LabelEncoder()
df['Gender'] = label_encoder.fit_transform(df['Gender'])

# Step 3: Split the data into features (X) and target (y)
X = df[['Height', 'Weight', 'Gender']] # Features
y = df['Age'] # Target variable

# Step 4: Split the data into training and testing sets (80% training,
20% testing)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)

# Step 5: Initialize the Linear Regression model


model = LinearRegression()

# Step 6: Train the model using the training data


model.fit(X_train, y_train)

# Step 7: Predict the Age on the test data


y_pred = model.predict(X_test)

# Step 8: Evaluate the model


mae = mean_absolute_error(y_test, y_pred) # Mean Absolute Error
mse = mean_squared_error(y_test, y_pred) # Mean Squared Error
r2 = r2_score(y_test, y_pred) # R-squared (coefficient of
determination)

# Print the evaluation metrics


print("Model Coefficients:", model.coef_)
print("Model Intercept:", model.intercept_)
print("Mean Absolute Error (MAE):", mae)
print("Mean Squared Error (MSE):", mse)
print("R-squared (R²):", r2)

# Step 9: Visualization

# Scatter Plot for Height vs Age


plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.scatter(df['Height'], df['Age'], color='blue', label='Height vs
Age')
plt.title('Height vs Age')
plt.xlabel('Height (cm)')
plt.ylabel('Age')
plt.grid(True)

# Scatter Plot for Weight vs Age


plt.subplot(1, 2, 2)
plt.scatter(df['Weight'], df['Age'], color='green', label='Weight vs
Age')
plt.title('Weight vs Age')
plt.xlabel('Weight (kg)')
plt.ylabel('Age')
plt.grid(True)

plt.tight_layout()
plt.show()

# Line Graph for Predicted Age vs Actual Age (test set)


plt.figure(figsize=(8, 6))
plt.plot(y_test.values, color='blue', marker='o', label='Actual Age')
plt.plot(y_pred, color='red', marker='x', linestyle='dashed',
label='Predicted Age')
plt.title('Actual vs Predicted Age')
plt.xlabel('Test Sample Index')
plt.ylabel('Age')
plt.legend()
plt.grid(True)
plt.show()

# Step 10: Make a prediction for a new input (Example: Height=172,


Weight=68, Gender=Female)
new_data = pd.DataFrame({'Height': [172], 'Weight': [68], 'Gender':
[label_encoder.transform(['Female'])[0]]})
predicted_age = model.predict(new_data)
print("Predicted Age for the new data:", predicted_age[0])

Output
Model Coefficients: [ 0.25391986 0.25391986 -0.45557491]
Model Intercept: -30.940766550522664
Mean Absolute Error (MAE): 0.14939024390244704
Mean Squared Error (MSE): 0.04463488994646417
R-squared (R²): 0.9982146044021414
Predicted Age for the new data: 30.000000000000004

Result
By following this procedure, you can implement a Linear Regression model in Python to predict the
age of a person based on the features Height, Weight, and Gender, and use scatter plots and line graphs
for visualizing the data and predictions.

You might also like