導航:首頁 > 編程語言 > 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相關的資料

熱點內容
全球票房實時查詢 瀏覽:221
伺服器如何添加域用戶 瀏覽:271
java靜態static 瀏覽:227
程序員容易掉頭發嗎 瀏覽:333
python通用管理系統 瀏覽:204
apachephphtml 瀏覽:141
安慶智能雲伺服器找哪家 瀏覽:763
linuxtab輸入 瀏覽:932
小說網盤資源 瀏覽:504
全免費影視投屏網站 瀏覽:254
娘娘懷孕快生了忍著不生 瀏覽:804
git拉取代碼的命令 瀏覽:995
程序員節西安市 瀏覽:687
單片機的閃燈 瀏覽:969
phpmime映射 瀏覽:583
關鍵特徵分析python 瀏覽:994
linux粘滯位 瀏覽:137
安卓如何把備忘錄調成黑色 瀏覽:864
dhcp伺服器手動分配ip地址 瀏覽:308
阿里雲國內伺服器數量 瀏覽:455