导航:首页 > 编程语言 > pythonnumpyrandn

pythonnumpyrandn

发布时间:2022-07-01 15:04:14

python怎样生成正太分布的随机数

一般的正态分布可以通过标准正态分布配合数学期望向量和协方差矩阵得到。

格式为:numpy.random.randn(d0,d1,...,dn)

Python使用Mersenne Twister为核心的生成器,它会生成53 bit精度的浮点值,周期为2的19937次方减1,底层的C语言实现是快速和线程安全的.

⑵ python中向量指的是什么意思


一、向量是什么

在数学中,向量(也称为欧几里得向量、几何向量、矢量),指具有大小(magnitude)和方向的量。它可以形象化地表示为带箭头的线段。箭头所指:代表向量的方向;线段长度:代表向量的大小。与向量对应的只有大小,没有方向的量叫做数量(物理学中称标量)

在这里,向量即一维数组,用 arange 函数创建向量是最简单的方式之一:

arange函数也可以指定初始值、终止值和步长来创建一维数组:

向量还能直接对每个元素进行运算:

二、创建向量

上面使用 arange 则是创建向量的一种方式,其实只要是数组创建的函数均可以创建向量,如:

linspace() 函数

前文介绍:linspace 通过制定初始值、终止值和元素个数创建等差数列向量,通过endpoint 参数指定是否包含终止值,默认为True

logspace() 函数

同linspace,创建等比数列,基数通过base参数指定,默认基数为10

zeros() 函数和 ones() 函数

这两个函数分别可以创建指定长度或形状的全0或全1的 ndarray 数组,比如:

指定数据类型:

empty() 函数

这个函数可以创建一个没有任何具体值的 ndarray 数组,例如:

random.randn() 函数

randn 是 numpy.random 中生成正态分布随机数据的函数

fromstring() 函数

从字符串创建数组

上面从字符串创建的数组,定义为整形8bit,创建出来的其实就是字符串的ASCII 码

fromfunction() 函数

从函数创建数组,是数据分析常见的方法

可先定义一个从下标计算数值的函数,然后用fromfunction 创建数组

fromfunction 第一个参数为计算每个数组元素的函数名,第二个参数指定数组的形状。因为它支持多维数组,所以第二个参数必须是一个序列。

例如我创建一个九九乘法表:

注意,fromfunction 函数中的第二个参数指定的是数组的下标,下标作为实参通过遍历的方式传递给函数的形参。

众多python培训视频,尽在python学习网,欢迎在线学习!

⑶ 如何用python numpy产生一个正态分布随机数的向量或者矩阵

importnumpyasnp
x=np.random.randn(4,5)#生成一个4*5的服从正态分布(0,1)的数组
print(x)

结果:

array([[1.49880806,0.49802583,-0.73570234,0.6838595,-1.07146133],
[-0.80834618,0.28833047,0.6492072,-1.23454671,-0.42839883],
[0.75936243,-0.67680322,1.06767814,-0.11232622,-0.62300974],
[-1.66010364,-0.60023795,0.35930247,-0.5079359,0.21811627]])

⑷ 如何用python numpy产生一个正太分布随机数的向量或者矩阵

一般的正态分布可以通过标准正态分布配合数学期望向量和协方差矩阵得到。如下代码,可以得到满足一维和二维正态分布的样本。希望有用,如有错误,欢迎指正!

# coding=utf-8

import numpy as np
from numpy.linalg import cholesky
import matplotlib.pyplot as plt

sampleNo = 1000;
# 一维正态分布
# 下面三种方式是等效的
mu = 3
sigma = 0.1
np.random.seed(0)
s = np.random.normal(mu, sigma, sampleNo )
plt.subplot(141)
plt.hist(s, 30, normed=True)

np.random.seed(0)
s = sigma * np.random.randn(sampleNo ) + mu
plt.subplot(142)
plt.hist(s, 30, normed=True)

np.random.seed(0)
s = sigma * np.random.standard_normal(sampleNo ) + mu
plt.subplot(143)
plt.hist(s, 30, normed=True)

# 二维正态分布
mu = np.array([[1, 5]])
Sigma = np.array([[1, 0.5], [1.5, 3]])
R = cholesky(Sigma)
s = np.dot(np.random.randn(sampleNo, 2), R) + mu
plt.subplot(144)
# 注意绘制的是散点图,而不是直方图
plt.plot(s[:,0],s[:,1],'+')
plt.show()

⑸ PYTHON中randn函数的具体含义

比较2个对象,前者小于后者返回-1,相等则返回0,大于后者返回1。

⑹ py2exe python24 NumPy错误意思是问题,怎么解决

