已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Logistic RegressionThe data我们将建立一个逻辑回归模型来预测一个学生是否被大学录取。假设你是一个大学系的管理员,你想根据两次考试的结果来决定每个申请人的录取机会。你有以前的申请人的历史数据,你可以用它作为逻辑回归的训练集。对于每一个培训例子,你有两个考试的申请人的分数和录取决定。为了做到这一点,我们将建立一个分类模型,根据考试成绩估计入学概率。#三大件import numpy as npimport pandas as pdimport matplotlib.pyplot as plt%matplotlib inlineimport os#path = data + os.sep + LogiReg_data.txtpdData = pd.read_csv(F:研究课程机器学习宇迪系列python数据分析与机器学习实战-全-E唐宇迪-机器学习课程资料机器学习算法配套案例实战梯度下降dataLogiReg_data.txt, header=None, names=Exam 1, Exam 2, Admitted)pdData.head()pdData.shapepositive = pdDatapdDataAdmitted = 1 # returns the subset of rows such Admitted = 1, i.e. the set of *positive* examplesnegative = pdDatapdDataAdmitted = 0 # returns the subset of rows such Admitted = 0, i.e. the set of *negative* examplesfig, ax = plt.subplots(figsize=(10,5)ax.scatter(positiveExam 1, positiveExam 2, s=30, c=b, marker=o, label=Admitted)ax.scatter(negativeExam 1, negativeExam 2, s=30, c=r, marker=x, label=Not Admitted)ax.legend()ax.set_xlabel(Exam 1 Score)ax.set_ylabel(Exam 2 Score)def sigmoid(z):return 1 / (1 + np.exp(-z)nums = np.arange(-10, 10, step=1) #creates a vector containing 20 equally spaced values from -10 to 10fig, ax = plt.subplots(figsize=(12,4)ax.plot(nums, sigmoid(nums), r)def model(X, theta): return sigmoid(np.dot(X, theta.T)pdData.insert(0, Ones, 1) # in a try / except structure so as not to return an error if the block si executed several times# set X (training data) and y (target variable)orig_data = pdData.as_matrix() # convert the Pandas representation of the data to an array useful for further computationscols = orig_data.shape1X = orig_data:,0:cols-1y = orig_data:,cols-1:cols# convert to numpy arrays and initalize the parameter array theta#X = np.matrix(X.values)#y = np.matrix(data.iloc:,3:4.values) #np.array(y.values)theta = np.zeros(1, 3)X:5y:5def cost(X, y, theta): left = np.multiply(-y, np.log(model(X, theta) right = np.multiply(1 - y, np.log(1 - model(X, theta)return np.sum(left - right) / (len(X)cost(X, y, theta)def gradient(X, y, theta): grad = np.zeros(theta.shape) error = (model(X, theta)- y).ravel() for j in range(len(theta.ravel(): #for each parmeter term = np.multiply(error, X:,j) grad0, j = np.sum(term) / len(X) return gradGradient descent比较3中不同梯度下降方法STOP_ITER = 0STOP_COST = 1STOP_GRAD = 2def stopCriterion(type, value, threshold): #设定三种不同的停止策略 if type = STOP_ITER: return value threshold elif type = STOP_COST: return abs(value-1-value-2) thresholdelif type = STOP_GRAD: return np.linalg.norm(value) = n: k = 0 X, y = shuffleData(data) #重新洗牌 theta = theta - alpha*grad # 参数更新 costs.append(cost(X, y, theta) # 计算新的损失 i += 1 if stopType = STOP_ITER: value = i elif stopType = STOP_COST: value = costs elif stopType = STOP_GRAD: value = grad if stopCriterion(stopType, value, thresh): break return theta, i-1, costs, grad, time.time() - init_timedef runExpe(data, theta, batchSize, stopType, thresh, alpha): #import pdb; pdb.set_trace(); theta, iter, costs, grad, dur = descent(data, theta, batchSize, stopType, thresh, alpha) name = Original if (data:,12).sum() 1 else Scaled name += data - learning rate: - .format(alpha) if batchSize=n: strDescType = Gradient elif batchSize=1: strDescType = Stochastic else: strDescType = Mini-batch ().format(batchSize) name += strDescType + descent - Stop: if stopType = STOP_ITER: strStop = iterations.format(thresh) elif stopType = STOP_COST: strStop = costs change .format(thresh) else: strStop = gradient norm = 0.5 else 0 for x in model(X, theta)scaled_X = scaled_data:, :3y = scaled_data:, 3predictions = predict(scaled_X,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年广东省珠海市高职单招职业适应性测试考试题库含答案详解【考试直接用】
- 农药使用全程质量管控规范手册
- 生物质基活性炭的孔结构调控与超级电容性能结题报告
- 2026年太行智造学院高职单招职业技能考试题库附参考答案详解(培优)
- 森林消防直升机吊桶操作手册
- 2027年贵州省贵阳市高职单招职业适应性测试考试题库【原创题】附答案详解
- 2026年湖南宁乡职业学院单招综合素质考试模拟试卷及答案详解【历年真题】
- 2027年青岛开放大学高职单招职业技能考试题库含完整答案详解(易错题)
- 2026年江苏省徐州市单招职业技能考试题库(综合卷)附答案详解
- 2024年青海恒瑞职业学院高职单招职业适应性测试考试题库汇编附答案详解
- 内蒙古呼和浩特市2024-2025学年高一年级下册期末学业质量监测考试数学试卷(解析版)
- 食堂食材验收员工培训
- Pilz安全PLC培训教程中文
- 2025年电梯培训考核题目及答案
- 医院保安保洁服务礼仪培训课件
- 公安流动人口管理课件
- 《接近开关原理与应用》课件
- 展会保密协议书范本
- 《浙江省中药饮片炮制规范》 2015年版
- 建筑力学与结构教学大纲2024
- TSG21-2025固定式压力容器安全技术(送审稿)
评论
0/150
提交评论