-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
59 lines (46 loc) · 1.49 KB
/
train_model.py
File metadata and controls
59 lines (46 loc) · 1.49 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
58
59
import pandas as pd
import numpy as np
import joblib
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
from sklearn.metrics import mean_squared_error, r2_score
data = pd.read_csv("crop_yield_data.csv")
X = data.drop("yield_tonnes", axis=1)
y = data["yield_tonnes"]
numeric_features = [
"rainfall_mm", "temperature_c", "humidity_percent",
"soil_ph", "nitrogen", "phosphorus",
"potassium", "sunlight_hours", "area_hectares"
]
categorical_features = ["crop_type"]
preprocessor = ColumnTransformer(
transformers=[
("num", StandardScaler(), numeric_features),
("cat", OneHotEncoder(handle_unknown="ignore"), categorical_features)
]
)
model = RandomForestRegressor(
n_estimators=200,
random_state=42,
n_jobs=-1
)
pipeline = Pipeline(steps=[
("preprocessor", preprocessor),
("model", model)
])
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
pipeline.fit(X_train, y_train)
y_pred = pipeline.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
# rmse = mean_squared_error(y_test, y_pred, squared=False)
r2 = r2_score(y_test, y_pred)
print(f"RMSE: {rmse:.2f}")
print(f"R² Score: {r2:.2f}")
joblib.dump(pipeline, "crop_yield_model.pkl", compress=3)
print("Model saved as crop_yield_model.pkl")