You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
1.8 KiB
86 lines
1.8 KiB
6 years ago
|
#Linear Regression
|
||
|
|
||
|
#Load Train and Test datasets
|
||
|
#Identify feature and response variable(s) and values must be numeric and numpy arrays
|
||
|
x_train <- input_variables_values_training_datasets
|
||
|
y_train <- target_variables_values_training_datasets
|
||
|
x_test <- input_variables_values_test_datasets
|
||
|
x <- cbind(x_train,y_train)
|
||
|
# Train the model using the training sets and check score
|
||
|
linear <- lm(y_train ~ ., data = x)
|
||
|
summary(linear)
|
||
|
#Predict Output
|
||
|
predicted= predict(linear,x_test)
|
||
|
|
||
|
#Logistic Regression
|
||
|
|
||
|
x <- cbind(x_train,y_train)
|
||
|
# Train the model using the training sets and check score
|
||
|
logistic <- glm(y_train ~ ., data = x,family='binomial')
|
||
|
summary(logistic)
|
||
|
#Predict Output
|
||
|
predicted= predict(logistic,x_test)
|
||
|
|
||
|
#Decision tree
|
||
|
|
||
|
library(rpart)
|
||
|
x <- cbind(x_train,y_train)
|
||
|
# grow tree
|
||
|
fit <- rpart(y_train ~ ., data = x,method="class")
|
||
|
summary(fit)
|
||
|
#Predict Output
|
||
|
predicted= predict(fit,x_test)
|
||
|
|
||
|
|
||
|
#SVM
|
||
|
|
||
|
library(e1071)
|
||
|
x <- cbind(x_train,y_train)
|
||
|
# Fitting model
|
||
|
fit <-svm(y_train ~ ., data = x)
|
||
|
summary(fit)
|
||
|
#Predict Output
|
||
|
predicted= predict(fit,x_test)
|
||
|
|
||
|
# Naive Bayes
|
||
|
|
||
|
library(e1071)
|
||
|
x <- cbind(x_train,y_train)
|
||
|
# Fitting model
|
||
|
fit <-naiveBayes(y_train ~ ., data = x)
|
||
|
summary(fit)
|
||
|
#Predict Output
|
||
|
predicted= predict(fit,x_test)
|
||
|
|
||
|
#kNN (k- Nearest Neighbors)
|
||
|
|
||
|
library(knn)
|
||
|
x <- cbind(x_train,y_train)
|
||
|
# Fitting model
|
||
|
fit <-knn(y_train ~ ., data = x,k=5)
|
||
|
summary(fit)
|
||
|
#Predict Output
|
||
|
predicted= predict(fit,x_test)
|
||
|
|
||
|
#K-Means
|
||
|
|
||
|
library(cluster)
|
||
|
fit <- kmeans(X, 3) # 5 cluster solution
|
||
|
|
||
|
#Random Forest
|
||
|
|
||
|
library(randomForest)
|
||
|
x <- cbind(x_train,y_train)
|
||
|
# Fitting model
|
||
|
fit <- randomForest(Species ~ ., x,ntree=500)
|
||
|
summary(fit)
|
||
|
#Predict Output
|
||
|
predicted= predict(fit,x_test)
|
||
|
|
||
|
#Dimensionality Reduction Algorithms
|
||
|
|
||
|
library(stats)
|
||
|
pca <- princomp(train, cor = TRUE)
|
||
|
train_reduced <- predict(pca,train)
|
||
|
test_reduced <- predict(pca,test)
|