0% found this document useful (0 votes)
2 views7 pages

AI Using Python 3

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

AI Using Python 3

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

AI Using Python

Hands-on Exercise No. 3

Task 1

Task 2
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

# Load dataset
df = pd.read_csv("[Link]")

# Remove Id column if it exists


if "Id" in [Link]:
df = [Link]("Id", axis=1)

# Fill missing values in numerical columns with mean


numeric_columns = df.select_dtypes(include=["number"]).columns
for col in numeric_columns:
df[col] = df[col].fillna(df[col].mean())

# Fill missing values in categorical columns with mode


categorical_columns = df.select_dtypes(include=["object"]).columns
for col in categorical_columns:
if not df[col].mode().empty:
df[col] = df[col].fillna(df[col].mode()[0])

# Convert categorical columns into numerical columns


df = pd.get_dummies(df, drop_first=True)

# Fill any remaining missing values with 0


df = [Link](0)

# Separate features and target


X = [Link]("SalePrice", axis=1)
y = df["SalePrice"]

# Split dataset (80% training, 20% testing)


X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)

# Create and train the Linear Regression model


model = LinearRegression()
[Link](X_train, y_train)

print("Linear Regression model trained successfully!")

# Display training and testing shapes


print("Training Features:", X_train.shape)
print("Testing Features:", X_test.shape)
Task 3

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

# Load dataset
df = pd.read_csv("[Link]")

# Remove Id column if it exists


if "Id" in [Link]:
df = [Link]("Id", axis=1)

# Fill missing values


numeric_columns = df.select_dtypes(include=["number"]).columns
for col in numeric_columns:
df[col] = df[col].fillna(df[col].mean())

categorical_columns = df.select_dtypes(include=["object"]).columns
for col in categorical_columns:
if not df[col].mode().empty:
df[col] = df[col].fillna(df[col].mode()[0])
# Convert categorical data into numbers
df = pd.get_dummies(df, drop_first=True)

# Remove any remaining missing values


df = [Link](0)

# Features and Target


X = [Link]("SalePrice", axis=1)
y = df["SalePrice"]

# Split dataset
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)

# Train model
model = LinearRegression()
[Link](X_train, y_train)

# Make predictions
predictions = [Link](X_test)

print("First 10 Predicted House Prices:")


print(predictions[:10])
Task 4: Model Evaluation

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_absolute_error, mean_squared_error, r2_score
import math

# Load dataset
df = pd.read_csv("[Link]")

# Remove Id column if it exists


if "Id" in [Link]:
df = [Link]("Id", axis=1)

# Fill missing values in numerical columns


numeric_columns = df.select_dtypes(include=["number"]).columns
for col in numeric_columns:
df[col] = df[col].fillna(df[col].mean())

# Fill missing values in categorical columns


categorical_columns = df.select_dtypes(include=["object"]).columns
for col in categorical_columns:
if not df[col].mode().empty:
df[col] = df[col].fillna(df[col].mode()[0])

# Convert categorical variables into numerical form


df = pd.get_dummies(df, drop_first=True)

# Fill any remaining missing values


df = [Link](0)

# Features and Target


X = [Link]("SalePrice", axis=1)
y = df["SalePrice"]

# Split dataset
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)

# Train the model


model = LinearRegression()
[Link](X_train, y_train)

# Predict
y_pred = [Link](X_test)
# Evaluation
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = [Link](mse)
r2 = r2_score(y_test, y_pred)

print("Model Evaluation")
print("----------------")
print("MAE :", mae)
print("MSE :", mse)
print("RMSE:", rmse)
print("R² Score:", r2)

Task 5: Interpretation

The Linear Regression model was trained and evaluated successfully. The model
produced the following evaluation metrics:

 Mean Absolute Error (MAE): 32777.02


 Mean Squared Error (MSE): 2368321830.07
 Root Mean Squared Error (RMSE): 48665.41
 R² Score: 0.336

The R² Score of approximately 0.336 indicates that the model explains about 33.6%
of the variation in house prices. This means the model has moderate predictive
performance and can be improved by using better feature selection, additional data
preprocessing, or more advanced machine learning algorithms.

You might also like