from distutils.core import setup
import py2exe

from distutils.filelist import findall
import os
import matplotlib
matplotlibdatadir = matplotlib.get_data_path()
matplotlibdata = findall(matplotlibdatadir)
matplotlibdata_files = []
for f in matplotlibdata:
dirname = os.path.join('matplotlibdata', f[len(matplotlibdatadir)+1:])
matplotlibdata_files.append((os.path.split(dirname)[0], [f]))

packages = ['matplotlib', 'pytz']
includes = []
excludes = []
dll_excludes = ['libgdk_pixbuf-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk-win32-2.0-0.dll',
'wxmsw26uh_vc.dll']

opts = { 'py2exe': { 'packages' : packages,
'includes' : includes,
'excludes' : excludes,
'dll_excludes' : dll_excludes
}
}

setup ( console=['test.py'],
options = opts,
data_files = matplotlibdata_files
)

I compile the application by running >setup.py py2exe
At the end of compilation phase, it is written :
The following moles appear to be missing

['AppKit', 'FFT', 'Foundation', 'Image', 'LinearAlgebra', 'MA', 'MLab', 'Matrix', 'Numeric', 'PyObjCTools', 'P
yQt4', 'Pyrex', 'Pyrex.Compiler', 'RandomArray', '_curses', '_ssl', 'backends.draw_if_interactive', 'backends.
new_figure_manager', 'backends.pylab_setup', 'backends.show', 'cairo', 'cairo.gtk', 'fcompiler.FCompiler', 'fc
ompiler.show_fcompilers', 'fltk', 'gd', 'gobject', 'gtk', 'lib.add_newdoc', 'matplotlib.enthought.pyface.actio
n', 'mlab.amax', 'mlab.amin', 'numarray', 'numarray.convolve', 'numarray.fft', 'numarray.ieeespecial', 'numarr
ay.linear_algebra', 'numarray.linear_algebra.mlab', 'numarray.ma', 'numarray.numeric', 'numarray.random_array'
, 'numerix.ArrayType', 'numerix.Complex', 'numerix.Complex32', 'numerix.Complex64', 'numerix.Float', 'numerix.
Float32', 'numerix.Float64', 'numerix.Int', 'numerix.Int16', 'numerix.Int32', 'numerix.Int8', 'numerix.NewAxis
', 'numerix.UInt16', 'numerix.UInt32', 'numerix.UInt8', 'numerix.absolute', 'numerix.add', 'numerix.all', 'num
erix.allclose', 'numerix.alltrue', 'numerix.arange', 'numerix.arccos', 'numerix.arccosh', 'numerix.arcsin', 'n
umerix.arcsinh', 'numerix.arctan', 'numerix.arctan2', 'numerix.arctanh', 'numerix.argmax', 'numerix.argmin', '
numerix.argsort', 'numerix.around', 'numerix.array', 'numerix.arrayrange', 'numerix.asarray', 'numerix.asum',
'numerix.bitwise_and', 'numerix.bitwise_or', 'numerix.bitwise_xor', 'numerix.ceil', 'numerix.choose', 'numerix
.clip', 'numerix.compress', 'numerix.concatenate', 'numerix.conjugate', 'numerix.convolve', 'numerix.cos', 'nu
merix.cosh', 'numerix.cross_correlate', 'numerix.cumproct', 'numerix.cumsum', 'numerix.diagonal', 'numerix.d
ivide', 'numerix.dot', 'numerix.equal', 'numerix.exp', 'numerix.fabs', 'numerix.fft.fft', 'numerix.fft.inverse
_fft', 'numerix.floor', 'numerix.fmod', 'numerix.fromfunction', 'numerix.fromstring', 'numerix.greater', 'nume
rix.greater_equal', 'numerix.hypot', 'numerix.identity', 'numerix.indices', 'numerix.innerproct', 'numerix.i
scontiguous', 'numerix.less', 'numerix.less_equal', 'numerix.log', 'numerix.log10', 'numerix.logical_and', 'nu
merix.logical_not', 'numerix.logical_or', 'numerix.logical_xor', 'numerix.matrixmultiply', 'numerix.maximum',
'numerix.minimum', 'numerix.mlab.amax', 'numerix.mlab.amin', 'numerix.mlab.cov', 'numerix.mlab.diff', 'numerix
.mlab.hanning', 'numerix.mlab.rand', 'numerix.mlab.std', 'numerix.mlab.svd', 'numerix.multiply', 'numerix.nega
tive', 'numerix.newaxis', 'numerix.nonzero', 'numerix.not_equal', 'numerix.nx', 'numerix.ones', 'numerix.outer
proct', 'numerix.pi', 'numerix.power', 'numerix.proct', 'numerix.put', 'numerix.putmask', 'numerix.rank',
'numerix.ravel', 'numerix.repeat', 'numerix.reshape', 'numerix.resize', 'numerix.searchsorted', 'numerix.shape
', 'numerix.sin', 'numerix.sinh', 'numerix.size', 'numerix.sometrue', 'numerix.sort', 'numerix.sqrt', 'numerix
.subtract', 'numerix.swapaxes', 'numerix.take', 'numerix.tan', 'numerix.tanh', 'numerix.trace', 'numerix.trans
pose', 'numerix.typecode', 'numerix.typecodes', 'numerix.where', 'numerix.which', 'numerix.zeros', 'numpy.Comp
lex', 'numpy.Complex32', 'numpy.Complex64', 'numpy.Float', 'numpy.Float32', 'numpy.Float64', 'numpy.Infinity',
'numpy.Int', 'numpy.Int16', 'numpy.Int32', 'numpy.Int8', 'numpy.UInt16', 'numpy.UInt32', 'numpy.UInt8', 'nump
y.inf', 'numpy.infty', 'numpy.oldnumeric', 'objc', 'paint', 'pango', 'pre', 'pyemf', 'qt', 'setuptools', 'setu
ptools.command', 'setuptools.command.egg_info', 'trait_sheet', 'matplotlib.numerix.Float', 'matplotlib.numerix
.Float32', 'matplotlib.numerix.absolute', 'matplotlib.numerix.alltrue', 'matplotlib.numerix.asarray', 'matplot
lib.numerix.ceil', 'matplotlib.numerix.equal', 'matplotlib.numerix.fromstring', 'matplotlib.numerix.indices',
'matplotlib.numerix.put', 'matplotlib.numerix.ravel', 'matplotlib.numerix.sqrt', 'matplotlib.numerix.take', 'm
atplotlib.numerix.transpose', 'matplotlib.numerix.where', 'numpy.core.conjugate', 'numpy.core.equal', 'numpy.c
ore.less', 'numpy.core.less_equal', 'numpy.dft.old', 'numpy.random.rand', 'numpy.random.randn']

1) First Problem: numpy\core\_internal.pyc not included in Library.zip
No scipy-style subpackage 'core' found in C:\WinCE\Traces\py2exe test\dist\library.zip\numpy. Ignoring: No mole named _internal
Traceback (most recent call last):
File "profiler_ftt.py", line 15, in ?
from matplotlib.backends.backend_wx import Toolbar, FigureCanvasWx,\
File "matplotlib\backends\backend_wx.pyc", line 152, in ?
File "matplotlib\backend_bases.pyc", line 10, in ?
File "matplotlib\colors.pyc", line 33, in ?
File "matplotlib\numerix\__init__.pyc", line 67, in ?
File "numpy\__init__.pyc", line 35, in ?
File "numpy\_import_tools.pyc", line 173, in __call__
File "numpy\_import_tools.pyc", line 68, in _init_info_moles
File "<string>", line 1, in ?
File "numpy\lib\__init__.pyc", line 5, in ?
File "numpy\lib\type_check.pyc", line 8, in ?
File "numpy\core\__init__.pyc", line 6, in ?
File "numpy\core\umath.pyc", line 12, in ?
File "numpy\core\umath.pyc", line 10, in __load
AttributeError: 'mole' object has no attribute '_ARRAY_API'

