GVKun编程网logo

机器学习分享——逻辑回归推导以及 numpy 的实现(逻辑回归 csdn)

4

对于机器学习分享——逻辑回归推导以及numpy的实现感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解逻辑回归csdn,并且为您提供关于"importnumpyasnp"ImportError

对于机器学习分享——逻辑回归推导以及 numpy 的实现感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解逻辑回归 csdn,并且为您提供关于"import numpy as np" ImportError: No module named numpy、3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数、Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案、Difference between import numpy and import numpy as np的宝贵知识。

本文目录一览:

机器学习分享——逻辑回归推导以及 numpy 的实现(逻辑回归 csdn)

机器学习分享——逻辑回归推导以及 numpy 的实现(逻辑回归 csdn)

逻辑回归基本概念

什么是逻辑回归?
逻辑回归就是这样的一个过程:面对一个回归或者分类问题,建立代价函数,然后通过优化方法迭代求解出最优的模型参数,然后测试验证我们这个求解的模型的好坏。

Logistic 回归虽然名字里带“回归”,但是它实际上是一种分类方法,主要用于两分类问题(即输出只有两种,分别代表两个类别)

回归模型中,y是一个定性变量,比如y=0或1,logistic方法主要应用于研究某些事件发生的概率

概念解释

Logistic Regression推导过程

它的表达式是:

$$ f(x) = \frac{1}{1 + e^{-\theta}} $$

$$ \theta = WX + B $$

可以发现,经过sigmoid函数转换后, 输出值是在[0, 1]之间,可以认为输出是概率,下面就来详细的推导:

推导

为了计算方便, 我们只讨论二分类.

首先, 逻辑回归进行了一个假设,两个类别都服从均值不同,方差相同(方便推导)的高斯分布

$$ p(y|x=0) = \mu(\mu_0, \sigma) $$

$$ p(y|x=1) = \mu(\mu_1, \sigma) $$

高斯分布是比较容易处理的分布,根据中心极限定理也知道,最终会收敛于高斯分布。
从信息论的角度上看,当均值和方差已知时(尽管你并不知道确切的均值和方差,但是根据概率论,当样本量足够大时,样本均值和方差以概率1趋向于均值和方差),高斯分布是熵最大的分布,为什么要熵最大?因为最大熵的分布可以平摊你的风险(同一个值会有两个点可以取到, 不确定性很大),这就好比不要把鸡蛋放到同一个篮子里,想想二分查找中,为什么每次都是选取中间点作为查找点?就是为了平摊风险(假设方差相等只是为了计算方便)。

风险

$$ Risk(y=0|x) = \lambda_{00}P(y=0|x) + \lambda_{01}P(y = 1|x) $$

$$ Risk(y=1|x) = \lambda_{10}P(y=0|x) + \lambda_{11}P(y = 1|x) $$

其中,$Risk(y=0|x)$是把样本预测为0时的风险,$Risk(y=1|x)$是把样本预测为1时的风险,
$λ_{ij}$是样本实际标签为j时,却把它预测为i是所带来的风险。

我们认为预测正确并不会带来风险,因此$λ_{00}$和$λ_{11}$都为0,此外,我们认为当标签为0而预测为1 和 当标签为1而预测为0,这两者所带来的风险是相等的,因此$λ_{10}$和$λ_{01}$相等,方便起见,我们记为λ。但在一些领域里,比如医学、风控等,这些λ在大多数情况下是不相等的,有时候我们会选择“宁可错杀一一千也不能放过一个”;

那么我们简化后的表达式:

$$ Risk(y=0|x) = \lambda P(y = 1|x) $$

$$ Risk(y=1|x) = \lambda P(y=0|x) $$

根据最小化风险的原则,我们通常会选择风险较小的。

比如:

$$ Risk(y=0|x) < Risk(y=1|x) $$

这就说明了预测为第0类的风险小于预测为第1类的风险。

可以得到:

$$ \frac{Risk(y=0|x)}{Risk(y=1|x)} < 1 $$

$$ \frac{P(y = 1|x)}{P(y=0|x)} < 1 $$

