github线性回归怎么实现

本篇内容介绍了“github线性回归怎么实现”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

Simple Linear Regression

Data Preprocessing

1 读入数据集

# Importing the dataset
dataset <- read.csv('studentscores.csv') #时间和得分之间的关系

  Hours Scores
1   2.5     21
2   5.1     47
3   3.2     27
4   8.5     75
5   3.5     30
6   1.5     20

plot(dataset$Hours,dataset$Scores) #

2 数据预处理

首先按照上次分享的进行数据预处理  

R|ML_code-入门(1)

3 训练集和测试集

将数据按照4:1拆分,每一组分别包含自变量和因变量

# Splitting the dataset into the Training set and Test set
# install.packages('caTools')
library(caTools)
set.seed(123)
split = sample.split(dataset$Scores, SplitRatio = 1/4)
training_set <- subset(dataset, split == TRUE)
test_set <- subset(dataset, split == FALSE)

# Feature Scaling
# training_set <- scale(training_set)
# test_set <- scale(test_set)

4 模型拟合及预测

通过训练集进行模型拟合得到曲线,然后将测试集的X_test带入曲线中,得到预测结果y_pred,最后将预测结果y_pred与测试集中的y_test进行比较,确定预测是否准确。

# Fitting Simple Linear Regression to the Training set
regressor = lm(formula = Scores ~ Hours,
               data = training_set)

# Predicting the results
y_pred <- predict(regressor, newdata = test_set)

5 结果可视化

# Visualising the Training  results
library(ggplot2)
ggplot() +
geom_point(aes(x = training_set$Hours, y = training_set$Scores), colour = 'red') +
geom_line(aes(x = training_set$Hours, y = predict(regressor, newdata = training_set)), colour = 'blue') +
ggtitle('Scores vs Hours (Training set)') + xlab('Hours') + ylab('Scores')

# Visualising the Test  results
library(ggplot2)
ggplot() +
geom_point(aes(x = test_set$Hours, y = test_set$Scores), colour = 'red') +
geom_line(aes(x = training_set$Hours, y = predict(regressor, newdata = training_set)), colour = 'blue') +
ggtitle('Scores vs Hours (Test set)') + xlab('Hours') + ylab('Scores')

“github线性回归怎么实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程之家网站,小编将为大家输出更多高质量的实用文章!

相关文章

咱们在vscode中使用copilot的过程中,有可能会涉及到个人账号...
这篇文章给大家介绍怎么在GitHub上快速找到实用资源,内容非...
这篇文章主要介绍“github缓存穿透的解决方法是什么”,在日...
本篇内容介绍了“github线性回归怎么实现”的有关知识,在实...
怎样使用GitHub,很多新手对此不是很清楚,为了帮助大家解决...
今天小编给大家分享一下GitHub的高级搜索方法有哪些的相关知...