当前位置:网站首页>机器学习,吴恩达逻辑回归
机器学习,吴恩达逻辑回归
2022-07-23 06:15:00 【starmultiple】
一
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#路径来源
path = 'ex2data1.txt'
data = pd.read_csv(path, header=None, names=['Exam1 score', 'Exam2 score', 'Admitted'])
data.head()
data.describe()
#积极为1,消极为0
pos = data[data['Admitted'].isin([1])]
neg = data[data['Admitted'].isin([0])]
#画出图像
fig, ax = plt.subplots(figsize=(12, 8))
ax.scatter(pos['Exam1 score'], pos['Exam2 score'], s=50, c='black', marker='+', label='Admitted')
ax.scatter(neg['Exam1 score'], neg['Exam2 score'], s=50, c='y', marker='o', label='Not Admitted')
ax.legend()
ax.set_xlabel('Exam1 Score')
ax.set_ylabel('Exam2 Score')
plt.show()

二、sigmoid函数

def sigmoid(z):
return 1 / (1 + np.exp(-z))
nums = np.arange(-10, 10, step=0.5)
fig, ax = plt.subplots(figsize=(12, 8))
ax.plot(nums, sigmoid(nums), 'r')
plt.show()

三、代价函数

def cost(theta, X, Y):
first = Y * np.log(sigmoid([email protected].T))
second = (1 - Y) * np.log(1 - sigmoid([email protected].T))
return -1 * np.mean(first + second)
#预处理数据
# add ones column
data.insert(0, 'Ones', 1)
# set X(training data) and Y(target variable)
X = data.iloc[:, 0: -1].values
Y = data.iloc[:, -1].values
theta = np.zeros(3)
print(cost(theta, X, Y))#0.6931471805599453
四、梯度下降
#偏导公式
# 计算步长
def gradient(theta, X, Y):
return (1/len(X) * X.T @ (sigmoid(X @ theta.T) - Y))
print(gradient(theta, X, Y))#[ -0.1 -12.00921659 -11.26284221]
#gradient只是计算了梯度下降$\theta$更新的步长,使用Scipy.optimize.fmin_tnc拟合最优的$\theta$
#拟合参数
#拟合参数
import scipy.optimize as opt
result = opt.fmin_tnc(func=cost, x0=theta, fprime=gradient, args=(X, Y))
#print(result)#(array([-25.1613187 , 0.20623159, 0.20147149]), 36, 0)
type(result)
cost(result[0], X, Y)
#使用Scipy.optimize.minimize拟合最优的$\theta$公式
res = opt.minimize(fun=cost, x0=np.array(theta), args=(X, np.array(Y)), method='Newton-CG', jac=gradient)
cost(res.x, X, Y)

五、决策边界
coef = -res.x / res.x[2]
x = np.arange(30, 100, 0.5)
y = coef[0] + coef[1] * x
fig, ax = plt.subplots(figsize=(12, 8))
ax.scatter(pos['Exam1 score'], pos['Exam2 score'], s=50, c='black', marker='+', label='Admitted')
ax.scatter(neg['Exam1 score'], neg['Exam2 score'], s=50, c='y', marker='o', label='Not Admitted')
ax.plot(x, y, label='Decision Boundary', c='grey')
ax.legend()
ax.set_xlabel('Exam1 Score')
ax.set_ylabel('Exam2 Score')
plt.show()

边栏推荐
- 倍福PLC和C#通过ADS通信传输int类型变量
- 力扣 剑指 Offer II 094. 最少回文分割
- Quelle est la raison pour laquelle la plate - forme easygbs ne peut pas lire l'enregistrement vidéo et a un phénomène de streaming répété rtmp?
- Offer II 094. Minimum palindrome segmentation
- 【JZOF】10斐波那契数列
- Record a reptile question bank
- Why does the GOM engine version automatically drop the line or flash back?
- 【JZOF】12矩阵中的路径
- The relationship between method area, perpetual generation and meta space
- Numpy: element selection of matrix
猜你喜欢

MySQL - composite query external connection
Introduction to JVM memory model

GOM引擎版本为什么玩家会自动掉线或闪退?

信号完整性(SI)电源完整性(PI)学习笔记(三十二)电源分配网路(四)

图像处理 图像特征提取与描述
![[jzof] 10 Fibonacci series](/img/65/ab5b22b0879644c210abb38b0ce640.png)
[jzof] 10 Fibonacci series

Beifu PLC and C transmit string type through ads communication

Beihui information is 12 years old | happy birthday

What happens when you enter the web address and the web page is displayed

Opencv video operation
随机推荐
倍福PLC和C#通过ADS通信传输int数组类型变量
Why does the GOM engine version automatically drop the line or flash back?
方法区、永久代、元空间的关系
Shooting lesson 1-3: image Sprite
聊聊研发团队中的“人”
0722~线程池扩展
C language - big end storage and small end storage
Successful joint commissioning of Vientiane Aoke and CoDeSys Technology
费曼学习法(Redis总结)
【JZOF】10斐波那契数列
[jzof] 08 next node of binary tree
Complex networks - common drawing software and libraries
[jzof] 11 minimum number of rotation array
EasyGBS平臺出現錄像無法播放並存在RTMP重複推流現象,是什麼原因?
High voltage MOS tube knx42150 1500v/3a is applied to frequency converter power supply inverter, etc
MySQL - composite query external connection
Unity 模型显示到UI前面,后面的UI抖动
Current limiting based on redis+lua
The unity model is displayed in front of the UI, and the UI behind it jitters
数据进行脱敏