I resolved that issue by adding the file ...\Python24\Lib\site-packages\numpy\core\_internal.pyc in ...\test\dist\library.zip\numpy\core.
Each time I compile that executable, I add the file by hand.
Does anybody know how to automatically add that file?
2) Second problem: I don't know how to resolve that issue:
Traceback (most recent call last):
File "profiler_ftt.py", line 15, in ?
from matplotlib.backends.backend_wx import Toolbar, FigureCanvasWx,\
File "matplotlib\backends\backend_wx.pyc", line 152, in ?
File "matplotlib\backend_bases.pyc", line 10, in ?
File "matplotlib\colors.pyc", line 33, in ?
File "matplotlib\numerix\__init__.pyc", line 67, in ?
File "numpy\__init__.pyc", line 35, in ?
File "numpy\_import_tools.pyc", line 173, in __call__
File "numpy\_import_tools.pyc", line 68, in _init_info_moles
File "<string>", line 1, in ?
File "numpy\random\__init__.pyc", line 3, in ?
File "numpy\random\mtrand.pyc", line 12, in ?
File "numpy\random\mtrand.pyc", line 10, in __load
File "numpy.pxi", line 32, in mtrand
AttributeError: 'mole' object has no attribute 'dtype'

I don't find the file numpy.pxi in my file tree nor in \test\dist\library.zip.
I browsed the web in the hope to find a solution but nothing.
It seems that this issue is well known but no solution provided in mailing lists.

