当前位置:网站首页>【头歌】重生之机器学习-线性回归
【头歌】重生之机器学习-线性回归
2022-07-27 05:19:00 【垮起个老脸】
关于【头歌】线性回归理论与案例实践的其他单元仅粉丝看见,想要更多学习资源的同学关注我哦~
创作不易,参考之前,点个赞,收藏,关注一下不过分吧,家人们
第1关:数据载入与分析
任务描述
本关任务:编写一个能够载入线性回归相关数据的小程序。
编程要求
该实战内容中数据为一元数据,利用 pandas 读入数据文件,并为相应的数据附上名字标签,分别为Population 和 Profit。
data = pd.read_csv(path, header= , names=[ ' ', ' ' ])
if __name__ == "__main__":
path = os.getcwd() + '/ex1data1.txt'
#利用pandas读入数据data,并将数据属性分别命名为'Population'和'Profit'
#********* begin *********#
data=pd.read_csv(path,header=None,names=['Population','Profit'])
#********* end *********#
print(data.shape)第2关:计算损失函数
编程要求

根据以上公式,编写计算损失函数computeCost(X, y, theta),最后返回cost。
X:一元数据矩阵,即Population数据;y:目标数据,即Profit数据;theta:模型参数;cost:损失函数值。
测试说明
测试输入:无
测试输出:the cost is: 32.0727338775
def computeCost(X, y, theta):
#根据公式编写损失函数计算函数
#********* begin *********#
inner=np.power(((X*theta.T)-y),2)
cost=np.sum(inner)/(2*len(X))
cost=round(cost,10)
#********* end *********#
return cost第3关:进行梯度下降得到线性模型
编程要求

根据以上公式,编写计算损失函数gradientDescent(X, y, theta, alpha, iters),最后返回theta, cost。
x:一元数据矩阵,即Population数据;y:目标数据,即Profit数据;theta:模型参数;m:数据规模;α: 学习率。
测试说明
测试输入:无
测试输出:模型参数为:[[-3.241402141.1272942]]
def gradientDescent(X, y, theta, alpha, iters):
temp = np.matrix(np.zeros(theta.shape))
parameters = int(theta.ravel().shape[1])
cost = np.zeros(iters)
for i in range(iters):
error = (X * theta.T) - y
for j in range(parameters):
#********* begin *********#
term=np.multiply(error,X[:,j])
temp[0,j]=theta[0,j]-((alpha/len(X))*np.sum(term))
#********* end *********#
theta = temp
cost[i] = computeCost(X, y, theta)注:内容只做参考和分享,未经允许不可传播,侵权立删
边栏推荐
- Rk3288 board HDMI displays logo images of uboot and kernel
- 10. Gradient, activation function and loss
- 1. Introduction to pytorch
- 3.分类问题---手写数字识别初体验
- 【Arduino】重生之Arduino 学僧(1)
- 视觉横向课题bug1:FileNotFoundError: Could not find module ‘MvCameraControl.dll‘ (or one of it
- 【mysql学习】8
- 关于pytorch转onnx经常出现的问题
- Day 9. Graduate survey: A love–hurt relationship
- 维度问题以及等高线
猜你喜欢

Global evidence of expressed sentimental alterations during the covid-19 pandemics

基于深度神经网络的社交媒体用户级心理压力检测

Auto Encoder(AE),Denoising Auto Encoder(DAE), Variational Auto Encoder(VAE) 区别

Chrome 如何快速将一组正在浏览的网页(tabs)转移到另一台设备(电脑)上

面试常问Future、FutureTask和CompletableFuture

Day 7. Towards Preemptive Detection of Depression and Anxiety in Twitter

数字图像处理——第六章 彩色图像处理

Social media user level psychological stress detection based on deep neural network

Day 6.重大医疗伤害事件网络舆情能量传播过程分析*———以“魏则西事件”为例

贪心高性能神经网络与AI芯片应用研修
随机推荐
Day10. Work organization and mental health problems in PhD students
leetcode系列(一):买卖股票
Chrome 如何快速将一组正在浏览的网页(tabs)转移到另一台设备(电脑)上
数字图像处理第四章——频率域滤波
Digital image processing Chapter 2 fundamentals of digital image
关于pytorch转onnx经常出现的问题
【mysql学习】8
pytorch中交叉熵损失函数的细节
西瓜书学习笔记---第一、二章
【MVC架构】MVC模型
Day14. Using interpretable machine learning method to distinguish intestinal tuberculosis and Crohn's disease
Day14. 用可解释机器学习方法鉴别肠结核和克罗恩病
pytorch的多GPU训练的两种方式
子类调用父类构造函数的时机
18.卷积神经网络
GBASE 8C——SQL参考6 sql语法(4)
Digital image processing Chapter 4 - frequency domain filtering
Day 4.Social Data Sentiment Analysis: Detection of Adolescent Depression Signals
数字图像处理 第一章 绪论
Gbase 8C - SQL reference 6 SQL syntax (11)