就是说明预测第1类的概率小于第0类的概率。

我们对不等式两边分别取对数

$$ log\frac{{P(y = 1|x)}}{{P(y=0|x)}} < 0 $$

根据贝叶斯公式:

$$ log\frac{P(x|y = 1)p(y=1)}{P(x|y=0)p(y=0)} < 0 $$

$$ log\frac{P(x|y = 1)}{P(x|y=0)} + log\frac{p(y=1)}{p(y=0)} < 0 $$

我们开始假设过,两个类别分别服从均值不等,方差相等的高斯分布,根据高斯分布的公式有:

高斯分布

$$ g(x) = \frac{1}{2\pi\sigma}e^{-\frac{(x - \mu)^2}{2\sigma^2}} $$

忽略常数项(方差也是相等的)

$$ log\frac{P(x|y = 1)}{P(x|y=0)} + loge^{(\frac{(x - \mu_0)^2}{2\sigma^2} - \frac{(x - \mu_1)^2}{2\sigma^2})} $$

$$ log\frac{P(x|y = 1)}{P(x|y=0)} + (\frac{(x - \mu_0)^2}{2\sigma^2} - \frac{(x - \mu_1)^2}{2\sigma^2}) < 0 $$

$$ log\frac{P(x|y = 1)}{P(x|y=0)} < \frac{(x - \mu_1)^2}{2\sigma^2} - \frac{(x - \mu_0)^2}{2\sigma^2} $$

$$ log\frac{P(x|y = 1)}{P(x|y=0)} < \frac{\mu_0}{\sigma^2}x - \frac{\mu_1}{\sigma^2}x + C $$

C是常熟,可以使用矩阵的表示。

$$ log\frac{P(x|y = 1)}{P(x|y=0)} < \theta{X} $$

详细推导

对值取幂,以及等式取等号计算。

$$ \frac{P(y=1|x)}{P(y=0|x)} = e^{\theta x} $$

$$ = \frac{P(y=1|x)}{1 - P(y=1|x)} = e^{\theta x} $$

$$ = \frac{1 - P(y=1|x)}{P(y=1|x)} = e^{-\theta x} $$

$$ = \frac{1}{P(y=1|x)} - 1 = e^{-\theta x} $$

$$ = \frac{1}{P(y=1|x)} = e^{-\theta x} + 1 $$

$$ = P(y=1|x) = \frac{1}{e^{-\theta x} + 1} $$

以下是实现的一些截图

优化我们采用梯度下降算法

交叉熵损失函数

最终效果


电脑端查看完整代码

——————————————————————————————————
Mo (网址:momodel.cn )是一个支持 Python 的人工智能建模平台,能帮助你快速开发训练并部署 AI 应用。期待你的加入。

"import numpy as np" ImportError: No module named numpy

问题:没有安装 numpy

解决方法:

下载文件,安装

numpy-1.8.2-win32-superpack-python2.7

安装运行 import numpy,出现

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    import numpy
  File "C:\Python27\lib\site-packages\numpy\__init__.py", line 153, in <module>
    from . import add_newdocs
  File "C:\Python27\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "C:\Python27\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
    from .type_check import *
  File "C:\Python27\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "C:\Python27\lib\site-packages\numpy\core\__init__.py", line 6, in <module>
    from . import multiarray
ImportError: DLL load failed: %1 不是有效的 Win32 应用程序。

原因是:python 装的是 64 位的,numpy 装的是 32 位的

重新安装 numpy 为:numpy-1.8.0-win64-py2.7

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数

3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数

目录

[TOC]

前言

具体我们来学 Numpy 的统计函数

(一)函数一览表

调用方式:np.*

.sum(a) 对数组 a 求和
.mean(a) 求数学期望
.average(a) 求平均值
.std(a) 求标准差
.var(a) 求方差
.ptp(a) 求极差
.median(a) 求中值,即中位数
.min(a) 求最大值
.max(a) 求最小值
.argmin(a) 求最小值的下标,都处里为一维的下标
.argmax(a) 求最大值的下标,都处里为一维的下标
.unravel_index(index, shape) g 根据 shape, 由一维的下标生成多维的下标

