import pandas as pd from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler, OneHotEncoder from sklearn.compose import ColumnTransformer from sklearn.pipeline import Pipeline from sklearn.ensemble import RandomForestRegressor import joblib # Load the dataset url = "https://raw.githubusercontent.com/manishkr1754/CarDekho_Used_Car_Price_Prediction/main/notebooks/data/cardekho_dataset.csv" df = pd.read_csv(url) # Preprocessing num_features = ['vehicle_age', 'km_driven', 'mileage', 'engine', 'max_power', 'seats'] cat_features = ['brand', 'model', 'seller_type', 'fuel_type', 'transmission_type'] # Define the target variable X = df[num_features + cat_features] y = df['selling_price'] # Preprocessing pipeline numeric_transformer = StandardScaler() onehot_transformer = OneHotEncoder(handle_unknown='ignore') preprocessor = ColumnTransformer( transformers=[ ('num', numeric_transformer, num_features), ('cat', onehot_transformer, cat_features) ]) # Create and train the model model = Pipeline(steps=[ ('preprocessor', preprocessor), ('regressor', RandomForestRegressor(n_estimators=100, random_state=42)) ]) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) model.fit(X_train, y_train) # Save the model joblib.dump(model, 'random_forest_model.pkl')