0% found this document useful (0 votes)
66 views2 pages

KNN - Model: Train Test CL K

The document provides R code examples for building machine learning models for various algorithms using different R packages. These algorithms include KNN, naive Bayes, decision trees, SVM, ANN, linear regression, logistic regression, K-means clustering, Apriori, and AdaBoost. For each algorithm, it lists the relevant R package, function, and example code for building a model. It also provides a generic predict function that can be used to predict on new data for many of the models.

Uploaded by

GeethaSrinivasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views2 pages

KNN - Model: Train Test CL K

The document provides R code examples for building machine learning models for various algorithms using different R packages. These algorithms include KNN, naive Bayes, decision trees, SVM, ANN, linear regression, logistic regression, K-means clustering, Apriori, and AdaBoost. For each algorithm, it lists the relevant R package, function, and example code for building a model. It also provides a generic predict function that can be used to predict on new data for many of the models.

Uploaded by

GeethaSrinivasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

K-Nearest Neighbors (KNN) Classification: knn method from class package could be used

for K-NN modeling. One need to install and load class package. Following is the sample command
given X_train represents a training dataset, X_test represents test data set, k represents number of
nearest neighbors to be included for the modeling
knn_model <- knn(train=X_train, test=X_test, cl=as.factor(labels), k=K)

Naive Bayes Classification: naiveBayes method from e1071 package could be used for Naive
Bayes classification. One need to install and load e1071 package prior to analysis. Following is the
sample command:
naiveBayes_model <- naiveBayes(y ~ x1 + x2, data=as.data.frame(cbind(y,x1,x2)))

Decision Trees: rpart method from rpart can be used for Decision Trees. One need to install
and load rpart package. Following is the sample command:
cart_model <- rpart(y ~ x1 + x2, data=as.data.frame(cbind(y,x1,x2)), method="class")

Support Vector Machine (SVM): svm method from e1071 package could be used for SVM.
Note that the same package also provide method, naiveBayes, for Naive Bayes classification. One
need to install and load e1071 package. Following is the sample command given X is the matrix of
features, labels be the vector of 0-1 class labels, and C being regularization parameter
svm_model <- svm(x=X, y=as.factor(labels), kernel ="radial", cost=C)

Artifical Neural Network (ANN): neuralnet method from neuralnet package could be used for

ANN modeling. Following is sample command:


ann_model <- neuralnet( y ~ x1 + x2 + x3, data=as.data.frame(cbind(y,x1,x2, x3)), hidden = 1)
Prediction could be made using following Cheat Sheet ML Algorithms & R Commands

K-Nearest Neighbors (KNN) Classification: knn method from class package could be used
for K-NN modeling. One need to install and load class package. Following is the sample command
given X_train represents a training dataset, X_test represents test data set, k represents number of
nearest neighbors to be included for the modeling
knn_model <- knn(train=X_train, test=X_test, cl=as.factor(labels), k=K)

Naive Bayes Classification: naiveBayes method from e1071 package could be used for Naive
Bayes classification. One need to install and load e1071 package prior to analysis. Following is the
sample command:
naiveBayes_model <- naiveBayes(y ~ x1 + x2, data=as.data.frame(cbind(y,x1,x2)))

Decision Trees: rpart method from rpart can be used for Decision Trees. One need to install
and load rpart package. Following is the sample command:
cart_model <- rpart(y ~ x1 + x2, data=as.data.frame(cbind(y,x1,x2)), method="class")

Support Vector Machine (SVM): svm method from e1071 package could be used for SVM.
Note that the same package also provide method, naiveBayes, for Naive Bayes classification. One
need to install and load e1071 package. Following is the sample command given X is the matrix of
features, labels be the vector of 0-1 class labels, and C being regularization parameter
svm_model <- svm(x=X, y=as.factor(labels), kernel ="radial", cost=C)

Artifical Neural Network (ANN): neuralnet method from neuralnet package could be used for
ANN modeling. Following is sample command:
ann_model <- neuralnet( y ~ x1 + x2 + x3, data=as.data.frame(cbind(y,x1,x2, x3)), hidden = 1)

Prediction could be made using following Linear regression: lm method from base package could
be used for linear regression models. Following is the sample command:
lm_model <- lm(y ~ x1 + x2, data=as.data.frame(cbind(y,x1,x2)))

Logistic Regression: Logistic regression is a classification based model. glm method from base
R package could be used for logistic regression. Following is the sample command:
glm_model <- glm(y ~ x1+x2, family=binomial(link="logit"), data=as.data.frame(cbind(y,x1,x2)))

K-Means Clustering: kmeans method from base R package could be used to run k-means
clustering. Following is a sample command given X is a data matrix and m is the number of clusters:
kmeans_model <- kmeans(x=X, centers=m)

K-Nearest Neighbors (KNN) Classification: knn method from class package could be used
for K-NN modeling. One need to install and load class package. Following is the sample command
given X_train represents a training dataset, X_test represents test data set, k represents number of
nearest neighbors to be included for the modeling
knn_model <- knn(train=X_train, test=X_test, cl=as.factor(labels), k=K)

Naive Bayes Classification: naiveBayes method from e1071 package could be used for Naive
Bayes classification. One need to install and load e1071 package prior to analysis. Following is the
sample command:
naiveBayes_model <- naiveBayes(y ~ x1 + x2, data=as.data.frame(cbind(y,x1,x2)))

Decision Trees: rpart method from rpart can be used for Decision Trees. One need to install
and load rpart package. Following is the sample command:
cart_model <- rpart(y ~ x1 + x2, data=as.data.frame(cbind(y,x1,x2)), method="class")

Support Vector Machine (SVM): svm method from e1071 package could be used for SVM.
Note that the same package also provide method, naiveBayes, for Naive Bayes classification. One
need to install and load e1071 package. Following is the sample command given X is the matrix of
features, labels be the vector of 0-1 class labels, and C being regularization parameter
svm_model <- svm(x=X, y=as.factor(labels), kernel ="radial", cost=C)

Artifical Neural Network (ANN): neuralnet method from neuralnet package could be used for
ANN modeling. Following is sample command:
ann_model <- neuralnet( y ~ x1 + x2 + x3, data=as.data.frame(cbind(y,x1,x2, x3)), hidden = 1)

Prediction could be made using following formula:


p <- compute( ann_model, as.data.frame(cbind(x1,x2)) )

Apriori: apriori method from arules package could be used for Apriori analysis. One need to
install and load arules package. Following is the sample command:
apriori_model <- apriori(as.matrix(sampleDataset), parameter = list(supp = 0.8, conf = 0.9))

AdaBoost: ada method from rpart package could be used as boosting function. Following is
sample command:
boost_model <- ada(x=X, y=labels)

For most of the above formulas including linear regression model, one could use following function to predict:
predicted_values <- predict(some_model, newdata=as.data.frame(cbind(x1_test, x2_test)))

You might also like