Spaces:
Build error
Build error
shanmukakomal commited on
Commit ·
f09667c
1
Parent(s): 846b098
smallUpadte
Browse files
app.py
CHANGED
|
@@ -17,28 +17,53 @@ def load_and_preprocess_data(filename):
|
|
| 17 |
label_encoders[col] = le
|
| 18 |
|
| 19 |
X = df[["Category", "Gender", "Opening Rank", "Closing Rank", "Region"]]
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
return X,
|
| 23 |
|
| 24 |
-
filename = "AP_EAMCET_Engineering_10000 (1).csv"
|
| 25 |
-
X,
|
| 26 |
|
| 27 |
-
# Train model
|
| 28 |
-
def train_model(X, y):
|
| 29 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 30 |
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 31 |
model.fit(X_train, y_train)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
return model
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
joblib.dump(label_encoders, "label_encoders.pkl")
|
| 38 |
|
| 39 |
# Prediction function
|
| 40 |
def predict_colleges(category, gender, rank, region):
|
| 41 |
-
# Load label encoders
|
|
|
|
|
|
|
| 42 |
label_encoders = joblib.load("label_encoders.pkl")
|
| 43 |
|
| 44 |
# Transform input values using label encoders
|
|
@@ -49,7 +74,7 @@ def predict_colleges(category, gender, rank, region):
|
|
| 49 |
except ValueError:
|
| 50 |
return "Invalid input values. Please select valid options."
|
| 51 |
|
| 52 |
-
# Filter
|
| 53 |
filtered_df = df[
|
| 54 |
(df["Category"] == category_enc) &
|
| 55 |
(df["Gender"] == gender_enc) &
|
|
@@ -82,4 +107,4 @@ demo = gr.Interface(
|
|
| 82 |
description="Enter your details to predict all possible colleges and branches based on your rank."
|
| 83 |
)
|
| 84 |
|
| 85 |
-
demo.launch()
|
|
|
|
| 17 |
label_encoders[col] = le
|
| 18 |
|
| 19 |
X = df[["Category", "Gender", "Opening Rank", "Closing Rank", "Region"]]
|
| 20 |
+
y_college = df["College Name"]
|
| 21 |
+
y_branch = df["Branch"]
|
| 22 |
|
| 23 |
+
return X, y_college, y_branch, label_encoders, df
|
| 24 |
|
| 25 |
+
filename = "/content/AP_EAMCET_Engineering_10000 (1).csv"
|
| 26 |
+
X, y_college, y_branch, label_encoders, df = load_and_preprocess_data(filename)
|
| 27 |
|
| 28 |
+
# Train model and evaluate metrics
|
| 29 |
+
def train_model(X, y, target_name):
|
| 30 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 31 |
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 32 |
model.fit(X_train, y_train)
|
| 33 |
+
|
| 34 |
+
# Predictions
|
| 35 |
+
y_pred = model.predict(X_test)
|
| 36 |
+
|
| 37 |
+
# Evaluate model metrics
|
| 38 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 39 |
+
precision = precision_score(y_test, y_pred, average='weighted', zero_division=1)
|
| 40 |
+
recall = recall_score(y_test, y_pred, average='weighted', zero_division=1)
|
| 41 |
+
f1 = f1_score(y_test, y_pred, average='weighted', zero_division=1)
|
| 42 |
+
conf_matrix = confusion_matrix(y_test, y_pred)
|
| 43 |
+
|
| 44 |
+
print(f"{target_name} Model Metrics:")
|
| 45 |
+
print(f"Accuracy: {accuracy:.4f}")
|
| 46 |
+
print(f"Precision: {precision:.4f}")
|
| 47 |
+
print(f"Recall: {recall:.4f}")
|
| 48 |
+
print(f"F1 Score: {f1:.4f}")
|
| 49 |
+
print(f"Confusion Matrix:\n{conf_matrix}\n")
|
| 50 |
+
|
| 51 |
return model
|
| 52 |
|
| 53 |
+
# Train separate models
|
| 54 |
+
college_model = train_model(X, y_college, "College Name")
|
| 55 |
+
branch_model = train_model(X, y_branch, "Branch")
|
| 56 |
|
| 57 |
+
# Save models and encoders
|
| 58 |
+
joblib.dump(college_model, "college_model.pkl")
|
| 59 |
+
joblib.dump(branch_model, "branch_model.pkl")
|
| 60 |
joblib.dump(label_encoders, "label_encoders.pkl")
|
| 61 |
|
| 62 |
# Prediction function
|
| 63 |
def predict_colleges(category, gender, rank, region):
|
| 64 |
+
# Load models and label encoders
|
| 65 |
+
college_model = joblib.load("college_model.pkl")
|
| 66 |
+
branch_model = joblib.load("branch_model.pkl")
|
| 67 |
label_encoders = joblib.load("label_encoders.pkl")
|
| 68 |
|
| 69 |
# Transform input values using label encoders
|
|
|
|
| 74 |
except ValueError:
|
| 75 |
return "Invalid input values. Please select valid options."
|
| 76 |
|
| 77 |
+
# Filter dataset based on criteria
|
| 78 |
filtered_df = df[
|
| 79 |
(df["Category"] == category_enc) &
|
| 80 |
(df["Gender"] == gender_enc) &
|
|
|
|
| 107 |
description="Enter your details to predict all possible colleges and branches based on your rank."
|
| 108 |
)
|
| 109 |
|
| 110 |
+
demo.launch()
|