当前位置: 首页 > news >正文

中国铁路建设投资公司网站熊学军想开网站建设公司

中国铁路建设投资公司网站熊学军,想开网站建设公司,禅城南庄网站制作,用red5做直播网站目录 1、一元逻辑回归2、线性可分线性不可分3、Iris数据集实现多元逻辑回归4、绘制分类图5、鸢尾花分类图6、多分类问题#xff1a;#xff08;softmax回归#xff09;6.1、编码#xff1a;自然顺序码、独热编码、独冷编码6.2、二/多分类问题#xff1a;6.3、softmax… 目录 1、一元逻辑回归2、线性可分线性不可分3、Iris数据集实现多元逻辑回归4、绘制分类图5、鸢尾花分类图6、多分类问题softmax回归6.1、编码自然顺序码、独热编码、独冷编码6.2、二/多分类问题6.3、softmax回归6.4、非互斥的多分类问题 1、一元逻辑回归 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt #x是商品房面积y是对应的商品房类型 xnp.array([137.97,104.50,100.00,126.32,79.20,99.00,124.00,114.00,106.69,140.05,53.75,46.91,68.00,63.02,81.26,86.21]) ynp.array([1,1,0,1,0,1,1,0,0,1,0,0,0,0,0,0]) #plt.scatter(x,y) #可以看到横坐标面积都是大于40的正数 x_trainx-np.mean(x) #因为sigmoid函数是以0点为中心的所以需要中心化 y_trainy #plt.scatter(x,y)learn_rate0.005 #超参数 iter5 display_step1 #显示间隔np.random.seed(612) wtf.Variable(np.random.randn()) btf.Variable(np.random.randn()) cross_train[] #存放训练集的交叉熵损失 acc_train[] #存放训练集的分类准确率plt.scatter(x_train,y_train) x_range(-80,80) y_1/(1tf.exp(-(w*x_b))) plt.plot(x_,y_,colorred,linewidth3) #绘制使用初始的w、b值时的sigmoid函数曲线,此时w0,b0for i in range(0,iter1):with tf.GradientTape() as tape:#Sigmoid函数pred_train1/(1tf.exp(-(w*x_trainb)))#交叉熵损失函数Loss_train-tf.reduce_mean(y_train*tf.math.log(pred_train)(1-y_train)*tf.math.log(1-pred_train))#准确率Accuracy_traintf.reduce_mean(tf.cast(tf.equal(tf.where(pred_train0.5,0,1),y_train),tf.float32))cross_train.append(Loss_train)acc_train.append(Accuracy_train)dL_dw,dL_dbtape.gradient(Loss_train, [w,b])w.assign_sub(learn_rate*dL_dw)b.assign_sub(learn_rate*dL_db)if i%display_step0:print(i:%i,Train Loss:%f, Accuracy:%f %(i,Loss_train,Accuracy_train))y_1/(1tf.exp(-(w*x_b)))plt.plot(x_,y_) #绘制每次间隔后当前权值的sigmoid曲线训练测试数据不是测试集因为是数据无标签及其可视化 x_test[128.15,45.00,141.43,106.27,99.00,53.84,85.36,70.00,162.00,114.60] pred_test1/(1tf.exp(-(w*(x_test-np.mean(x))b))) y_testtf.where(pred_test0.5,0,1) for i in range(len(x_test)):print(x_test[i],\t,pred_test[i].numpy(),\t,y_test[i].numpy(),\t) #测试集数据的可视化 plt.scatter(x_test,y_test) y_1/(1tf.exp(-(w*x_b))) plt.plot(x_np.mean(x),y_) plt.show() #输出 128.15 0.8610252 1 45.0 0.0029561974 0 141.43 0.9545566 1 106.27 0.45318928 0 99.0 0.2981362 0 53.84 0.00663888 0 85.36 0.108105935 0 70.0 0.028681064 0 162.0 0.9928677 1 114.6 0.6406205 1 2、线性可分线性不可分 线性可分通过一条直线分开 与或非都是线性可分异或线性不可分 线性分类器 线性不可分无法通过一条直线分开但可以是2条直线或1条曲线 3、Iris数据集实现多元逻辑回归 属性选取花萼长度、宽度标签选山鸢尾Setosa、变色鸢尾Virginica 线性分类器 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import pandas as pd plt.rcParams[font.family] SimHei plt.rcParams[axes.unicode_minus]False #用来正常显示负号 #读取文件详见Python笔记10 train_pathtf.keras.utils.get_file(iris.csv, originNone) #获取文件的绝对路径 df_irispd.read_csv(train_path,header0) #结果是panda的二维数据表 irisnp.array(df_iris) #将二维数据表类型转化为二维数组类型shape(150,6)与视频中不一样索引号为0的是序号 xiris[:,1:3] #索引号1、2列属性花瓣长度和宽度,x.shape(150, 2) yiris[:,5] #train_y.shape(150,)x_svnp.concatenate((np.stack(x[ysetosa]), #选取2种花共100条数据以及其前2种属性np.stack(x[yversicolor])),axis0) y_svnp.concatenate((np.zeros(np.where(ysetosa)[0].size), #元组只有一个元素数组np.ones(np.where(yversicolor)[0].size)),axis0)np.random.seed(612) iris_randnp.concatenate((x_sv,np.expand_dims(y_sv,axis1)),axis1) np.random.shuffle(iris_rand) #打乱数组并选前面78条数据为训练集后面22条做测试集 x_trainiris_rand[:78,0:2] y_trainiris_rand[:78,2] x_testiris_rand[78:,0:2] y_testiris_rand[78:,2]#训练前的可视化 cm_ptmpl.colors.ListedColormap([blue,red]) #自定义颜色集合蓝色红色 #plt.scatter(x_train[:,0],x_train[:,1],cy_train,cmapcm_pt) #plt.show() #横坐标花萼长度纵坐标花萼宽度蓝色setosa红色versicolor尺度相同不用归一化x_trainx_train-np.mean(x_train,axis0) #中心化 x_testx_test-np.mean(x_test,axis0) #中心化 # plt.scatter(x_train[:,0],x_train[:,1],cy_train,cmapcm_pt) # plt.show()x0_trainnp.ones(len(x_train)).reshape(-1,1) #见机器学习笔记4——多元线性回归一样的 X_traintf.cast(tf.concat([x0_train,x_train],axis1),tf.float32) #X.shapeTensorShape([78, 3]) Y_traintf.constant(y_train.reshape(-1,1),tf.float32) #Y.shapeTensorShape([78, 1])x0_testnp.ones(len(x_test)).reshape(-1,1) #见上面训练集一样的 X_testtf.cast(tf.concat([x0_test,x_test],axis1),tf.float32) #X.shapeTensorShape([22, 3]) Y_testtf.constant(y_test.reshape(-1,1),tf.float32) #Y.shapeTensorShape([22, 1])learn_rate0.2 #超参数——学习率 iter120 #迭代次数 display_step30 #设置每迭代10次输出结果方便查看 np.random.seed(615) Wtf.Variable(np.random.randn(3,1),dtypetf.float32) #W列向量3行1列 ce_train[] #保存交叉熵损失 ce_test[] acc_train[] #保存准确率 acc_test[] #训练模型 for i in range(0,iter1):with tf.GradientTape() as tape:#Sigmoid函数,PRED是列向量是每个样品的预测概率PRED_train1/(1tf.exp(-tf.matmul(X_train,W)))PRED_test1/(1tf.exp(-tf.matmul(X_test,W)))#交叉熵损失函数Loss_train-tf.reduce_mean(Y_train*tf.math.log(PRED_train)(1-Y_train)*tf.math.log(1-PRED_train))Loss_test-tf.reduce_mean(Y_test*tf.math.log(PRED_test)(1-Y_test)*tf.math.log(1-PRED_test))#准确率训练集将预测值PRED_train二值化并与真实值Y_train比较Accuracy_traintf.reduce_mean(tf.cast(tf.equal(tf.where(PRED_train.numpy()0.5,0.,1.),Y_train),tf.float32))Accuracy_testtf.reduce_mean(tf.cast(tf.equal(tf.where(PRED_test.numpy()0.5,0.,1.),Y_test),tf.float32)) ce_train.append(Loss_train)ce_test.append(Loss_test)acc_train.append(Accuracy_train)acc_test.append(Accuracy_test)#只使用训练集来更新参数dL_dWtape.gradient(Loss_train, W)W.assign_sub(learn_rate*dL_dW)if i%display_step0:print(i:%i,\tTrainAcc:%f,TrainLoss:%f\tTestAcc:%f,TestLoss:%f %(i,Accuracy_train,Loss_train,Accuracy_test,Loss_test))#可视化 plt.figure(figsize(8,8)) #绘制损失和准确率的变化曲线 plt.subplot(221) plt.plot(ce_train,colorblue,labelTrain Loss) plt.plot(acc_train,colorred,labelTrain Acc) plt.legend() plt.subplot(223) plt.plot(ce_test,colorblue,labelTest Loss) plt.plot(acc_test,colorred,labelTest Acc) plt.legend() #绘制散点图、决策边界 plt.subplot(222) plt.scatter(x_train[:,0],x_train[:,1],cy_train,cmapcm_pt) x_[-1.5,1.5] y_-(W[1]*x_W[0])/W[2] plt.plot(x_,y_,colorg) plt.subplot(224) plt.scatter(x_test[:,0],x_test[:,1],cy_test,cmapcm_pt) plt.plot(x_,y_,colorg) plt.suptitle(训练集上测试集下) plt.show()输出 i:0, TrainAcc:0.474359,TrainLoss:0.887229 TestAcc:0.409091,TestLoss:0.854671 i:30, TrainAcc:0.846154,TrainLoss:0.464031 TestAcc:0.818182,TestLoss:0.448182 i:60, TrainAcc:0.961538,TrainLoss:0.317919 TestAcc:0.909091,TestLoss:0.318348 i:90, TrainAcc:0.987179,TrainLoss:0.244545 TestAcc:0.909091,TestLoss:0.256466 i:120, TrainAcc:1.000000,TrainLoss:0.200362 TestAcc:0.909091,TestLoss:0.2203434、绘制分类图 生成网格坐标矩阵np.meshgrid() 填充网格plt.pcolomesh() import numpy as np import matplotlib.pyplot as plt n10 xnp.linspace(-10,10,n) #生成等差数列 ynp.linspace(-10,10,n) X,Ynp.meshgrid(x,y) ZXY plt.pcolormesh(X,Y,Z,cmaprainbow) #X是横坐标Y是纵坐标Z来控制颜色cmap颜色方案 plt.show()也可以自定义颜色序列 绘制轮廓线plt.contour()、plt.contourf()自定义颜色方案cmmpl.colors.ListedColormap([“#FFA0A0”,“red”]) import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl n200 xnp.linspace(-10,10,n) #生成等差数列 ynp.linspace(-10,10,n) X,Ynp.meshgrid(x,y) ZXY #Ztf.where(Z0,0,1) #也可以用这个函数来设置阈值划分颜色 plt.figure(figsize(6,6)) #2种颜色 plt.subplot(221) cm_bg1mpl.colors.ListedColormap([#FFA0A0,#A0FFA2])#注意有中括号[] plt.pcolormesh(X,Y,Z,cmapcm_bg1) #用Z来控制颜色 #3种颜色 plt.subplot(222) cm_bg2mpl.colors.ListedColormap([#FFA0A0,#A0FFA2,#AA5555]) plt.pcolormesh(X,Y,Z,cmapcm_bg2) #用Z来控制颜色均分为3种颜色 #绘制轮廓图 plt.subplot(223) ZX**2Y**2 plt.contour(X,Y,Z,cmaprainbow) #用Z来控制颜色每一条线上的取值相同理解为等高线 #绘制轮廓并填充 plt.subplot(224) plt.contourf(X,Y,Z,cmaprainbow) plt.show()5、鸢尾花分类图 x_train的数值见上面“Iris数据集实现多元逻辑回归”代码 #绘制分类图 M300 x1_min,x2_minx_train.min(axis0) #算出训练集中花萼长度x1、花萼宽度x2的最大值最小值 x1_max,x2_maxx_train.max(axis0) m1,m2np.meshgrid(np.linspace(x1_min,x1_max,M), #绘制网格x1横坐标x2纵坐标np.linspace(x2_min,x2_max,M)) #linspace创建等差数列M是元素个数 X_meshtf.cast(np.stack((np.ones(M*M),m1.reshape(-1),m2.reshape(-1)),axis1),dtypetf.float32) #生成多元线性回归需要的矩阵shape(90000, 3)并转化为浮点数类型 Y_meshtf.cast(1/(1tf.exp(-tf.matmul(X_mesh,W))),dtypetf.float32) Y_meshtf.where(Y_mesh0.5,0,1) #0,1作为背景颜色填充的依据 ntf.reshape(Y_mesh,m1.shape) #维度变换使Y_mesh与m1有相同形状 cm_ptmpl.colors.ListedColormap([blue,red]) #散点图颜色方案 cm_bgmpl.colors.ListedColormap([#FFA022,#A0F111])#背景颜色方案 plt.pcolormesh(m1,m2,n,cmapcm_bg) plt.scatter(x_train[:,0],x_train[:,1],cy_train,cmapcm_pt)6、多分类问题softmax回归 6.1、编码自然顺序码、独热编码、独冷编码 自然顺序码转化为独热编码tf.one_hot(indices,depth) indices是一个整数depth是深度依次是用一个数字、一个向量独热是1独冷是0表示一个类别 6.2、二/多分类问题 二分类问题输入的x1、x2是样品的2个特征令x01因此可以看成是输入3个特征w0表示偏置项通过sigmoid函数转化为一个0~1之间的概率值逻辑回归多分类问题样品的标记常常表示为独热编码的形式如下图(0 0 1)^T其中红色框里面的向量是对应标记的概率softmax回归 交叉熵损失函数 二分类问题二元交叉熵损失函数BCE多分类问题多元交叉熵损失函数CCE 如下模型中A、B的准确率一样但是A中那个预测错的比较离谱计算知道A交叉熵损失较大 6.3、softmax回归 tf.nn.softmax()如下图使得输入序列中较大的数在输出中的概率更大。这也是广义线性回归的一种用来完成分类任务 例如鸢尾花数据集有4个属性加上偏置项x0输出有3个标签so模型参数矩阵W是5行3列 6.4、非互斥的多分类问题 互斥的多分类问题手写数字识别、鸢尾花识别、…非互斥的多分类问题包含人物的图片与包含汽车的图片识别、…
http://www.w-s-a.com/news/68334/