(二)统计函数 1

(1)说明

(2)输出

.sum(a)

.mean(a)

.average(a)

.std(a)

.var(a)

(三)统计函数 2

(1)说明

(2)输出

.max(a) .min(a)

.ptp(a)

.median(a)

.argmin(a)

.argmax(a)

.unravel_index(index,shape)

作者:Mark

日期:2019/02/11 周一

Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案

Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案

如何解决Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案?

希望有人能在这里提供帮助。我一直在绕圈子一段时间。我只是想设置一个 python 脚本,它将一些 json 数据从 REST API 加载到云数据库中。我在 Anaconda 上设置了一个虚拟环境(因为 GCP 库推荐这样做),安装了依赖项,现在我只是尝试导入库并向端点发送请求。 我使用 Conda(和 conda-forge)来设置环境并安装依赖项,所以希望一切都干净。我正在使用带有 Python 扩展的 VS 编辑器作为编辑器。 每当我尝试运行脚本时,我都会收到以下消息。我已经尝试了其他人在 Google/StackOverflow 上找到的所有解决方案,但没有一个有效。我通常使用 IDLE 或 Jupyter 进行脚本编写,没有任何问题,但我对 Anaconda、VS 或环境变量(似乎是相关的)没有太多经验。 在此先感谢您的帮助!

  \Traceback (most recent call last):
