import pandas as pd
# Load the dataset
df = pd.read_csv('student_scores.csv')
# Display the first few rows and basic information
print(df.head())
print(df.describe())
print(df.info())
from sklearn.model_selection import train_test_split
# Separate features and target
X = df.drop(columns=['Score']) # Assuming 'Score' is the target column
y = df['Score']
# 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
# Initialize and fit the scaler on training data
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error
import numpy as np
# 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)
# Evaluate performance
mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print("Without Dimensionality Reduction")
print("Mean Absolute Error (MAE):", mae)
print("Root Mean Squared Error (RMSE):", rmse)
from sklearn.decomposition import PCA
# Initialize PCA and fit on the scaled training data
pca = PCA(n_components=0.95) # Retain 95% variance
X_train_pca = pca.fit_transform(X_train_scaled)
X_test_pca = pca.transform(X_test_scaled)
# Check the number of components
print("Number of components selected:", pca.n_components_)
# Train the regression model on PCA-reduced data
model_pca = LinearRegression()
model_pca.fit(X_train_pca, y_train)
# Predict on the PCA-transformed test set
y_pred_pca = model_pca.predict(X_test_pca)
# Evaluate performance
mae_pca = mean_absolute_error(y_test, y_pred_pca)
rmse_pca = np.sqrt(mean_squared_error(y_test, y_pred_pca))
print("With Dimensionality Reduction (PCA)")
print("Mean Absolute Error (MAE):", mae_pca)
print("Root Mean Squared Error (RMSE):", rmse_pca)
print("Comparison of Model Performance:")
print("Without Dimensionality Reduction - MAE:", mae, ", RMSE:", rmse)
print("With Dimensionality Reduction (PCA) - MAE:", mae_pca, ", RMSE:", rmse_pca)
Comments