相关文章:

  • 优化一个网站多少钱网站开发北京
  • html教学关键词优化价格
  • 黄冈论坛网站有哪些给wordpress首页添加公告栏
  • 初中做数学题的网站做淘宝必备网站
  • 买拆车件上什么网站谁有那种手机网站
  • 一家专做有机蔬菜的网站万户网络是干嘛的
  • 十堰百度网站建设八宝山做网站公司
  • 地区电商网站系统建筑施工图纸培训班
  • 网站外包维护一年多少钱医院网站 功能
  • 电子商务市场的发展前景seo推广平台服务
  • 乐清网页设计公司哪家好seo推广任务小结
  • 360建筑网是什么pc优化工具
  • 越秀免费网站建设风景区网站建设项目建设可行性
  • 网站建站公司一站式服务学校网站开发招标
  • asp.net mvc 5 网站开发之美电商网站 流程图
  • 室内设计素材网站推荐郑州专业做淘宝网站建设
  • 新建的网站怎么做seo优化模板规格尺寸及价格
  • 平湖网站设计做电子元器件销售什么网站好
  • 可视化网站模板我想建个网站网站怎么建域名
  • 达州网站建设qinsanw南京市建设发展集团有限公司网站
  • django 网站开发实例公司排行榜
  • 韩国做美食网站阳江网站建设 公司价格
  • 网站开发哪里接业务长春高端模板建站
  • 深圳网站制作公司方案dw一个完整网页的代码
  • asp手机网站源码下载做seo推广网站
  • 网站优化建议怎么写网站维护主要有哪些内容和方法
  • 建设网站需要钱吗网络推广加盟
  • 高清素材图片的网站泰安网签备案查询
  • 自助网站建设怎么建设房地产的最新政策
  • 企业网站 生成html网站侵权怎么做公证或证据保存