1、前面Python 版本的多项线性回归,现不在对名词和操作流程做过多解释,直接上代码
dataset = read.csv('Position_Salaries.csv')
dataset = dataset[2:3]
#设置单一线性回归
lin_reg = lm(formula = Salary ~ . , data = dataset) #两个课星 合理范围内
#设置多项线性回归
dataset$Level2 = dataset$Level^2
dataset$Level3 = dataset$Level^3
dataset$Level4 = dataset$Level^4
ploy_reg = lm(formula = Salary ~. ,data = dataset) # 用R更直观
#可视化(单一)
#install.packages('ggplot2')
library(ggplot2)
ggplot() +
geom_point(aes(x = dataset$Level, y = dataset$Salary), colour = 'red') +
geom_line(aes(x = dataset$Level, y = predict(lin_reg, newdata = dataset), colour = 'blue')) +
ggtitle('Truth or Burff') +
xlab('Level')+
ylab('Salary')
单一线性模型画出来的图
在看下多项线性回归模型画出来的图
#可视化 (多项)
library(ggplot2)
ggplot() +
geom_point(aes(x = dataset$Level, y = dataset$Salary), colour = 'red') +
geom_line(aes(x = dataset$Level, y = predict(ploy_reg, newdata = dataset), colour = 'blue')) +
ggtitle('Truth or Burff') +
xlab('Level')+
ylab('Salary')