Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

don't have direct access to external files or the internet. I can provide you with the code and guidance on how to perform the tasks

don't have direct access to external files or the internet. I can provide you with the code and guidance on how to perform the tasks you mentioned using Python and common libraries. You'll need to download the "Real estate valuation data set.xlsx" file from the UCI repository and have it locally accessible.
Here's how you can accomplish the tasks you mentioned:
Task 1: Linear Regression Model
First, let's load and split the dataset into training and testing sets. You can use the train_test_split function from scikit-learn to accomplish this:
import pandas as pd
from sklearn.model_selection import train_test_split
# Load the dataset
data = pd.read_excel("Real estate valuation data set.xlsx")
# Split the dataset into features and targets
X = data.iloc[:, :-1] # Features (all columns except the last one)
y = data.iloc[:,-1] # Target (last column)
# Split the data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Next, we'll train a linear regression model on the training set and evaluate its performance on the test set:
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
# Create a linear regression model
model = LinearRegression()
# Train the model
model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = model.predict(X_test)
# Calculate performance metrics
mse = mean_squared_error(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)
r2= r2_score(y_test, y_pred)
# Print the performance metrics
print("Mean Squared Error:", mse)
print("Mean Absolute Error:", mae)
print("R-squared Score:", r2)
Explanation:
This code will calculate and print the mean squared error (MSE), mean absolute error (MAE), and R-squared score for the linear regression model.
Step 2
Task 2: Applying PCA on the Dataset
To apply PCA on the dataset and select the first three principal components, you can use the PCA class from scikit-learn:
from sklearn.decomposition import PCA
# Apply PCA on the dataset
pca = PCA(n_components=3)
X_pca = pca.fit_transform(X)
# Split the PCA-transformed data into train and test sets
X_pca_train, X_pca_test, _,_= train_test_split(X_pca, y, test_size=0.2, random_state=42)
Now, you have X_pca_train and X_pca_test as the PCA-transformed features, and you can proceed to train a linear regression model and evaluate its performance using the same code as in Task 1.
Step 3
Task 3: Logistic Regression Model with PCA on IRIS Dataset
To load the "IRIS" dataset from scikit-learn, apply PCA, split it into train and test sets, and train a logistic regression model, you can use the following code:
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# Load the IRIS dataset
iris = load_iris()
# Split the dataset into features and targets
X = iris.data
y = iris.target
# Apply PCA on the dataset
pca = PCA(n_components=3)
X_pca = pca.fit_transform(X)
# Split the PCA-transformed data into train and test sets
X_pca_train, X_pca_test, y_train, y_test = train_test_split(X_pca, y, test_size=0.2, random_state=42)
# Create a logistic regression model
model = LogisticRegression()
# Train the model
model.fit(X_pca_train, y_train)
# Make predictions on the test set
y_pred = model.predict(X_pca_test)
# Calculate performance metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='weighted')
recall = recall_score(y_test, y_pred, average='weighted')
f1= f1_score(y_test, y_pred, average='weighted')
# Print the performance metrics
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1 Score:", f1)
This code will calculate and print the accuracy, precision, recall, and F1 score for the logistic regression model trained on the PCA-transformed IRIS dataset.
Step 4
Task 4: Logistic Regression Model with Regularization
To apply L1 or L2 regularization to the logistic regression model using the same train and test data as in Task 3, you can modify the logistic regression model creation code as follows:
# Create a logistic regression model with L1 regularization
model = LogisticRegression(penalty='l1', solver='saga')
# Create a logistic regression model with L2 regularization
model = LogisticRegression(penalty='l2')
After modifying the model creation code, you can proceed to train the model, make predictions, and calculate the performance metrics as before. Compare the performance of this regularized model with the performance reported in Task 3 to observe the outcome.
Remember to import the necessary libraries and make sure you have scikit-learn installed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

What is a budget? (p. 314)

Answered: 1 week ago