Spaces:
Sleeping
Sleeping
Gokulnath2003
commited on
Commit
•
4ae0223
1
Parent(s):
abd84ba
Create model.py
Browse files
model.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from sklearn.model_selection import train_test_split
|
3 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
4 |
+
from sklearn.compose import ColumnTransformer
|
5 |
+
from sklearn.pipeline import Pipeline
|
6 |
+
from sklearn.ensemble import RandomForestRegressor
|
7 |
+
import joblib
|
8 |
+
|
9 |
+
# Load the dataset
|
10 |
+
url = "https://raw.githubusercontent.com/manishkr1754/CarDekho_Used_Car_Price_Prediction/main/notebooks/data/cardekho_dataset.csv"
|
11 |
+
df = pd.read_csv(url)
|
12 |
+
|
13 |
+
# Preprocessing
|
14 |
+
num_features = ['vehicle_age', 'km_driven', 'mileage', 'engine', 'max_power', 'seats']
|
15 |
+
cat_features = ['brand', 'model', 'seller_type', 'fuel_type', 'transmission_type']
|
16 |
+
|
17 |
+
# Define the target variable
|
18 |
+
X = df[num_features + cat_features]
|
19 |
+
y = df['selling_price']
|
20 |
+
|
21 |
+
# Preprocessing pipeline
|
22 |
+
numeric_transformer = StandardScaler()
|
23 |
+
onehot_transformer = OneHotEncoder(handle_unknown='ignore')
|
24 |
+
|
25 |
+
preprocessor = ColumnTransformer(
|
26 |
+
transformers=[
|
27 |
+
('num', numeric_transformer, num_features),
|
28 |
+
('cat', onehot_transformer, cat_features)
|
29 |
+
])
|
30 |
+
|
31 |
+
# Create and train the model
|
32 |
+
model = Pipeline(steps=[
|
33 |
+
('preprocessor', preprocessor),
|
34 |
+
('regressor', RandomForestRegressor(n_estimators=100, random_state=42))
|
35 |
+
])
|
36 |
+
|
37 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
38 |
+
model.fit(X_train, y_train)
|
39 |
+
|
40 |
+
# Save the model
|
41 |
+
joblib.dump(model, 'random_forest_model.pkl')
|