DS13
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]
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_)