1. 程式人生 > >Linear Classification in R

Linear Classification in R

In this post you will discover recipes for 3 linear classification algorithms in R.

All recipes in this post use the iris flowers dataset provided with R in the datasets package. The dataset describes the measurements if iris flowers and requires classification of each observation to one of three flower species.

binary classification

Red vs Blue
Photo by Robert Couse-Baker, some rights reserved

Logistic Regression

Logistic Regression is a classification method that models the probability of an observation belonging to one of two classes. As such, normally logistic regression is demonstrated with binary classification problem (2 classes). Logistic Regression can also be used on problems with more than two classes (multinomial), as in this case.

This recipe demonstrates multinomial logistic regression method on the iris dataset.

Logistic Regression in R R
12345678910111213141516 # load the packagelibrary(VGAM)# load datadata(iris)# fit modelfit<-vglm(Species~.,family=multinomial,data=iris)# summarize the fitsummary(fit)# make predictionsprobabilities<-predict(fit,iris[,1:4],type="response")predictions<-apply(probabilities,1,which.max)predictions[which(predictions=="1")]<-levels(iris$Species)[1]predictions[which(predictions=="2")]<-levels(iris$Species)[2]predictions[which(predictions=="3")]<-levels(iris$Species)[3]# summarize accuracytable(predictions,iris$Species)

Learn more about the vglm function in the VGAM package.

Need more Help with R for Machine Learning?

Take my free 14-day email course and discover how to use R on your project (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Linear Discriminant Analysis

LDA is a classification method that finds a linear combination of data attributes that best separate the data into classes.

This recipes demonstrates the LDA method on the iris dataset.

Linear Discriminant Analysis in R R
1234567891011 # load the packagelibrary(MASS)data(iris)# fit modelfit<-lda(Species~.,data=iris)# summarize the fitsummary(fit)# make predictionspredictions<-predict(fit,iris[,1:4])$class# summarize accuracytable(predictions,iris$Species)

Learn more about the lda function the MASS package.

Partial Least Squares Discriminant Analysis

Partial Least Squares Discriminate Analysis is the application of LDA on a dimension-reducing projection of the input data (partial least squares).

This recipe demonstrates the PLSDA method on the iris dataset.

Partial Least Squares Discriminant Analysis in R R
12345678910111213 # load the packagelibrary(caret)data(iris)x<-iris[,1:4]y<-iris[,5]# fit modelfit<-plsda(x,y,probMethod="Bayes")# summarize the fitsummary(fit)# make predictionspredictions<-predict(fit,iris[,1:4])# summarize accuracytable(predictions,iris$Species)

Learn more about the plsda function in the caret package.

Summary

In this post, you discovered 3 recipes for linear classification that you can copy and paste into your own problem.


Frustrated With Your Progress In R Machine Learning?

Master Machine Learning With R

Develop Your Own Models in Minutes

…with just a few lines of R code

Covers self-study tutorials and end-to-end projects like:
Loading data, visualization, build models, tuning, and much more…

Finally Bring Machine Learning To
Your Own Projects

Skip the Academics. Just Results.


相關推薦

Linear Classification in R with Decision Trees

Tweet Share Share Google Plus In this post you will discover 7 recipes for non-linear classifica

Linear Classification in R

Tweet Share Share Google Plus In this post you will discover recipes for 3 linear classification

Linear Regression in R with Decision Trees

Tweet Share Share Google Plus In this post, you will discover 8 recipes for non-linear regressio

Linear Regression in R

Tweet Share Share Google Plus In this post you will discover 4 recipes for non-linear regression

intersect for multiple vectors in R

con span osi library tar other and pos intersect Say you have a <- c(1,3,5,7,9) b <- c(3,6,8,9,10) c <- c(2,3,4,5,7,9) A stra

print,cat打印格式及字符串引號格式,去掉字符串空格 in R

with letters out logs true right int 函數 cnblogs print 函數的打印格式: ##no quote print out > x <- letters[1:5] > print(x,quote=F,);pri

Match function in R

nom -i strong UNC https tps true mat amp Examples: print(match(5, c(1,2,9,5,3,6,7,4,5)))[1] 4 5 %in% c(1,2,9,5,3,6,7,4,5)[1] T

Beautiful and Powerful Correlation Tables in R

select auto edit for open sat local plot fig Another correlation function?! Yes, the correlation function from the psycho package. devt

神經網路學習引入-線性分類器 Linear Classification

線性分類器 評分函式(score function):計算原始資料到所屬分類的得分 損失函式(loss function):量化預測得分和實際情況之間的認可度 從影象到標籤得分的引數對映 定義評分函式

Customer Lifetime Value in R筆記

原文:http://www.keetan.me/clv CLVcustomer lifetime value是顧客對公司的價值估計,客戶在與公司整個接觸時期的未來貢獻現金流淨值,利用到的模型為Pareto/NBD Buy Til'You Die;重點是預測顧客的未來價值 Four T

3DmFV: Three-Dimensional Point Cloud Classification in Real-Time Using Convolutional Neural Networks

此篇為對論文的理解。一下關鍵地方直接使用原文,避免誤導。 Abstract—Modern robotic systems are often equipped with a direct three-dimensional (3-D) data acquisit

Linear Equations in Linear Algebra

Linear System      Vector Equations  The Matrix Equation                    

Logistic regression in R

Datacamp Learning Logistic regression in R Building simple logistic regression models The donors dataset contains 93,462 examples of peop

How to write tidy SQL queries in R

How to write tidy SQL queries in RMost of us have to interact with databases nowadays, and SQL is by far the most common language used. However, working wi

Linear Regression in the Wild

In one of my job interviews for a data scientist position, I was given a home assignment I'd like to share with you. The interviewer sent me a CSV file con

OLS and Logistic Regression Models in R

OLS and Logistic Regression Models in RWe use linear models primarily to analyse cross-sectional data; i.e. data collected at one specific point in time ac

Cross-Correlation of Currency Pairs In R (ccf)

Cross-Correlation of Currency Pairs In R (ccf)When working with a time series, one important thing we wish to determine is whether one series “causes” chan

Simple and Multiple Linear Regression in Python

Linear Regression in PythonThere are two main ways to perform linear regression in Python — with Statsmodels and scikit-learn. It is also possible to use t

Linear Regression in 6 lines of Python

Today to perform Linear Regression quickly, we will be using the library scikit-learn. If you don’t have it already you can install it using pip:pip instal

style app in R using Shiny+Flexdashboard

Managing Human Intelligence TasksThis workflow has served us well, but we quickly developed a backlog of messages that did not fall into one of our existin