Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数numpy.REFORMATE_Python_Numpy_Reshape - Fatal编程技术网

Python 函数numpy.REFORMATE

Python 函数numpy.REFORMATE,python,numpy,reshape,Python,Numpy,Reshape,我在matlab中有这个函数 cn = reshape (repmat (sn, n_rep, 1), 1, []); 没有包含关键代码的python: import numpy like np from numpy.random import randint M = 2 N = 2 * 10 ** 8 ### data value n_rep = 3 ## number of repetitions sn = randint (0, M, size = N) ### integers 0

我在matlab中有这个函数

cn = reshape (repmat (sn, n_rep, 1), 1, []);
没有包含关键代码的python:

import numpy like np
from numpy.random import randint

M = 2
N = 2 * 10 ** 8 ### data value
n_rep = 3 ## number of repetitions
sn = randint (0, M, size = N) ### integers 0 and 1
print ("sn =", sn)
cn_repmat = np.tile (sn, n_rep)
print ("cn_repmat =", cn_repmat)
cn = np.reshape (cn_repmat, 1, [])
print (cn)
我不确定复古的亲属关系是否未知

File "C: / Users / Sergio Malhao / .spyder-py3 / Desktop / untitled6.py", line 17, under <module>
cn = np.reshape (cn_repmat, 1, [])

File "E: \ Anaconda3 \ lib \ site-packages \ numpy \ core \ fromnumeric.py", line 232, in reshape
return _wrapfunc (a, 'reshape', newshape, order = order)

File "E: \ Anaconda3 \ lib \ site-packages \ numpy \ core \ fromnumeric.py", line 57, in _wrapfunc
return getattr (obj, method) (* args, ** kwds)

ValueError: Can not reshape the array of size 600000000 in shape (1,)
文件“C:/Users/Sergio Malhao/.spyder-py3/Desktop/untitled6.py”,第17行,下
cn=np.reporate(cn_repmat,1,[]))
文件“E:\Anaconda3\lib\site packages\numpy\core\fromnumeric.py”,第232行,在整形中
return _wrapfunc(a,‘重塑’、新闻形状、订单=订单)
文件“E:\Anaconda3\lib\site packages\numpy\core\fromneric.py”,第57行,在\u wrapfunc中
返回getattr(对象,方法)(*args,**kwds)
ValueError:无法在形状(1,)中重塑大小为600000000的数组

Numpy不应该是1:1的matlab。它的工作原理相似,但方式不同。 我假设你想把一个矩阵转换成一维数组

尝试:

np.reshape (cn_repmat, (1, -1))
其中(1,-1)是定义新数组大小的元组

一个形状标注可以是-1。在这种情况下,将推断该值 从数组的长度和剩余维度

八度:

>> sn = [0,1,2,3,4]
sn =
   0   1   2   3   4
>> repmat(sn,4,1)
ans =
   0   1   2   3   4
   0   1   2   3   4
   0   1   2   3   4
   0   1   2   3   4
>> reshape(repmat(sn,4,1),1,[])
ans =
   0   0   0   0   1   1   1   1   2   2   2   2   3   3   3   3   4   4   4   4
numpy
中:

In [595]: sn=np.array([0,1,2,3,4])
In [596]: np.repeat(sn,4)
Out[596]: array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4])
In [597]: np.tile(sn,4)
Out[597]: array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4])
在MATLAB中,矩阵至少为2d;在努比,它们可能是1d<代码>输出[596]为1d

我们可以通过制作
sn
2d:

In [608]: sn2 = sn[None,:]    # = sn.reshape((1,-1))
In [609]: sn2
Out[609]: array([[0, 1, 2, 3, 4]])
In [610]: np.repeat(sn2,4,1)
Out[610]: array([[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]])
使用
tile
时,我们必须转换或玩顺序游戏(MATLAB是顺序F):

ravel
相当于
重塑(..,-1)
<代码>-1函数类似于重塑形状时MATLAB中的
[]


numpy
中,repeat
是基本功能
tile
使用不同的用户界面(更像是
repmat
)重复

我得到了这样的结果:sn[0 1 0…,1 1 1 1],cn_repmat[[0 1 0…,1 1 1]],cn=[0 1 0…,1 1 1]]Mikaelblomkvistsongrations。这是正确的还是什么?如果你想让它完全是一维的,那么写:cn=np.squence(np.reforme(cn_repmat,(1,-1)))
In [613]: np.tile(sn,[4,1])
Out[613]: 
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])
In [614]: np.tile(sn,[4,1]).T.ravel()
Out[614]: array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4])
In [615]: np.tile(sn,[4,1]).ravel(order='F')
Out[615]: array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4])