File "C:\Conda\envs\gcp\lib\site-packages\numpy\core\__init__.py",line 22,in <module>
from . import multiarray
File "C:\Conda\envs\gcp\lib\site-packages\numpy\core\multiarray.py",line 12,in <module>
from . import overrides
File "C:\Conda\envs\gcp\lib\site-packages\numpy\core\overrides.py",line 7,in <module>
from numpy.core._multiarray_umath import (
ImportError: DLL load Failed while importing _multiarray_umath: The specified module Could not be found.

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
File "c:\API\citi-bike.py",line 4,in <module>
import numpy as np
File "C:\Conda\envs\gcp\lib\site-packages\numpy\__init__.py",line 150,in <module>
from . import core
File "C:\Conda\envs\gcp\lib\site-packages\numpy\core\__init__.py",line 48,in <module>
raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions Failed. This error can happen for
many reasons,often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

* The Python version is: python3.9 from "C:\Conda\envs\gcp\python.exe"
* The NumPy version is: "1.21.1"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: DLL load Failed while importing _multiarray_umath: The specified module Could not be found.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Difference between import numpy and import numpy as np

Difference between import numpy and import numpy as np

Difference between import numpy and import numpy as np

up vote 18 down vote favorite

5

I understand that when possible one should use

import numpy as np

This helps keep away any conflict due to namespaces. But I have noticed that while the command below works

import numpy.f2py as myf2py

the following does not

import numpy as np
np.f2py #throws no module named f2py

Can someone please explain this?

python numpy

shareimprove this question

edited Mar 24 ''14 at 23:20

mu 無

24.7k104471

asked Mar 24 ''14 at 23:19

user1318806

3001311

 
1  

@roippi have you tried exit your python and enter it and just do import numpy then numpy.f2py ? It throws an error in my case too – aha Mar 24 ''14 at 23:24

1  

Importing a module doesn''t import sub-modules. You need to explicitly import the numpy.f2py module regardless of whether or not/how numpy itself has been imported. – alecb Mar 24 ''14 at 23:39

add a comment

4 Answers

active oldest votes

 

up vote 13 down vote

numpy is the top package name, and doing import numpy doesn''t import submodule numpy.f2py.

When you do import numpy it creats a link that points to numpy, but numpy is not further linked to f2py. The link is established when you do import numpy.f2py

In your above code:

import numpy as np # np is an alias pointing to numpy, but at this point numpy is not linked to numpy.f2py
import numpy.f2py as myf2py # this command makes numpy link to numpy.f2py. myf2py is another alias pointing to numpy.f2py as well

Here is the difference between import numpy.f2py and import numpy.f2py as myf2py:

  • import numpy.f2py
    • put numpy into local symbol table(pointing to numpy), and numpy is linked to numpy.f2py
    • both numpy and numpy.f2py are accessible
  • import numpy.f2py as myf2py
    • put my2py into local symbol table(pointing to numpy.f2py)
    • Its parent numpy is not added into local symbol table. Therefore you can not access numpy directly

shareimprove this answer

edited Mar 25 ''14 at 0:31

answered Mar 24 ''14 at 23:33

aha

1,2291718

 

add a comment

 

up vote 7 down vote

The import as syntax was introduced in PEP 221 and is well documented there.

When you import a module via

import numpy

the numpy package is bound to the local variable numpy. The import as syntax simply allows you to bind the import to the local variable name of your choice (usually to avoid name collisions, shorten verbose module names, or standardize access to modules with compatible APIs).

Thus,

import numpy as np

is equivalent to,

import numpy
np = numpy
del numpy

When trying to understand this mechanism, it''s worth remembering that import numpy actually means import numpy as numpy.

When importing a submodule, you must refer to the full parent module name, since the importing mechanics happen at a higher level than the local variable scope. i.e.

import numpy as np
import numpy.f2py   # OK
import np.f2py      # ImportError

I also take issue with your assertion that "where possible one should [import numpy as np]". This is done for historical reasons, mostly because people get tired very quickly of prefixing every operation with numpy. It has never prevented a name collision for me (laziness of programmers actually suggests there''s a higher probability of causing a collision with np)

Finally, to round out my exposé, here are 2 interesting uses of the import as mechanism that you should be aware of:

1. long subimports

import scipy.ndimage.interpolation as warp
warp.affine_transform(I, ...)

2. compatible APIs

try:
    import pyfftw.interfaces.numpy_fft as fft
except:
    import numpy.fft as fft
# call fft.ifft(If) with fftw or the numpy fallback under a common name

shareimprove this answer

answered Mar 25 ''14 at 0:59

hbristow

68345

 

add a comment

 

up vote 1 down vote

numpy.f2py is actually a submodule of numpy, and therefore has to be imported separately from numpy. As aha said before:

When you do import numpy it creats a link that points to numpy, but numpy is not further linked to f2py. The link is established when you do import numpy.f2py

when you call the statement import numpy as np, you are shortening the phrase "numpy" to "np" to make your code easier to read. It also helps to avoid namespace issues. (tkinter and ttk are a good example of what can happen when you do have that issue. The UIs look extremely different.)

shareimprove this answer

answered Mar 24 ''14 at 23:47

bspymaster

760923

 

add a comment

 

up vote 1 down vote

This is a language feature. f2py is a subpackage of the module numpy and must be loaded separately.

This feature allows:

  • you to load from numpy only the packages you need, speeding up execution.
  • the developers of f2py to have namespace separation from the developers of another subpackage.

Notice however that import numpy.f2py or its variant import numpy.f2py as myf2py are still loading the parent module numpy.

Said that, when you run

import numpy as np
np.f2py

You receive an AttributeError because f2py is not an attribute of numpy, because the __init__() of the package numpy did not declare in its scope anything about the subpackage f2py.

shareimprove this answer

answered Mar 24 ''14 at 23:57

gg349

7,67321739

 
    

when you do import numpy.f2py as myf2py, how do you access its parent numpy? it seems import numpy.f2py allows you to access its parent numpy, but import numpy.f2py as myf2py doesn''t – aha Mar 25 ''14 at 0:00

    

You don''t access it because you decided you didn''t want to use anything from numpy, and you only care of using the subpackage. It is similar to using from foo import bar: the name foo will not be accessible. See the comment after the first example of the docs, LINK – gg349 Mar 25 ''14 at 0:05

add a comment

关于机器学习分享——逻辑回归推导以及 numpy 的实现逻辑回归 csdn的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于"import numpy as np" ImportError: No module named numpy、3.7Python 数据处理篇之 Numpy 系列 (七)---Numpy 的统计函数、Anaconda Numpy 错误“Importing the Numpy C Extension Failed”是否有另一种解决方案、Difference between import numpy and import numpy as np等相关知识的信息别忘了在本站进行查找喔。

本文标签: