北京网站开发品牌,网站模板免费下载中文版,东莞市手机网站建设品牌,网站建设和网络推广方案一、何为逻辑回归
逻辑回归可以简单理解为是基于多元线性回归的一种缩放。 多元线性回归y的取值范围在(-∞#xff0c;∞)#xff0c;数据集中的x是准确的一个数值。 用这样的一个数据集代入线性回归算法当中会得到一个模型。 这个模型所具备的功能就是当有人给这个模型一个…一、何为逻辑回归
逻辑回归可以简单理解为是基于多元线性回归的一种缩放。 多元线性回归y的取值范围在(-∞∞)数据集中的x是准确的一个数值。 用这样的一个数据集代入线性回归算法当中会得到一个模型。 这个模型所具备的功能就是当有人给这个模型一个新的数据x的时候模型就会给出一个预测结果y这个预测结果也是在(-∞∞)因为训练集中的取值范围也是在(-∞∞)之间故预测的结果也在(-∞∞)之间。 多元线性回归yw0 w1x1 w2x2 … wn*xn 逻辑回归就是在多元线性回归y这个结果上将y的(-∞∞)取值范围进行缩放其中t就是多元线性回归中的y。 逻辑回归包括两部分 ①多元线性回归 ②将得出的y代入sigmoid函数中 ③最后得出的结果在0-1之间即可
逻辑回归是用用来进行分类的默认是通过0.5进行二分类。[00.5][0.51]进行二分类。
二、多元线性回归和逻辑回归的区别
多元线性回归主要解决的是回归问题y的取值范围为(-∞,∞)
因为是有监督的机器学习故将已知的数据集的x和y分别代入上述公式即可求出相应的w0,w1,…,wn即可得到最终的模型
逻辑回归主要解决的是分类问题这里以二分类为例因为是分类问题故y的取值范围为[0,1] 因为是有监督的机器学习故将已知的数据集的x和y分别代入上述公式即可求出相应的w0,w1,…,wn即可得到最终的模型
三、逻辑回归相关概念
多元线性回归Ridge、Lasso、ElasticNet是做拟合进行回归预测的 逻辑回归Logistic Regression是做分类任务的
Ⅰ做回归预测损失函数是什么
答平方均值损失函数MSE
Ⅱ做分类损失函数是什么
答做分类损失函数是交叉熵
Ⅲ什么是熵
答熵是一种测量分子不稳定性的指标分子运动越不稳定熵就越大来自热力学 熵是一种测量信息量的单位信息熵包含的信息越多熵就越大来自信息论香农所提出来的 熵是一种测量不确定性的单位不确定性越大概率越小熵就越大
Ⅳ熵和概率是什么一个关系
答随着概率的减小熵会增大
Ⅴ什么是交叉熵
答交叉熵来自于香农提出的信息论。交叉熵可以在神经网络(机器学习)中作为损失函数p表示真实标记的分布q则表示为训练后的模型的预测标记分布交叉熵损失函数可以衡量p和q的相似性。 公式为-(logq * p)例如实际为1预测为0.8则代入公式可得其损失函数(交叉熵)为 - [(log0.8) * 1]
Ⅵ逻辑回归推导
因为逻辑回归是个二分类问题通过概率p^来确定是0还是1 代入损失函数(交叉熵)中这是单个的损失函数若样本有m个则需要进行求和 将损失函数进行整合一个得出最终的损失函数 因为后续的操作都是基于SGD随机梯度下降故此处求了一下偏导为后续做简便运算
Ⅶ为什么逻辑回归的本质是多元线性回归
答1公式首先应用了多元线性回归的公式其次才是把多元线性回归的结果交给sigmoid函数去进行缩放 2导函数逻辑回归的损失函数推导的导函数整个形式上和多元线性回归基本一致只是y_hat(y^)求解公式包含了一个sigmoid过程而已
Ⅷ逻辑回归的损失函数是什么
答交叉熵做分类就用交叉熵-y * logP因为逻辑回归是二分类所以损失函数loss func (-y*logP -(1-y)*log(1-P))也就是说我们期望这个损失最小然后找到最优解事实上我们就可以利用前面学过的梯度下降法来求解最优解
Ⅸ逻辑回归为什么阈值是0.5
答因为线性回归区间是负无穷到正无穷的所以区间可以按照0来分成两部分所以带到sigmoid公式里面去z0的话y就等于0.5 把z0代入公式中可得y0.5故逻辑回归的阈值为0.5
Ⅹ逻辑回归做多分类
答逻辑回归做多分类把多分类的问题转化成多个二分类的问题如果假如要分三个类别就需要同时训练三个互相不影响的模型比如我们n个维度那么三分类w参数的个数就会是 (n1)*3个参数 所谓的互不影响指的是模型在梯度下降的时候分别去训练分别去下降三个模型互相不需要传递数据也不需要等待收敛
Ⅺ文字本身是几维的数据音乐本身是几维的数据图片本身是几维的数据视频本身是几维的数据
答看什么类型的数据文字是一维的数据 音乐是单声道的音乐音乐是一维的数据如果音乐是双声道的就是二维的数据 图片如果看成是张图片就是个平面二维的数据 视频是一张张图片按时间顺序码放的那就是三维的数据 但我们做机器学习的时候真的只会这样考虑吗 文章是由不同的词组成的词的种类越多事实上考虑的维度就越多 图片如果是彩色的图片图片可以有R、G、B、alpha也可以有不同的频率有不同的滤波每个频率如果看成是一个维度那么就可以N多个维度 音乐可以有不同的频率每个频率如果看成是一个维度那么就可以N多个维度
四、案例实战
通过逻辑回归完成鸢尾花三分类问题 使用鸢尾花数据集通过逻辑回归完成多分类任务实际上就是多个二分类而已
完整代码
import numpy as np
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
import matplotlib.pyplot as plt
from time import timeiris datasets.load_iris()
print(list(iris.keys()))
print(iris[DESCR])
print(iris[feature_names])#特征名X iris[data][:, 3:]#取出x矩阵
print(X)#petal width(cm)print(iris[target])
y iris[target]
# y (iris[target] 2).astype(np.int)
print(y)#获取类别号# Utility function to report best scores
# def report(results, n_top3):
# for i in range(1, n_top 1):
# candidates np.flatnonzero(results[rank_test_score] i)
# for candidate in candidates:
# print(Model with rank: {0}.format(i))
# print(Mean validation score: {0:.3f} (std: {1:.3f}).format(
# results[mean_test_score][candidate],
# results[std_test_score][candidate]))
# print(Parameters: {0}.format(results[params][candidate]))
# print()
#
#
# start time()
# param_grid {tol: [1e-4, 1e-3, 1e-2],
# C: [0.4, 0.6, 0.8]}
log_reg LogisticRegression(multi_classovr, solversag)#多个二分类来解决多分类为ovr若为multinomial则使用softmax求解多分类问题梯度下降法sag
# grid_search GridSearchCV(log_reg, param_gridparam_grid, cv3)
log_reg.fit(X, y)
# print(GridSearchCV took %.2f seconds for %d candidate parameter settings.
# % (time() - start, len(grid_search.cv_results_[params])))
# report(grid_search.cv_results_)X_new np.linspace(0, 3, 1000).reshape(-1, 1)#创建新的数据集从0-3这个区间范围内取1000个数值linspace为平均分成1000个段取出1000个点
print(X_new)y_proba log_reg.predict_proba(X_new)#预测分类号具体分类成哪一个类别的概率值
y_hat log_reg.predict(X_new)#预测分类号具体分类成哪一个类别跟0.5去比较从而划分为0或者1
print(y_proba)
print(y_hat)
print(w1,log_reg.coef_)
print(w0,log_reg.intercept_)plt.plot(X_new, y_proba[:, 2], g-, labelIris-Virginica)
plt.plot(X_new, y_proba[:, 1], r-, labelIris-Versicolour)
plt.plot(X_new, y_proba[:, 0], b--, labelIris-Setosa)
plt.show()print(log_reg.predict([[1.7], [1.5]]))[data, target, frame, target_names, DESCR, feature_names, filename, data_module]
.. _iris_dataset:Iris plants dataset
--------------------**Data Set Characteristics:**:Number of Instances: 150 (50 in each of three classes):Number of Attributes: 4 numeric, predictive attributes and the class:Attribute Information:- sepal length in cm- sepal width in cm- petal length in cm- petal width in cm- class:- Iris-Setosa- Iris-Versicolour- Iris-Virginica:Summary Statistics: Min Max Mean SD Class Correlation sepal length: 4.3 7.9 5.84 0.83 0.7826sepal width: 2.0 4.4 3.05 0.43 -0.4194petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)petal width: 0.1 2.5 1.20 0.76 0.9565 (high!) :Missing Attribute Values: None:Class Distribution: 33.3% for each of 3 classes.:Creator: R.A. Fisher:Donor: Michael Marshall (MARSHALL%PLUio.arc.nasa.gov):Date: July, 1988The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken
from Fishers paper. Note that its the same as in R, but not as in the UCI
Machine Learning Repository, which has two wrong data points.This is perhaps the best known database to be found in the
pattern recognition literature. Fishers paper is a classic in the field and
is referenced frequently to this day. (See Duda Hart, for example.) The
data set contains 3 classes of 50 instances each, where each class refers to a
type of iris plant. One class is linearly separable from the other 2; the
latter are NOT linearly separable from each other... topic:: References- Fisher, R.A. The use of multiple measurements in taxonomic problemsAnnual Eugenics, 7, Part II, 179-188 (1936); also in Contributions toMathematical Statistics (John Wiley, NY, 1950).- Duda, R.O., Hart, P.E. (1973) Pattern Classification and Scene Analysis.(Q327.D83) John Wiley Sons. ISBN 0-471-22361-1. See page 218.- Dasarathy, B.V. (1980) Nosing Around the Neighborhood: A New SystemStructure and Classification Rule for Recognition in Partially ExposedEnvironments. IEEE Transactions on Pattern Analysis and MachineIntelligence, Vol. PAMI-2, No. 1, 67-71.- Gates, G.W. (1972) The Reduced Nearest Neighbor Rule. IEEE Transactionson Information Theory, May 1972, 431-433.- See also: 1988 MLC Proceedings, 54-64. Cheeseman et als AUTOCLASS IIconceptual clustering system finds 3 classes in the data.- Many, many more ...
[sepal length (cm), sepal width (cm), petal length (cm), petal width (cm)]
[[0.2][0.2][0.2][0.2][0.2][0.4]
...[2.5][2.3][1.9][2. ][2.3][1.8]]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 22 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 22 2]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 22 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 22 2]
[[0. ][0.003003 ][0.00600601][0.00900901][0.01201201][0.01501502][0.01801802][0.02102102][0.02402402][0.02702703][0.03003003][0.03303303][0.03603604][0.03903904][0.04204204][0.04504505][0.04804805][0.05105105][0.05405405][0.05705706][0.06006006][0.06306306][0.06606607]...[3. ]]
[[7.92375143e-01 2.07016620e-01 6.08236533e-04][7.92180828e-01 2.07202857e-01 6.16315878e-04][7.91985384e-01 2.07390111e-01 6.24505129e-04]...[2.67645128e-05 3.18752286e-01 6.81220950e-01][2.63977911e-05 3.18853898e-01 6.81119704e-01][2.60361037e-05 3.18955589e-01 6.81018375e-01]]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 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2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 22]
[2 1]五、逻辑回归二分类和多分类本质区别
逻辑回归二分类
逻辑回归多分类 与二分类不同的地方在于对数据集的处理