Spaces:
Paused
Paused
| import pandas as pd | |
| import numpy as np | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.preprocessing import StandardScaler | |
| from sklearn.ensemble import RandomForestRegressor | |
| from sklearn.metrics import mean_squared_error, r2_score | |
| import joblib | |
| # Load the synthetic data | |
| df = pd.read_csv('dairy_farm_synthetic_data.csv') | |
| # Prepare features and target | |
| features = ['protein_content', 'fiber_content', 'energy_content', 'body_condition_score', | |
| 'somatic_cell_count', 'temperature', 'humidity', 'num_cows'] | |
| target = 'milk_production' | |
| X = df[features] | |
| y = df[target] | |
| # Split the data | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| # Scale the features | |
| scaler = StandardScaler() | |
| X_train_scaled = scaler.fit_transform(X_train) | |
| X_test_scaled = scaler.transform(X_test) | |
| # Train a Random Forest model | |
| model = RandomForestRegressor(n_estimators=100, random_state=42) | |
| model.fit(X_train_scaled, y_train) | |
| # Make predictions on the test set | |
| y_pred = model.predict(X_test_scaled) | |
| # Evaluate the model | |
| mse = mean_squared_error(y_test, y_pred) | |
| r2 = r2_score(y_test, y_pred) | |
| print(f"Mean Squared Error: {mse}") | |
| print(f"R-squared Score: {r2}") | |
| # Save the model and scaler | |
| joblib.dump(model, 'milk_production_model.joblib') | |
| joblib.dump((scaler, features), 'feature_scaler.joblib') | |
| print("Model and scaler saved successfully.") | |
| def predict_milk_production(protein_content, fiber_content, energy_content, body_condition_score, | |
| somatic_cell_count, temperature, humidity, num_cows): | |
| # Load the saved model and scaler | |
| loaded_model = joblib.load('milk_production_model.joblib') | |
| loaded_scaler, features = joblib.load('feature_scaler.joblib') | |
| # Prepare the input data | |
| input_data = pd.DataFrame([[protein_content, fiber_content, energy_content, body_condition_score, | |
| somatic_cell_count, temperature, humidity, num_cows]], | |
| columns=features) | |
| # Scale the input data | |
| input_data_scaled = loaded_scaler.transform(input_data) | |
| # Make the prediction | |
| prediction = loaded_model.predict(input_data_scaled) | |
| return prediction[0] | |
| # Example usage | |
| # print(predict_milk_production(16.5, 20, 1.65, 3.5, 150000, 22, 60, 200)) |