#加载包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
path = 'LogiReg_data.txt'
#header=None:表示文件中的第一行不会被默认设置成列名。
#names用来设置列名
pdData = pd.read_csv(path,header=None,sep = ',',names=['Exam 1','Exam 2','Admitted'])
pdData.head() %head函数默认读取前五行
pdData.shape
#数据可视化展示
positive=pdData[pdData['Admitted']==1]
negative=pdData[pdData['Admitted']==0]
fig,ax = plt.subplots(figsize=(10,5))
#plt.subplots()是一个函数,返回一个包含figure和axes对象的元组。
#因此,使用fig,ax = plt.subplots()将元组分解为fig和ax两个变量。
ax.scatter(positive['Exam 1'],positive['Exam 2'],s=30,c='b',marker='o',label='Admitted')
ax.scatter(negative['Exam 1'], negative['Exam 2'], s=30, c='r', marker='x', label='Not Admitted')
ax.legend()
ax.set_xlabel('Exam 1 Score')
ax.set_ylabel('Exam 2 Score')
The logistic regression
目标:建立分类器(求解出三个参数012)
设定阈值,根据阈值判断录取结果
要完成的模块
sigmod:映射到概率的函数
model:返回预测结果值
cost:根据参数计算损失
gradient:计算每个参数的梯度方向
descent:进行参数更新
accuracy:计算精度
#sigmod:映射到概率的函数
def sigmoid(z):
return 1/(1+np.exp(-z))
#创建一个从-10到10大小为20的向量
nums=np.arange(-10,10,1)
fig,ax = plt.subplots(figsize=(12,4))
ax.plot(nums,sigmoid(nums),'r')