Spaces:
Build error
Build error
| import numpy as np | |
| import pandas as pd | |
| from sklearn.datasets import fetch_california_housing | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.preprocessing import StandardScaler | |
| # Load dataset | |
| data = fetch_california_housing() | |
| X = pd.DataFrame(data.data, columns=data.feature_names) | |
| y = pd.Series(data.target) | |
| # Split dataset | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| # Standardize the features | |
| scaler = StandardScaler() | |
| X_train = scaler.fit_transform(X_train) | |
| X_test = scaler.transform(X_test) | |
| from sklearn.ensemble import GradientBoostingRegressor | |
| # Initialize and train GBM | |
| gbm = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42) | |
| gbm.fit(X_train, y_train) | |
| import xgboost as xgb | |
| # Initialize and train XGBoost | |
| xgb_model = xgb.XGBRegressor(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42) | |
| xgb_model.fit(X_train, y_train) | |
| import lightgbm as lgb | |
| # Initialize and train LightGBM | |
| lgb_model = lgb.LGBMRegressor(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42) | |
| lgb_model.fit(X_train, y_train) | |
| from sklearn.metrics import accuracy_score | |
| # Make predictions | |
| gbm_preds = gbm.predict(X_test) | |
| xgb_preds = xgb_model.predict(X_test) | |
| lgb_preds = lgb_model.predict(X_test) | |
| accuracy_gbm = gbm.score(X_test, y_test) | |
| accuracy_xgb = xgb_model.score(X_test, y_test) | |
| accuracy_lgb = lgb_model.score(X_test, y_test) | |
| print(f'Accuracy of GBM: {accuracy_gbm:.2f}') | |
| print(f'Accuracy of XGBoost: {accuracy_xgb:.2f}') | |
| print(f'Accuracy of LightGBM: {accuracy_lgb:.2f}') | |
| from sklearn.metrics import mean_squared_error | |
| # Make predictions | |
| gbm_preds = gbm.predict(X_test) | |
| xgb_preds = xgb_model.predict(X_test) | |
| lgb_preds = lgb_model.predict(X_test) | |
| # Average predictions | |
| hybrid_preds = (gbm_preds + xgb_preds + lgb_preds) / 3 | |
| # Evaluate performance | |
| mse = mean_squared_error(y_test, hybrid_preds) | |
| print(f'Mean Squared Error of Hybrid Model: {mse:.2f}') | |
| import gradio as gr | |
| def predict(*features): | |
| # Convert input to DataFrame | |
| features_df = pd.DataFrame([features], columns=X.columns) | |
| features_df = scaler.transform(features_df) | |
| # Make predictions | |
| gbm_pred = gbm.predict(features_df)[0] | |
| xgb_pred = xgb_model.predict(features_df)[0] | |
| lgb_pred = lgb_model.predict(features_df)[0] | |
| # Combine predictions | |
| hybrid_pred = (gbm_pred + xgb_pred + lgb_pred) / 3 | |
| return f"GBM Prediction: ${gbm_pred:.2f}\nXGBoost Prediction: ${xgb_pred:.2f}\nLightGBM Prediction: ${lgb_pred:.2f}\nHybrid Prediction: ${hybrid_pred:.2f}" | |
| # Create Gradio interface | |
| inputs = [gr.Slider(minimum=float(X[col].min()), maximum=float(X[col].max()), value=float(X[col].mean()), label=col) for col in X.columns] | |
| interface = gr.Interface( | |
| fn=predict, | |
| inputs=inputs, | |
| outputs="text", | |
| title="Housing Price Prediction", | |
| description="Enter values for each feature to get predictions from the ensemble model." | |
| ) | |
| # Launch the interface | |
| interface.launch(share=True,inline=False) |