-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode implementation on random values
More file actions
57 lines (44 loc) · 1.63 KB
/
code implementation on random values
File metadata and controls
57 lines (44 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error
import joblib
np.random.seed(42)
n_samples = 500
data = pd.DataFrame({
"day_of_week": np.random.randint(0, 7, n_samples),
"month": np.random.randint(1, 13, n_samples),
"local_event": np.random.choice([0, 1], n_samples),
"competitor_avg_price": np.random.randint(50, 200, n_samples),
"occupancy_last_week": np.random.uniform(0.3, 1.0, n_samples),
"days_to_booking": np.random.randint(0, 90, n_samples),
"base_price": np.random.randint(60, 150, n_samples),
})
data["optimal_price"] = (
data["base_price"]
+ (data["local_event"] * 1000)
+ ((1 - data["occupancy_last_week"]) * 1000)
+ np.random.normal(0, 5, n_samples)
)
features = data.drop("optimal_price", axis=1)
target = data["optimal_price"]
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2)
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
mae = mean_absolute_error(y_test, predictions)
print(f"Mean Absolute Error: rs{mae:.2f}")
joblib.dump(model, "pricepilot_model.pkl")
# Step 6: Sample Prediction
sample_input = pd.DataFrame([{
"day_of_week": 5, # Saturday
"month": 7, # July
"local_event": 1,
"competitor_avg_price": 140,
"occupancy_last_week": 0.6,
"days_to_booking": 10,
"base_price": 100,
}])
predicted_price = model.predict(sample_input)[0]
print(f"Suggested Price for Listing: rs{predicted_price:.2f}")