当前位置:网站首页>[first song] machine learning of rebirth - linear regression
[first song] machine learning of rebirth - linear regression
2022-07-27 06:06:00 【Collapse an old face】
About 【 Head song 】 Linear regression theory and case practice Other units are only seen by fans , Students who want more learning resources pay attention to me ~
It's not easy to create , Before reference , Point a praise , Collection , You can't pay too much attention , Family
The first 1 Turn off : Data loading and analysis
Task description
Our mission : Write a small program that can load linear regression related data .
Programming requirements
The data in the actual combat content is a metadata , utilize pandas Read in the data file , And attach the name tag to the corresponding data , Respectively Population and Profit.
data = pd.read_csv(path, header= , names=[ ' ', ' ' ])
if __name__ == "__main__":
path = os.getcwd() + '/ex1data1.txt'
# utilize pandas Read in the data data, And the data attributes are named 'Population' and 'Profit'
#********* begin *********#
data=pd.read_csv(path,header=None,names=['Population','Profit'])
#********* end *********#
print(data.shape)The first 2 Turn off : Calculate the loss function
Programming requirements

According to the above formula , Write calculation loss function computeCost(X, y, theta), Finally back to cost.
X: I. metadata matrix , namelyPopulationdata ;y: Target data , namelyProfitdata ;theta: Model parameters ;cost: Loss function value .
Test instructions
Test input : nothing
Test output :the cost is: 32.0727338775
def computeCost(X, y, theta):
# Write the loss function calculation function according to the formula
#********* begin *********#
inner=np.power(((X*theta.T)-y),2)
cost=np.sum(inner)/(2*len(X))
cost=round(cost,10)
#********* end *********#
return costThe first 3 Turn off : Perform gradient descent to get a linear model
Programming requirements

According to the above formula , Write calculation loss function gradientDescent(X, y, theta, alpha, iters), Finally back to theta, cost.
x: I. metadata matrix , namelyPopulationdata ;y: Target data , namelyProfitdata ;theta: Model parameters ;m: Data scale ;α: Learning rate .
Test instructions
Test input : nothing
Test output : The model parameters are :[[-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)notes : The content is only for reference and sharing , Do not spread without permission , Tort made delete
边栏推荐
猜你喜欢
随机推荐
判断是否为回文结构的三种方法
[first song] rebirth of me in py introductory training (6): definition and application of functions
编程学习记录——第9课【操作符】
arcgis style样式表文件转换成geoserver sld文件
向量和矩阵的范数
Lightroom classic 2022 v11.4 Chinese version "latest resources"
C语言-动态内存管理
STM32 infrared remote control
19. Up and down sampling and batchnorm
Live Home 3D Pro室内家居设计工具
物联网操作系统
李宏毅 2020 深度学习与人类语言处理 DLHLP-Conditional Generation by RNN and Attention-p22
17. Attenuation of momentum and learning rate
c语言-线性顺序表
18. Convolutional neural network
[song] rebirth of me in py introduction training (5): List
Auto Encoder(AE),Denoising Auto Encoder(DAE), Variational Auto Encoder(VAE) 区别
[first song] Introduction to data science of rebirth -- return to advanced level
PZK学C语言之数据类型,进制转换,输入输出,运算符,分支语句ifelse
Live Home 3D Pro interior home design tool

![[MySQL learning] 8](/img/25/84d5acbdd8aba3455ab8e3eb17dfa8.png)






![[MVC Architecture] MVC model](/img/71/e10da490d5f0098c64b33e77d158e7.png)