OneCompiler

DS3

215

Q. 1) Write a PHP script to accept username and password. If in the first three chances, username and
password entered is correct then display second form with “Welcome message” otherwise display error
message. [Use Session] [Marks 15]

<?php session_start(); // Define valid username and password $validUsername = "admin"; $validPassword = "password"; // Check if form is submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { // Get username and password from form $username = $_POST["username"]; $password = $_POST["password"]; // Check if username and password are correct if ($username === $validUsername && $password === $validPassword) { $_SESSION["loggedIn"] = true; // Set session variable to indicate user is logged in header("Location: welcome.php"); // Redirect to welcome page exit; } else { $_SESSION["attempts"] = ($_SESSION["attempts"] ?? 0) + 1; // Increment attempts if ($_SESSION["attempts"] >= 3) { $errorMessage = "Maximum attempts reached. Please try again later."; } else { $errorMessage = "Incorrect username or password. Please try again."; } } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Login</title> </head> <body> <h2>Login</h2> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"> <label for="username">Username:</label> <input type="text" id="username" name="username"><br><br> <label for="password">Password:</label> <input type="password" id="password" name="password"><br><br> <input type="submit" value="Login"> </form> <?php if (isset($errorMessage)) echo "<p style='color: red;'>$errorMessage</p>"; ?> </body> </html>

Q. 2)Create ‘User’ Data set having 5 columns namely: User ID, Gender, Age, Estimated Salary and
Purchased. Build a logistic regression model that can predict whether on the given parameter a person
will buy a car or not.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, classification_report

Create the 'User' dataset

data = {'User ID': [1, 2, 3, 4, 5],
'Gender': ['Male', 'Female', 'Male', 'Female', 'Male'],
'Age': [25, 30, 35, 40, 45],
'Estimated Salary': [30000, 40000, 50000, 60000, 70000],
'Purchased': [0, 1, 0, 1, 0]} # 0: Not purchased, 1: Purchased

Convert the dataset into a DataFrame

df = pd.DataFrame(data)

Identify independent variables (Gender, Age, Estimated Salary) and target variable (Purchased)

X = df[['Gender', 'Age', 'Estimated Salary']]
y = df['Purchased']

Convert categorical variable (Gender) into dummy/indicator variables

X = pd.get_dummies(X, drop_first=True)

Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

Feature scaling

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

Build a logistic regression model

classifier = LogisticRegression(random_state=0)
classifier.fit(X_train, y_train)

Predictions

y_pred = classifier.predict(X_test)

Evaluate the model

print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred))
print("\nClassification Report:")
print(classification_report(y_test, y_pred))