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

qq炫舞做浴缸的网站家具网站建设方案

qq炫舞做浴缸的网站,家具网站建设方案,六安人论坛百姓杂谈,建筑工程管理适合女生吗回归 概念 假设现在有一些数据点#xff0c;我们用一条直线对这些点进行拟合#xff08;这条直线称为最佳拟合直线#xff09;#xff0c;这个拟合的过程就叫做回归。进而可以得到对这些点的拟合直线方程。 最后结果用sigmoid函数输出 因此#xff0c;为了实现 Logisti…回归 概念 假设现在有一些数据点我们用一条直线对这些点进行拟合这条直线称为最佳拟合直线这个拟合的过程就叫做回归。进而可以得到对这些点的拟合直线方程。 最后结果用sigmoid函数输出 因此为了实现 Logistic 回归分类器我们可以在每个特征上都乘以一个回归系数如下公式所示然后把所有结果值相加将这个总和代入 Sigmoid 函数中进而得到一个范围在 0~1 之间的数值。任何大于 0.5 的数据被分入 1 类小于 0.5 即被归入 0 类。所以Logistic 回归也是一种概率估计比如这里Sigmoid 函数得出的值为0.5可以理解为给定数据和参数数据被分入 1 类的概率为0.5 逻辑回归实际上就是一层神经网络 sigmoid函数的输入 梯度上升 梯度梯度值梯度方向 要找到某函数的最大值最好的方法是沿着该函数的梯度方向探寻。如果梯度记为 ▽ 则函数 f(x, y) 的梯度由下式表示: 上式表示要沿x的方向移动沿y方向移动。其中函数f(x,y)必须要在待计算的点上有定义并且可微。 梯度下降和梯度上升 其实这个两个方法在此情况下本质上是相同的。关键在于代价函数cost function或者叫目标函数objective function。如果目标函数是损失函数那就是最小化损失函数来求函数的最小值就用梯度下降。 如果目标函数是似然函数Likelihood function就是要最大化似然函数来求函数的最大值那就用梯度上升。在逻辑回归中 损失函数和似然函数无非就是互为正负关系。 只需要在迭代公式中的加法变成减法。因此对应的公式可以写成 from __future__ import print_function from numpy import * import matplotlib.pyplot as plt# --------------------------------------------------------------------------- # 使用 Logistic 回归在简单数据集上的分类# 解析数据 def loadDataSet(file_name):Desc: 加载并解析数据Args:file_name -- 文件名称要解析的文件所在磁盘位置Returns:dataMat -- 原始数据的特征labelMat -- 原始数据的标签也就是每条样本对应的类别# dataMat为原始数据 labelMat为原始数据的标签dataMat []labelMat []fr open(file_name)for line in fr.readlines():lineArr line.strip().split()if len(lineArr) 1:continue # 这里如果就一个空的元素则跳过本次循环# 为了方便计算我们将 X0 的值设为 1.0 也就是在每一行的开头添加一个 1.0 作为 X0dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])labelMat.append(int(lineArr[2]))return dataMat, labelMat# sigmoid跳跃函数 def sigmoid(inX):# return 1.0 / (1 exp(-inX))# Tanh是Sigmoid的变形与 sigmoid 不同的是tanh 是0均值的。因此实际应用中tanh 会比 sigmoid 更好。return 2 * 1.0/(1exp(-2*inX)) - 1# 正常的处理方案 # 两个参数: 第一个参数 dataMatIn 是一个2维NumPy数组每列分别代表每个不同的特征每行则代表每个训练样本。 # 第二个参数 classLabels 是类别标签它是一个 1*100 的行向量。为了便于矩阵计算需要将该行向量转换为列向量做法是将原向量转置再将它赋值给labelMat。 def gradAscent(dataMatIn, classLabels):Desc:正常的梯度上升法Args:dataMatIn -- 输入的 数据的特征 ListclassLabels -- 输入的数据的类别标签Returns:array(weights) -- 得到的最佳回归系数# 转化为矩阵[[1,1,2],[1,1,2]....]dataMatrix mat(dataMatIn) # 转换为 NumPy 矩阵# 转化为矩阵[[0,1,0,1,0,1.....]]并转制[[0],[1],[0].....]# transpose() 行列转置函数# 将行向量转化为列向量 矩阵的转置labelMat mat(classLabels).transpose() # 首先将数组转换为 NumPy 矩阵然后再将行向量转置为列向量# m-数据量样本数 n-特征数m, n shape(dataMatrix)# print m, n, __*10, shape(dataMatrix.transpose()), __*100# alpha代表向目标移动的步长alpha 0.001# 迭代次数maxCycles 500# 生成一个长度和特征数相同的矩阵此处n为3 - [[1],[1],[1]]# weights 代表回归系数 此处的 ones((n,1)) 创建一个长度和特征数相同的矩阵其中的数全部都是 1weights ones((n, 1))for k in range(maxCycles): # heavy on matrix operations# m*3 的矩阵 * 3*1 的单位矩阵 m*1的矩阵# 那么乘上单位矩阵的意义就代表: 通过公式得到的理论值# 参考地址: 矩阵乘法的本质是什么 https://www.zhihu.com/question/21351965/answer/31050145# print dataMatrix, dataMatrix # print weights, weights# n*3 * 3*1 n*1h sigmoid(dataMatrix * weights) # 矩阵乘法# print hhhhhhh, h# labelMat是实际值error (labelMat - h) # 向量相减# 0.001* (3*m)*(m*1) 表示在每一个列上的一个误差情况最后得出 x1,x2,xn的系数的偏移量weights weights alpha * dataMatrix.transpose() * error # 矩阵乘法最后得到回归系数return array(weights)# 随机梯度下降 # 梯度下降优化算法在每次更新数据集时都需要遍历整个数据集计算复杂都较高 # 随机梯度下降一次只用一个样本点来更新回归系数 def stocGradAscent0(dataMatrix, classLabels):Desc:随机梯度下降只使用一个样本点来更新回归系数Args:dataMatrix -- 输入数据的数据特征除去最后一列classLabels -- 输入数据的类别标签最后一列数据Returns:weights -- 得到的最佳回归系数m, n shape(dataMatrix)alpha 0.01# n*1的矩阵# 函数ones创建一个全1的数组weights ones(n) # 初始化长度为n的数组元素全部为 1for i in range(m):# sum(dataMatrix[i]*weights)为了求 f(x)的值 f(x)a1*x1b2*x2..nn*xn,此处求出的 h 是一个具体的数值而不是一个矩阵h sigmoid(sum(dataMatrix[i] * weights))# print dataMatrix[i], dataMatrix[i]# 计算真实类别与预测类别之间的差值然后按照该差值调整回归系数error classLabels[i] - h# 0.01*(1*1)*(1*n)# print weights, * * 10, dataMatrix[i], * * 10, errorweights weights alpha * error * dataMatrix[i]return weights# 随机梯度下降算法随机化 def stocGradAscent1(dataMatrix, classLabels, numIter150):Desc:改进版的随机梯度下降使用随机的一个样本来更新回归系数Args:dataMatrix -- 输入数据的数据特征除去最后一列数据classLabels -- 输入数据的类别标签最后一列数据numIter150 -- 迭代次数Returns:weights -- 得到的最佳回归系数m, n shape(dataMatrix)weights ones(n) # 创建与列数相同的矩阵的系数矩阵所有的元素都是1# 随机梯度, 循环150,观察是否收敛for j in range(numIter):# [0, 1, 2 .. m-1]dataIndex range(m)for i in range(m):# i和j的不断增大导致alpha的值不断减少但是不为0alpha 4 / (1.0 j i) 0.0001 # alpha 会随着迭代不断减小但永远不会减小到0因为后边还有一个常数项0.0001# 随机产生一个 0len()之间的一个值# random.uniform(x, y) 方法将随机生成下一个实数它在[x,y]范围内,x是这个范围内的最小值y是这个范围内的最大值。randIndex int(random.uniform(0, len(dataIndex)))# sum(dataMatrix[i]*weights)为了求 f(x)的值 f(x)a1*x1b2*x2..nn*xnh sigmoid(sum(dataMatrix[dataIndex[randIndex]] * weights))error classLabels[dataIndex[randIndex]] - h# print weights, __h%s % h, __*20, alpha, __*20, error, __*20, dataMatrix[randIndex]weights weights alpha * error * dataMatrix[dataIndex[randIndex]]del (dataIndex[randIndex])return weights# 可视化展示 def plotBestFit(dataArr, labelMat, weights):Desc:将我们得到的数据可视化展示出来Args:dataArr:样本数据的特征labelMat:样本数据的类别标签即目标变量weights:回归系数Returns:Nonen shape(dataArr)[0]xcord1 []ycord1 []xcord2 []ycord2 []for i in range(n):if int(labelMat[i]) 1:xcord1.append(dataArr[i, 1])ycord1.append(dataArr[i, 2])else:xcord2.append(dataArr[i, 1])ycord2.append(dataArr[i, 2])fig plt.figure()ax fig.add_subplot(111)ax.scatter(xcord1, ycord1, s30, cred, markers)ax.scatter(xcord2, ycord2, s30, cgreen)x arange(-3.0, 3.0, 0.1)y的由来卧槽是不是没看懂首先理论上是这个样子的。dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])w0*x0w1*x1w2*x2f(x)x0最开始就设置为1叻 x2就是我们画图的y值而f(x)被我们磨合误差给算到w0,w1,w2身上去了所以: w0w1*xw2*y0 y (-w0-w1*x)/w2 y (-weights[0] - weights[1] * x) / weights[2]ax.plot(x, y)plt.xlabel(X)plt.ylabel(Y)plt.show()def simpleTest():# 1.收集并准备数据dataMat, labelMat loadDataSet(data/5.Logistic/TestSet.txt)# print dataMat, ---\n, labelMat# 2.训练模型 f(x)a1*x1b2*x2..nn*xn中 (a1,b2, .., nn).T的矩阵值# 因为数组没有是复制n份 array的乘法就是乘法dataArr array(dataMat)# print dataArr# weights gradAscent(dataArr, labelMat)# weights stocGradAscent0(dataArr, labelMat)weights stocGradAscent1(dataArr, labelMat)# print **30, weights# 数据可视化plotBestFit(dataArr, labelMat, weights) # -------------------------------------------------------------------------------- # 从疝气病症预测病马的死亡率 # 分类函数根据回归系数和特征向量来计算 Sigmoid的值 def classifyVector(inX, weights):Desc: 最终的分类函数根据回归系数和特征向量来计算 Sigmoid 的值大于0.5函数返回1否则返回0Args:inX -- 特征向量featuresweights -- 根据梯度下降/随机梯度下降 计算得到的回归系数Returns:如果 prob 计算大于 0.5 函数返回 1否则返回 0prob sigmoid(sum(inX * weights))if prob 0.5: return 1.0else: return 0.0# 打开测试集和训练集,并对数据进行格式化处理 def colicTest():Desc:打开测试集和训练集并对数据进行格式化处理Args:NoneReturns:errorRate -- 分类错误率frTrain open(data/5.Logistic/horseColicTraining.txt)frTest open(data/5.Logistic/horseColicTest.txt)trainingSet []trainingLabels []# 解析训练数据集中的数据特征和Labels# trainingSet 中存储训练数据集的特征trainingLabels 存储训练数据集的样本对应的分类标签for line in frTrain.readlines():currLine line.strip().split(\t)lineArr []for i in range(21):lineArr.append(float(currLine[i]))trainingSet.append(lineArr)trainingLabels.append(float(currLine[21]))# 使用 改进后的 随机梯度下降算法 求得在此数据集上的最佳回归系数 trainWeightstrainWeights stocGradAscent1(array(trainingSet), trainingLabels, 500)# trainWeights stocGradAscent0(array(trainingSet), trainingLabels)errorCount 0numTestVec 0.0# 读取 测试数据集 进行测试计算分类错误的样本条数和最终的错误率for line in frTest.readlines():numTestVec 1.0currLine line.strip().split(\t)lineArr []for i in range(21):lineArr.append(float(currLine[i]))if int(classifyVector(array(lineArr), trainWeights)) ! int(currLine[21]):errorCount 1errorRate (float(errorCount) / numTestVec)print(the error rate of this test is: %f % errorRate)return errorRate# 调用 colicTest() 10次并求结果的平均值 def multiTest():numTests 10errorSum 0.0for k in range(numTests):errorSum colicTest()print(after %d iterations the average error rate is: %f % (numTests, errorSum / float(numTests)))simpleTest() # multiTest() # 逻辑回归中的 L1 惩罚和稀缺性 L1 Penalty and Sparsity in Logistic Regression print(__doc__)import numpy as np import matplotlib.pyplot as pltfrom sklearn.linear_model import LogisticRegression from sklearn import datasets from sklearn.preprocessing import StandardScalerdigits datasets.load_digits()X, y digits.data, digits.target X StandardScaler().fit_transform(X)# 将大小数字分类为小 y (y 4).astype(np.int)# 设置正则化参数 for i, C in enumerate((100, 1, 0.01)):# 减少训练时间短的容忍度clf_l1_LR LogisticRegression(CC, penaltyl1, tol0.01)clf_l2_LR LogisticRegression(CC, penaltyl2, tol0.01)clf_l1_LR.fit(X, y)clf_l2_LR.fit(X, y)coef_l1_LR clf_l1_LR.coef_.ravel()coef_l2_LR clf_l2_LR.coef_.ravel()# coef_l1_LR contains zeros due to the# L1 sparsity inducing norm# 由于 L1 稀疏诱导规范coef_l1_LR 包含零sparsity_l1_LR np.mean(coef_l1_LR 0) * 100sparsity_l2_LR np.mean(coef_l2_LR 0) * 100print(C%.2f % C)print(Sparsity with L1 penalty: %.2f%% % sparsity_l1_LR)print(score with L1 penalty: %.4f % clf_l1_LR.score(X, y))print(Sparsity with L2 penalty: %.2f%% % sparsity_l2_LR)print(score with L2 penalty: %.4f % clf_l2_LR.score(X, y))l1_plot plt.subplot(3, 2, 2 * i 1)l2_plot plt.subplot(3, 2, 2 * (i 1))if i 0:l1_plot.set_title(L1 penalty)l2_plot.set_title(L2 penalty)l1_plot.imshow(np.abs(coef_l1_LR.reshape(8, 8)), interpolationnearest,cmapbinary, vmax1, vmin0)l2_plot.imshow(np.abs(coef_l2_LR.reshape(8, 8)), interpolationnearest,cmapbinary, vmax1, vmin0)plt.text(-8, 3, C %.2f % C)l1_plot.set_xticks(())l1_plot.set_yticks(())l2_plot.set_xticks(())l2_plot.set_yticks(())plt.show() # 具有 L1-逻辑回归的路径 from datetime import datetime import numpy as np import matplotlib.pyplot as pltfrom sklearn import linear_model from sklearn import datasets from sklearn.svm import l1_min_ciris datasets.load_iris() X iris.data y iris.targetX X[y ! 2] y y[y ! 2]X - np.mean(X, 0)cs l1_min_c(X, y, losslog) * np.logspace(0, 3)print(Computing regularization path ...) start datetime.now() clf linear_model.LogisticRegression(C1.0, penaltyl1, tol1e-6) coefs_ [] for c in cs:clf.set_params(Cc)clf.fit(X, y)coefs_.append(clf.coef_.ravel().copy()) print(This took , datetime.now() - start)coefs_ np.array(coefs_) plt.plot(np.log10(cs), coefs_) ymin, ymax plt.ylim() plt.xlabel(log(C)) plt.ylabel(Coefficients) plt.title(Logistic Regression Path) plt.axis(tight) plt.show() # 绘制多项式和一对二的逻辑回归 Plot multinomial and One-vs-Rest Logistic Regression print(__doc__)import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_blobs from sklearn.linear_model import LogisticRegression# 制作 3 类数据集进行分类 centers [[-5, 0], [0, 1.5], [5, -1]] X, y make_blobs(n_samples1000, centerscenters, random_state40) transformation [[0.4, 0.2], [-0.4, 1.2]] X np.dot(X, transformation)for multi_class in (multinomial, ovr):clf LogisticRegression(solversag, max_iter100, random_state42,multi_classmulti_class).fit(X, y)# 打印训练分数print(training score : %.3f (%s) % (clf.score(X, y), multi_class))# 创建一个网格来绘制h .02 # 网格中的步长x_min, x_max X[:, 0].min() - 1, X[:, 0].max() 1y_min, y_max X[:, 1].min() - 1, X[:, 1].max() 1xx, yy np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h))# 绘制决策边界。为此我们将为网格 [x_min, x_max]x[y_min, y_max]中的每个点分配一个颜色。Z clf.predict(np.c_[xx.ravel(), yy.ravel()])# 将结果放入彩色图Z Z.reshape(xx.shape)plt.figure()plt.contourf(xx, yy, Z, cmapplt.cm.Paired)plt.title(Decision surface of LogisticRegression (%s) % multi_class)plt.axis(tight)# 将训练点也绘制进入colors bryfor i, color in zip(clf.classes_, colors):idx np.where(y i)plt.scatter(X[idx, 0], X[idx, 1], ccolor, cmapplt.cm.Paired)# 绘制三个一对数分类器xmin, xmax plt.xlim()ymin, ymax plt.ylim()coef clf.coef_intercept clf.intercept_def plot_hyperplane(c, color):def line(x0):return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1]plt.plot([xmin, xmax], [line(xmin), line(xmax)],ls--, colorcolor)for i, color in zip(clf.classes_, colors):plot_hyperplane(i, color)plt.show()from __future__ import print_function# Logistic Regression 3-class Classifier 逻辑回归 3-类 分类器 print(__doc__)import numpy as np import matplotlib.pyplot as plt from sklearn import linear_model, datasets# 引入一些数据来玩 iris datasets.load_iris() # 我们只采用样本数据的前两个feature X iris.data[:, :2] Y iris.targeth .02 # 网格中的步长logreg linear_model.LogisticRegression(C1e5)# 我们创建了一个 Neighbours Classifier 的实例并拟合数据。 logreg.fit(X, Y)# 绘制决策边界。为此我们将为网格 [x_min, x_max]x[y_min, y_max] 中的每个点分配一个颜色。 x_min, x_max X[:, 0].min() - .5, X[:, 0].max() .5 y_min, y_max X[:, 1].min() - .5, X[:, 1].max() .5 xx, yy np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) Z logreg.predict(np.c_[xx.ravel(), yy.ravel()])# 将结果放入彩色图中 Z Z.reshape(xx.shape) plt.figure(1, figsize(4, 3)) plt.pcolormesh(xx, yy, Z, cmapplt.cm.Paired)# 将训练点也同样放入彩色图中 plt.scatter(X[:, 0], X[:, 1], cY, edgecolorsk, cmapplt.cm.Paired) plt.xlabel(Sepal length) plt.ylabel(Sepal width)plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.xticks(()) plt.yticks(())plt.show()# Logistic function 逻辑回归函数 # 这个类似于咱们之前讲解 logistic 回归的 Sigmoid 函数模拟的阶跃函数 print(__doc__)import numpy as np import matplotlib.pyplot as pltfrom sklearn import linear_model# 这是我们的测试集它只是一条直线带有一些高斯噪声。 xmin, xmax -5, 5 n_samples 100 np.random.seed(0) X np.random.normal(sizen_samples) y (X 0).astype(np.float) X[X 0] * 4 X .3 * np.random.normal(sizen_samples)X X[:, np.newaxis] # 运行分类器 clf linear_model.LogisticRegression(C1e5) clf.fit(X, y)# 并且画出我们的结果 plt.figure(1, figsize(4, 3)) plt.clf() plt.scatter(X.ravel(), y, colorblack, zorder20) X_test np.linspace(-5, 10, 300)def model(x):return 1 / (1 np.exp(-x)) loss model(X_test * clf.coef_ clf.intercept_).ravel() plt.plot(X_test, loss, colorred, linewidth3)ols linear_model.LinearRegression() ols.fit(X, y) plt.plot(X_test, ols.coef_ * X_test ols.intercept_, linewidth1) plt.axhline(.5, color.5)plt.ylabel(y) plt.xlabel(X) plt.xticks(range(-5, 10)) plt.yticks([0, 0.5, 1]) plt.ylim(-.25, 1.25) plt.xlim(-4, 10) plt.legend((Logistic Regression Model, Linear Regression Model),loclower right, fontsizesmall) plt.show()
http://www.tj-hxxt.cn/news/132247.html

