Upload placement.py
Browse files- placement.py +72 -0
placement.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
|
| 4 |
+
from sklearn.compose import ColumnTransformer
|
| 5 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
+
from sklearn.impute import SimpleImputer
|
| 7 |
+
from sklearn.model_selection import train_test_split
|
| 8 |
+
from sklearn.pipeline import Pipeline
|
| 9 |
+
from sklearn.preprocessing import LabelEncoder, StandardScaler
|
| 10 |
+
|
| 11 |
+
# Load the CSV data
|
| 12 |
+
data = pd.read_csv('dataset.csv')
|
| 13 |
+
|
| 14 |
+
# Split the data into features and labels
|
| 15 |
+
X = data.drop('PlacedOrNot', axis=1)
|
| 16 |
+
y = data['PlacedOrNot']
|
| 17 |
+
|
| 18 |
+
# Encode categorical features
|
| 19 |
+
categorical_features = ['HistoryOfBacklogs']
|
| 20 |
+
for feature in categorical_features:
|
| 21 |
+
encoder = LabelEncoder()
|
| 22 |
+
X[feature] = encoder.fit_transform(X[feature])
|
| 23 |
+
|
| 24 |
+
# Split the data into training and testing sets
|
| 25 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 26 |
+
|
| 27 |
+
# Create the pipeline
|
| 28 |
+
numerical_features = ['Internships', 'CGPA']
|
| 29 |
+
numerical_transformer = StandardScaler()
|
| 30 |
+
categorical_features = [ 'HistoryOfBacklogs']
|
| 31 |
+
categorical_transformer = SimpleImputer(strategy='most_frequent')
|
| 32 |
+
preprocessor = ColumnTransformer(
|
| 33 |
+
transformers=[
|
| 34 |
+
('num', numerical_transformer, numerical_features),
|
| 35 |
+
('cat', categorical_transformer, categorical_features)
|
| 36 |
+
])
|
| 37 |
+
|
| 38 |
+
pipeline = Pipeline([
|
| 39 |
+
('preprocessor', preprocessor),
|
| 40 |
+
('classifier', RandomForestClassifier(random_state=42))
|
| 41 |
+
])
|
| 42 |
+
|
| 43 |
+
# Train the model
|
| 44 |
+
pipeline.fit(X_train, y_train)
|
| 45 |
+
|
| 46 |
+
# Evaluate the model
|
| 47 |
+
accuracy = pipeline.score(X_test, y_test)
|
| 48 |
+
print('Accuracy:', accuracy)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Create Flask app
|
| 52 |
+
app = Flask(__name__)
|
| 53 |
+
|
| 54 |
+
# Define API route for making predictions
|
| 55 |
+
@app.route('/predict', methods=['POST'])
|
| 56 |
+
def predict():
|
| 57 |
+
# Get input data from request
|
| 58 |
+
data = request.get_json()
|
| 59 |
+
|
| 60 |
+
# Convert input data to dataframe
|
| 61 |
+
input_data = pd.DataFrame(data, index=[0])
|
| 62 |
+
|
| 63 |
+
# Make predictions using the trained pipeline
|
| 64 |
+
predictions = pipeline.predict(input_data)
|
| 65 |
+
|
| 66 |
+
# Prepare response
|
| 67 |
+
response = {'prediction': predictions[0]}
|
| 68 |
+
return jsonify(response)
|
| 69 |
+
|
| 70 |
+
# Run the Flask app
|
| 71 |
+
if __name__ == '__main__':
|
| 72 |
+
app.run(debug=True)
|