DS12
Q. 1)Write AJAX program to read contact.dat file and print the contents of the file in a tabular format
when the user clicks on print button. Contact.dat file should contain srno, name, residence number,
mobile number, Address. [Enter at least 3 record in contact.dat file] [Marks 15]
<button onclick="printContacts()">Print Contacts</button>
<div id="contactTable"></div> </body> </html> function printContacts() { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { var contacts = JSON.parse(xhr.responseText); var table = "<table border='1'><tr><th>Sr. No.</th><th>Name</th><th>Residence Number</th><th>Mobile Number</th><th>Address</th></tr>"; for (var i = 0; i < contacts.length; i++) { table += "<tr><td>" + contacts[i].srno + "</td><td>" + contacts[i].name + "</td><td>" + contacts[i].residence + "</td><td>" + contacts[i].mobile + "</td><td>" + contacts[i].address + "</td></tr>"; } table += "</table>"; document.getElementById("contactTable").innerHTML = table; } }; xhr.open("GET", "contact.dat", true); xhr.send(); }Q. 2)Create ‘heights-and-weights’ Data set . 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 numpy as np
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
Generate synthetic dataset
np.random.seed(0)
heights = np.random.normal(65, 3, 100) # mean=65, std=3
weights = 2.3 * heights + np.random.normal(0, 5, 100) # weight = 2.3 * height + random noise
Create DataFrame
data = pd.DataFrame({'Height': heights, 'Weight': weights})
Independent variable (feature)
X = data[['Height']]
Target variable
y = data['Weight']
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()
Fit the model on the training data
model.fit(X_train, y_train)
Make predictions on the testing data
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_)