3.Design and Demonstrate Regression model to predict the rent of a house. Evaluate the performance of the model. Consider Pune_rent.csv dataset.
Sun Nov 03 2024 12:55:59 GMT+0000 (Coordinated Universal Time)
Saved by
@varuntej
#python
import pandas as pd
# Load the dataset
df = pd.read_csv('Pune_rent.csv')
# Display the first few rows and basic information
print(df.head())
print(df.describe())
print(df.info())
# Check for missing values
print(df.isnull().sum())
# Convert categorical variables to dummy/indicator variables if necessary
# For demonstration, assuming 'location', 'house_type' are categorical variables
df = pd.get_dummies(df, columns=['location', 'house_type'], drop_first=True)
from sklearn.model_selection import train_test_split
# Separate features and target
X = df.drop(columns=['Rent'])
y = df['Rent']
# Split into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
from sklearn.preprocessing import StandardScaler
# Standardize the features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
from sklearn.linear_model import LinearRegression
# Initialize and train the model
model = LinearRegression()
model.fit(X_train_scaled, y_train)
# Predict on the test set
y_pred = model.predict(X_test_scaled)
from sklearn.metrics import mean_absolute_error, mean_squared_error
import numpy as np
# Calculate MAE and RMSE
mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print("Mean Absolute Error (MAE):", mae)
print("Root Mean Squared Error (RMSE):", rmse)
content_copyCOPY
Comments