|
import numpy as np |
|
import pandas as pd |
|
import torch |
|
import torch.nn as nn |
|
import torch.optim as optim |
|
from sklearn.model_selection import train_test_split |
|
from sklearn.metrics import accuracy_score |
|
|
|
class PlacementModel(nn.Module): |
|
def __init__(self, input_size, hidden_size, output_size): |
|
super(PlacementModel, self).__init__() |
|
self.fc1 = nn.Linear(input_size, hidden_size) |
|
self.fc2 = nn.Linear(hidden_size, output_size) |
|
|
|
def forward(self, x): |
|
x = torch.relu(self.fc1(x)) |
|
x = self.fc2(x) |
|
return x |
|
|
|
|
|
df = pd.read_csv("Placement (2).csv") |
|
df = df.drop(columns=["sl_no","stream","ssc_p","ssc_b","hsc_p","hsc_b","etest_p"]) |
|
df['internship'] = df['internship'].map({'Yes':1,'No':0}) |
|
df['status'] = df['status'].map({'Placed':1,'Not Placed':0}) |
|
|
|
X_fullstk = df.drop(['status','management','leadership','communication','sales'], axis=1) |
|
y = df['status'] |
|
|
|
X_train_fullstk, X_test_fullstk, y_train, y_test = train_test_split(X_fullstk, y, test_size=0.20, random_state=42) |
|
|
|
|
|
input_size = X_fullstk.shape[1] |
|
hidden_size = 128 |
|
output_size = 2 |
|
learning_rate = 0.01 |
|
epochs = 100 |
|
|
|
|
|
model = PlacementModel(input_size, hidden_size, output_size) |
|
criterion = nn.CrossEntropyLoss() |
|
optimizer = optim.Adam(model.parameters(), lr=learning_rate) |
|
|
|
|
|
for epoch in range(epochs): |
|
inputs = torch.tensor(X_train_fullstk.values, dtype=torch.float32) |
|
labels = torch.tensor(y_train.values, dtype=torch.long) |
|
|
|
optimizer.zero_grad() |
|
|
|
outputs = model(inputs) |
|
loss = criterion(outputs, labels) |
|
loss.backward() |
|
optimizer.step() |
|
|
|
if epoch % 10 == 0: |
|
print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}') |
|
|
|
|
|
with torch.no_grad(): |
|
inputs = torch.tensor(X_test_fullstk.values, dtype=torch.float32) |
|
labels = torch.tensor(y_test.values, dtype=torch.long) |
|
|
|
outputs = model(inputs) |
|
_, predicted = torch.max(outputs.data, 1) |
|
accuracy = accuracy_score(labels, predicted) |
|
|
|
print(f'Test Accuracy: {accuracy:.4f}') |
|
|
|
|
|
torch.save(model.state_dict(), 'placement_model.pth') |
|
|