资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Java线性回归基础代码怎么写

这篇文章主要介绍“Java线性回归基础代码怎么写”,在日常操作中,相信很多人在Java线性回归基础代码怎么写问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java线性回归基础代码怎么写”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

我们提供的服务有:成都网站设计、成都网站制作、微信公众号开发、网站优化、网站认证、琼山ssl等。为1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的琼山网站制作公司

# Use linear model to model this data.
from sklearn.linear_model import LinearRegression
import numpy as np
lr=LinearRegression()
lr.fit(pga.distance[:,np.newaxis],pga['accuracy']) # Another way is using pga[['distance']]
theta0=lr.intercept_
theta1=lr.coef_
print(theta0)
print(theta1)
#calculating cost-function for each theta1
#计算平均累积误差
def cost(x,y,theta0,theta1):
 J=0
 for i in range(len(x)):
 mse=(x[i]*theta1+theta0-y[i])**2
 J+=mse
 return J/(2*len(x))
theta0=100
theta1s = np.linspace(-3,2,197)
costs=[]
for theta1 in theta1s:
 costs.append(cost(pga['distance'],pga['accuracy'],theta0,theta1))
plt.plot(theta1s,costs)
plt.show()
print(pga.distance)
#调整theta
def partial_cost_theta0(x,y,theta0,theta1):
 #我们的模型是线性拟合函数时:y=theta1*x + theta0,而不是sigmoid函数,当非线性时我们可以用sigmoid
 #直接多整个x series操作,省的一个一个计算,最终求sum 再平均
 h=theta1*x+theta0 
 diff=(h-y)
 partial=diff.sum()/len(diff)
 return partial
partial0=partial_cost_theta0(pga.distance,pga.accuracy,1,1)
def partial_cost_theta1(x,y,theta0,theta1):
 #我们的模型是线性拟合函数:y=theta1*x + theta0,而不是sigmoid函数,当非线性时我们可以用sigmoid
 h=theta1*x+theta0
 diff=(h-y)*x
 partial=diff.sum()/len(diff)
 return partial
partial1=partial_cost_theta1(pga.distance,pga.accuracy,0,5)
print(partial0)
print(partial1)
def gradient_descent(x,y,alpha=0.1,theta0=0,theta1=0): #设置默认参数
 #计算成本
 #调整权值
 #计算错误代价,判断是否收敛或者达到最大迭代次数
 most_iterations=1000
 convergence_thres=0.000001 
 
 c=cost(x,y,theta0,theta1)
 costs=[c]
 cost_pre=c+convergence_thres+1.0
 
 counter=0
 while( (np.abs(c-cost_pre)>convergence_thres) & (counterhttp://cdkjz.cn/article/pceegc.html
        
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220