⑺ python里x=randn mat=x.T.dot 是求什么

x=randn这个写法是不对的。

randn是numpy里的一个生成随机array的函数。

比如说要生成一个三行两列的随机array,可以这样写:


import numpy

x = numpy.random.randn(3,2)

像这样:

dot(2)是点乘常数就不说了,

那个x.T.dot([1,2,3])就是x.T的

1*1+2*2+3*3=14

2*1+3*2+4*3=20


懂了木有 =。=

⑻ python关于numpy基础问题

Python发展至今,已经有越来越多的人使用python进行科学技术,NumPY是python中的一款高性能科学计算和数据分析的基础包。
ndarray
ndarray(以下简称数组)是numpy的数组对象,需要注意的是,它是同构的,也就是说其中的所有元素必须是相同的类型。其中每个数组都有一个shape和dtype。
shape既是数组的形状,比如
复制代码
1 import numpy as np
2 from numpy.random import randn
3
4 arr = randn(12).reshape(3, 4)
5
6 arr
7
8 [[ 0.98655235 1.20830283 -0.72135183 0.40292924]
9 [-0.05059849 -0.02714873 -0.62775486 0.83222997]
10 [-0.84826071 -0.29484606 -0.76984902 0.09025059]]
11
12 arr.shape
13 (3, 4)
复制代码
其中(3, 4)即代表arr是3行4列的数组,其中dtype为float64
一下函数可以用来创建数组
array将输入数据转换为ndarray,类型可制定也可默认
asarray将输入转换为ndarray
arange类似内置range
ones、ones_like根据形状创建一个全1的数组、后者可以复制其他数组的形状
zeros、zeros_like类似上面,全0
empty、empty_like创建新数组、只分配空间
eye、identity创建对角线为1的对角矩阵
数组的转置和轴对称
转置是多维数组的基本运算之一。可以使用.T属性或者transpose()来实现。.T就是进行轴对换而transpose则可以接收参数进行更丰富的变换
复制代码
arr = np.arange(6).reshape((2,3))
print arr
[[0 1 2]
[3 4 5]]
print arr.T
[[0 3]
[1 4]
[2 5]]
arr = np.arange(24).reshape((2,3,4))
print arr
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
print arr.transpose((0,1,2))
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
复制代码
数组的运算
大小相等的数组之间做任何算术运算都会将运算应用到元素级别。
复制代码
1 arr = np.arange(9).reshape(3, 3)
2 print arr
3
4 [[0 1 2]
5 [3 4 5]
6 [6 7 8]]
7
8 print arr*arr
9
10 [[ 0 1 4]
11 [ 9 16 25]
12 [36 49 64]]
13
14 print arr+arr
15
16 [[ 0 2 4]
17 [ 6 8 10]
18 [12 14 16]]
19
20 print arr*4
21
22 [[ 0 4 8]
23 [12 16 20]
24 [24 28 32]]
复制代码
numpy的简单计算中,ufunc通用函数是对数组中的数据执行元素级运算的函数。
如:
复制代码
arr = np.arange(6).reshape((2,3))
print arr
[[0 1 2]
[3 4 5]]
print np.square(arr)
[[ 0 1 4]
[ 9 16 25]]
复制代码
类似的有:abs,fabs,sqrt,square,exp,log,sign,ceil,floor,rint,modf,isnan,isfinite,isinf,cos,cosh,sin,sinh,tan,tanh,
add,subtract,multiply,power,mod,equal,等等

阅读全文

与pythonnumpyrandn相关的资料

热点内容
云服务器建设原理 浏览:256
javajunit4for 浏览:843
华为服务器如何进阵列卡配置 浏览:433
apache服务器ip地址访问 浏览:718
如何买到安卓手机预装软件 浏览:537
冤罪百度云不要压缩 浏览:86
苏州云存储服务器 浏览:173
解压收纳原声 浏览:384
java注册验证 浏览:375
火花app怎么上推荐 浏览:980
什么app能游戏投屏到电视上 浏览:455
服务器托管到云端是什么意思 浏览:835
app保存草稿怎么用 浏览:808
安卓如何进入proumb 浏览:144
主机虚拟云服务器 浏览:619
删除分区加密的空间会不会恢复 浏览:706
京东app客户上门怎么看搜索量 浏览:741
怎么在农行app购买黄金 浏览:46
c型开发板和单片机 浏览:146
虚拟机建立用户的模板文件夹 浏览:905