OneCompiler

DS13

332

Q. 1) Write AJAX program where the user is requested to write his or her name in a text box, and the
server keeps sending back responses while the user is typing. If the user name is not entered then the
message displayed will be, “Stranger, please tell me your name!”. If the name is Rohit, Virat, Dhoni,
Ashwin or Harbhajan , the server responds with “Hello, master !”. If the name is anything else, the
message will be “, I don’t know you!” [Marks 15]

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>AJAX Name Recognition</title> <script> function checkName() { var name = document.getElementById('name').value; var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("response").innerHTML = this.responseText; } }; xhttp.open("GET", "ajax_name_recognition.php?name=" + name, true); xhttp.send(); } </script> </head> <body> <p>Enter your name: <input type="text" id="name" onkeyup="checkName()"></p> <p id="response"></p> </body> </html> <?php $name = $_GET['name']; if (empty($name)) { echo "Stranger, please tell me your name!"; } else if (in_array($name, ['Rohit', 'Virat', 'Dhoni', 'Ashwin', 'Harbhajan'])) { echo "Hello, master!"; } else { echo "I don't know you!"; } ?>

Q. 2)Download nursery dataset from UCI. Build a linear regression model by identifying independent
and target variable. Split the variables into training and testing sets and print them. Build a simple linear
regression model for predicting purchases.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

Load the dataset

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/nursery/nursery.data"
column_names = ["parents", "has_nurs", "form", "children", "housing", "finance", "social", "health", "purchased"]
data = pd.read_csv(url, names=column_names)

Identify independent variables (features) and target variable

X = data.drop("purchased", axis=1) # Independent variables
y = data["purchased"] # Target variable

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=42)

Print the shapes of training and testing sets

print("Training set shape:", X_train.shape, y_train.shape)
print("Testing set shape:", X_test.shape, y_test.shape)

Build a linear regression model

model = LinearRegression()

Train the model using the training set

model.fit(X_train, y_train)

Make predictions on the testing set

y_pred = model.predict(X_test)

Calculate Mean Squared Error

mse = mean_squared_error(y_test, y_pred)
print("Mean Squared Error:", mse)

Print the coefficients of the model

print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)