Question: Code for k - NN library ( class ) library ( readxl ) library ( pROC ) library ( ROCR ) data < - read

Code for k-NN
library(class)
library(readxl)
library(pROC)
library(ROCR)
data <- read_excel("C:/Users/hemun/Downloads/RidingMowerskNN.xlsx")
print(head(data))
print(table(data$Validation))
training_data <- data[data$Validation ==0,]
validation_data <- data[data$Validation ==1,]
print(nrow(training_data))
print(nrow(validation_data))
train_features <- scale(training_data[, c("Income", "Lot Size")])
validation_features <- scale(validation_data[, c("Income", "Lot Size")])
train_labels <- training_data$Ownership
validation_labels <- validation_data$Ownership
print(head(train_features))
print(table(train_labels))
k <-8
predictions <- knn(train = train_features,
test = validation_features,
cl = train_labels,
k = k)
accuracy <- mean(predictions == validation_labels)*100
print(paste("Accuracy:", round(accuracy,2),"%"))
results <- data.frame(
Actual = validation_labels,
Predicted = predictions,
Income = validation_data$Income,
Lot Size = validation_data$Lot Size
)
print("KNN Classification Results:")
print(results)
print(paste("Accuracy:", round(accuracy,2),"%"))
plot(data$Income, data$Lot_Size,
col = ifelse(data$Ownership == "owner", "blue", "red"),
pch = ifelse(data$Validation ==0,16,17),
xlab = "Income (thousands)",
ylab = "Lot Size (thousands sq. ft)",
main = "RidingMowers KNN Classification")
legend("topleft",
legend = c("Owner", "Non-owner", "Training", "Validation"),
col = c("blue", "red", "black", "black"),
pch = c(16,16,16,17))
validation_labels_numeric<- as.numeric(validation_labels=="owner")
predictions_numeric <- as.numeric(predictions=="owner")
roc_curve_knn <- roc(validation_labels, as.numeric(predictions))
plot(roc_curve_knn, main = "ROC Curve for k-NN", col = "blue")
pred_knn <- prediction(as.numeric(predictions),
as.numeric(validation_labels_numeric))
perf_knn <- performance(pred_knn, "lift", "rpp")
plot(perf_knn, main = "Lift Curve for k-NN", col = "red")
actual <- as.numeric(validation_labels == "owner")
predicted_numeric_knn <- as.numeric(predictions == "owner")
rase_knn <- sqrt(mean((actual - predicted_numeric_knn)^2))
rsq_knn <-1-(sum((actual - predicted_numeric_knn)^2)/ sum((actual - mean(actual))^2))
print(paste("RASE for k-NN:", rase_knn))
print(paste("R-squared for k-NN:", rsq_knn))
predict_ownership <- function(income,lot size){
new_data <- scale(matrix(c(income, lot size), ncol =2),
center = attr(train_features, "scaled:center"),
scale = attr(train_features, "scaled:scale"))
prediction <- knn(train = train_features,
test = new_data,
cl = train_labels,
k = k)
return(prediction)
}
new_customer_prediction <- predict_ownership(income =70, lot size =20)
print(paste("Predicted ownership status:", new_customer_prediction))
code for logistic regression
library(caret)
library(pROC)
library(ROCR)
library(readxl)
data <- read_excel("C:/Users/hemun/Downloads/RidingMowerskNN.xlsx")
data$Ownership <- factor(data$Ownership, levels = c("non-owner", "owner"))
set.seed(123)
split_index <- createDataPartition(data$Ownership, p =0.7, list = FALSE)
train <- data[split_index, ]
test <- data[-split_index, ]
log_model <- glm(Ownership ~ Income + Lot Size, data = train, family = "binomial")
log_pred <- predict(log_model, test, type = "response")
log_pred_class <- factor(ifelse(log_pred >0.5, "owner", "non-owner"),
levels = c("non-owner", "owner"))
conf_mat_log <- confusionMatrix(log_pred_class, test$Ownership)
print(conf_mat_log)
roc_curve_log <- roc(test$Ownership, as.numeric(log_pred))
plot(roc_curve_log, main = "ROC Curve for Logistic Regression", col = "blue")
pred_log <- prediction(log_pred, test$Ownership)
perf_log <- performance(pred_log, "lift", "rpp")
plot(perf_log, main = "Lift Curve for Logistic Regression", col = "red")
actual <- as.numeric(test$Ownership == "owner")
predicted <- as.numeric(log_pred_class == "owner")
rase_log <- sqrt(mean((actual - predicted)^2))
rsq_log <-1-(sum((actual - predicted)^2)/ sum((actual - mean(actual))^2))
print(paste("RASE for Logistic Regression:", rase_log))
print(paste("R-squared for Logistic Regression:", rsq_log))
Give me a code for ensembles with k-NN and logistic regression

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!