sigmoid 函数
print("===============sigmoid函数=============")
#g代表一个常用的逻辑函数(logistic function)为S形函数。
def sigmoid(z):
return 1/(1+np.exp(-z))#np.exp表示e的多少次方
nums=np.arange(-10,10,step=1)
print(nums)
fig,ax=plt.subplots(figsize=(12,8))
ax.plot(nums,sigmoid(nums),'r')
plt.show()
def cost(theta,X,y):
theta=np.matrix(theta)
X=np.matrix(X)
y=np.matrix(y)
first=np.multiply(-y,np.log(sigmoid(X*theta.T)))
second=np.multiply((1-y),np.log(1-sigmoid(X*theta.T)))
return np.sum(first-second)/len(X)
#add a ones column - this makes the matrix multiplication work out easier
data.insert(0,'One',1)
#set X(training data) and (target variable)
cols=data.shape[1]
X=data.iloc[:,0:cols-1]
y=data.iloc[:,cols-1:cols]
#convert to numpy arrays and initalize the parameter array theta
X=np.array(X.values)#转换成矩阵或者向量
y=np.array(y.values)
theta =np.zeros(3)
print(X.shape,y.shape,theta.shape)
print(cost(theta, X, y))
- 总结:如果喜欢人工智能的小伙伴,推荐去看吴恩达教授的机器学习,我目前也在学习,老师讲的课通俗易懂。最后本次内容仅作为学习使用。如有侵权,联系删除
- 点赞666和收藏,私信分享源码和数据训练集