Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,74 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
-
import
|
| 3 |
-
from sklearn.compose import ColumnTransformer
|
| 4 |
-
from sklearn.ensemble import RandomForestClassifier
|
| 5 |
-
from sklearn.impute import SimpleImputer
|
| 6 |
from sklearn.model_selection import train_test_split
|
|
|
|
| 7 |
from sklearn.pipeline import Pipeline
|
| 8 |
-
from sklearn.
|
| 9 |
-
from
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
|
| 13 |
# Load the CSV data
|
| 14 |
data = pd.read_csv('dataset.csv')
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
X = data.drop('PlacedOrNot', axis=1)
|
| 18 |
y = data['PlacedOrNot']
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
encoder = LabelEncoder()
|
| 24 |
-
X[feature] = encoder.fit_transform(X[feature])
|
| 25 |
|
| 26 |
# Split the data into training and testing sets
|
| 27 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 28 |
|
| 29 |
-
# Create the pipeline
|
| 30 |
-
numerical_features = ['Internships', 'CGPA']
|
| 31 |
-
numerical_transformer = StandardScaler()
|
| 32 |
-
categorical_features = [ 'HistoryOfBacklogs']
|
| 33 |
-
categorical_transformer = SimpleImputer(strategy='most_frequent')
|
| 34 |
preprocessor = ColumnTransformer(
|
| 35 |
transformers=[
|
| 36 |
-
('num',
|
| 37 |
-
('cat',
|
| 38 |
])
|
| 39 |
|
|
|
|
| 40 |
pipeline = Pipeline([
|
| 41 |
('preprocessor', preprocessor),
|
| 42 |
('classifier', RandomForestClassifier(random_state=42))
|
| 43 |
])
|
| 44 |
|
| 45 |
-
#
|
| 46 |
pipeline.fit(X_train, y_train)
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
print('Accuracy:', accuracy)
|
|
|
|
| 51 |
joblib.dump(pipeline, 'student_placement_model.joblib')
|
| 52 |
|
| 53 |
# Define Streamlit API
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
pipeline = joblib.load('student_placement_model.joblib')
|
| 57 |
-
|
| 58 |
-
# Prepare input data
|
| 59 |
-
input_data = pd.DataFrame({'internships': [internships],
|
| 60 |
-
'cgpa': [cgpa],
|
| 61 |
-
'history_of_backlogs': [history_of_backlogs],
|
| 62 |
-
'stream': [stream]})
|
| 63 |
-
|
| 64 |
-
# Make prediction
|
| 65 |
-
prediction = pipeline.predict(input_data)
|
| 66 |
-
|
| 67 |
-
return prediction[0]
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
if prediction == 1:
|
| 78 |
-
result = 'Placed'
|
| 79 |
-
else:
|
| 80 |
-
result = 'Not Placed'
|
| 81 |
-
button('Predict Placement')
|
| 82 |
-
write(f'Result: {result}')
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 3 |
from sklearn.model_selection import train_test_split
|
| 4 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 5 |
from sklearn.pipeline import Pipeline
|
| 6 |
+
from sklearn.compose import ColumnTransformer
|
| 7 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
| 8 |
+
from sklearn.metrics import accuracy_score
|
| 9 |
+
import streamlit as st
|
| 10 |
|
| 11 |
# Load the CSV data
|
| 12 |
data = pd.read_csv('dataset.csv')
|
| 13 |
|
| 14 |
+
|
| 15 |
+
# Split the data into features and target variable
|
| 16 |
X = data.drop('PlacedOrNot', axis=1)
|
| 17 |
y = data['PlacedOrNot']
|
| 18 |
|
| 19 |
+
# Split the data into training and testing sets
|
| 20 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 21 |
+
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Split the data into training and testing sets
|
| 24 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
preprocessor = ColumnTransformer(
|
| 27 |
transformers=[
|
| 28 |
+
('num', StandardScaler(), ['internships', 'cgpa', 'history_of_backlogs']),
|
| 29 |
+
('cat', OneHotEncoder(), ['gender', 'stream'])
|
| 30 |
])
|
| 31 |
|
| 32 |
+
# Create the pipeline with Random Forest classifier
|
| 33 |
pipeline = Pipeline([
|
| 34 |
('preprocessor', preprocessor),
|
| 35 |
('classifier', RandomForestClassifier(random_state=42))
|
| 36 |
])
|
| 37 |
|
| 38 |
+
# Fit the pipeline to the training data
|
| 39 |
pipeline.fit(X_train, y_train)
|
| 40 |
|
| 41 |
+
# Make predictions on the test data
|
| 42 |
+
y_pred = pipeline.predict(X_test)
|
| 43 |
+
|
| 44 |
+
# Calculate accuracy of the model
|
| 45 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 46 |
print('Accuracy:', accuracy)
|
| 47 |
+
|
| 48 |
joblib.dump(pipeline, 'student_placement_model.joblib')
|
| 49 |
|
| 50 |
# Define Streamlit API
|
| 51 |
+
# Streamlit API for serving the model
|
| 52 |
+
st.title('Student Job Placement Prediction')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Input form for user to enter features
|
| 55 |
+
st.markdown('Please enter the following information:')
|
| 56 |
+
internships = st.number_input('Number of Internships', min_value=0, max_value=10)
|
| 57 |
+
cgpa = st.number_input('CGPA', min_value=0.0, max_value=10.0)
|
| 58 |
+
history_of_backlogs = st.number_input('History of Backlogs', min_value=0, max_value=10)
|
| 59 |
+
gender = st.selectbox('Gender', ('Male', 'Female'))
|
| 60 |
+
stream = st.selectbox('Stream', ('Engineering', 'Science', 'Commerce'))
|
| 61 |
+
submit = st.button('Submit')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# Make prediction on user input when 'Submit' button is clicked
|
| 64 |
+
if submit:
|
| 65 |
+
# Create a dataframe with user input
|
| 66 |
+
user_data = pd.DataFrame([[internships, cgpa, history_of_backlogs, gender, stream]],
|
| 67 |
+
columns=['internships', 'cgpa', 'history_of_backlogs', 'gender', 'stream'])
|
| 68 |
+
# Make prediction using the pipeline
|
| 69 |
+
prediction = pipeline.predict(user_data)
|
| 70 |
+
# Display prediction
|
| 71 |
+
if prediction[0] == 1:
|
| 72 |
+
st.success('Congratulations! The student is likely to be placed.')
|
| 73 |
+
else:
|
| 74 |
+
st.warning('Sorry, the student is unlikely to be placed.')
|