相关文章:

  • 网站使用方法设计什么网站简单
  • 电商网站建设懂你所需网站策划选题
  • 适合做公司网站的cmswordpress 精简
  • 云南网站设计平台flash 好的网站
  • 广州知名网站建设兰州做网站客户
  • 行业网站策划方案网页微信电脑版
  • 建设网站需要的安全设备网站建设性意见表
  • 招聘高级网站建设网站逻辑结构优化是指
  • wordpress 4.9.9中小企业网站优化
  • 购物网站创业时是如何做宣传的广州做韩国网站
  • 杭州公司网站WordPress手机不显示
  • 网站建设克隆wordpress 转移 问号
  • wordpress建站ftp常州网站外包
  • 汽车网站哪个好网站建设是学哪个学科
  • 惠州城乡住房建设厅网站wordpress本文地址
  • 商城网站模板框架wordpress打开前台页面空白
  • 小企业网站建设新市场报价.net wap网站
  • asp.net网站建设项目实战资料小型购物网站建设
  • 建设网站需要租赁主机吗网站开发工作进展情况
  • python和php做网站网站建设属于销售费用
  • 马鞍山住房和城乡建设局网站成都的科技公司有哪些
  • 网站代码 公告栏 php如何编辑网站标题栏
  • 做国际网站有补贴吗只选设计师的网站
  • 网站描述怎么修改flash制作网页
  • 江西网站icp备案注销中山网站建设文化流程
  • 上海网站设计优刻天琥网页设计培训
  • 建设银行网站招聘官网wordpress咋用
  • 网站服务器租用有什么好photoshop网页版
  • 泰安吧重庆优化官网服务
  • 网站设计学习网seo外链在线提交工具