-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_preprocessing.py
More file actions
15 lines (15 loc) · 5.89 KB
/
Copy pathfix_preprocessing.py
File metadata and controls
15 lines (15 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import pickle\nimport pandas as pd\nimport numpy as np\nimport logging\n\n# Configure logging\nlogging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')\n\n# Load model and scaler\nwith open('vehicle_load_model.pkl', 'rb') as f:\n model = pickle.load(f)\n\nwith open('vehicle_load_scaler.pkl', 'rb') as f:\n scaler = pickle.load(f)\n\n# Print model features\nprint('Model expects these features:')\nfor feature in model.feature_names_in_:\n print(f'- {feature}')\n\n# Create test data\ntest_data = {\n 'vehicle_type': '2-wheeler',\n 'weight': 100,\n 'max_load_capacity': 150,\n 'passenger_count': 2,\n 'cargo_weight': 20,\n 'region': 'Urban',\n 'road_condition': 'Good',\n 'weather': 'Clear'\n}\n\n# Fixed preprocessing function\ndef fixed_preprocess_data(data):\n \
\\Fixed
preprocessing
function
that
ensures
feature
names
match
training
data.\\\\n # Convert to DataFrame if dict\n if isinstance(data, dict):\n df = pd.DataFrame([data])\n else:\n df = pd.DataFrame(data)\n \n # Rename 'weather' to 'weather_condition' if needed\n if 'weather' in df.columns and 'weather_condition' not in df.columns:\n df['weather_condition'] = df['weather']\n df = df.drop('weather', axis=1)\n \n # Define numerical features (these need to be scaled)\n numerical_features = ['weight', 'max_load_capacity', 'passenger_count', 'cargo_weight']\n \n # Ensure all numerical features exist\n for feature in numerical_features:\n if feature not in df.columns:\n df[feature] = 0\n \n # Scale numerical features\n numerical_data = df[numerical_features].values\n scaled_data = scaler.transform(numerical_data)\n \n # Create a new DataFrame with just the features the model expects\n result_df = pd.DataFrame()\n \n # Add scaled numerical features\n for i, feature in enumerate(numerical_features):\n result_df[f\
feature
_scaled\] = scaled_data[:, i]\n \n # Handle one-hot encoding for vehicle_type\n vehicle_types = ['2-wheeler', '4-wheeler 5-seater', '4-wheeler 7-seater', 'delivery vehicle', 'heavy vehicle']\n current_type = df['vehicle_type'].iloc[0] if 'vehicle_type' in df.columns else None\n \n for vtype in vehicle_types:\n result_df[f\vehicle_type_
vtype
\] = 1 if current_type == vtype else 0\n \n # Make sure columns are in the same order as the model expects\n result_df = result_df[model.feature_names_in_]\n \n return result_df\n\n# Process test data\ntry:\n processed_data = fixed_preprocess_data(test_data)\n \n # Print processed data\n print('\\nProcessed data:')\n print(processed_data)\n \n # Make prediction\n prediction = model.predict(processed_data)\n print(f'\\nPrediction: {prediction[0]}')\n \n print('\\nThis fixed preprocessing function works! Update app.py to use this approach.')\nexcept Exception as e:\n print(f'Error: {e}')