Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.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.matrix分配给numpy.matrix中的列_Python_Numpy_Matrix - Fatal编程技术网

Python 如何将(行)numpy.matrix分配给numpy.matrix中的列

Python 如何将(行)numpy.matrix分配给numpy.matrix中的列,python,numpy,matrix,Python,Numpy,Matrix,相关代码如下。我创建了一个零矩阵,这里正好是一个2X2矩阵。然后我遍历一个数据文件,在输入数据集的每一列的范围内用随机数填充矩阵。这是可行的,除了输出矩阵是转置的,我更愿意做得对。请参见代码中的注释 centroids = mat(zeros((k,n))) #create centroid mat print('centroids: \n', centroids, '\n', type(centroids)) print('starting for loop for j in

相关代码如下。我创建了一个零矩阵,这里正好是一个2X2矩阵。然后我遍历一个数据文件,在输入数据集的每一列的范围内用随机数填充矩阵。这是可行的,除了输出矩阵是转置的,我更愿意做得对。请参见代码中的注释

centroids = mat(zeros((k,n))) #create centroid mat
print('centroids: \n', centroids, '\n', type(centroids))        
print('starting for loop for j in range(%s)' %(n))
    for j in range(n):#create random cluster centers, within bounds of each dimension
        print('\n')
        # get the min value in the jth column
        minJ = min(dataSet[:,j]) 
        # get the max value in the jth column
        maxJ = max(dataSet[:,j]) 
        # the range of values is max - min
        rangeJ = maxJ - minJ
        print('col %s, min = %s, max = %s, range = %s' %(j, minJ, maxJ, rangeJ))
        # create a 'column' of random values for each colum
        col =  mat(minJ + rangeJ * random.rand(1,k))
        print('column %s is %s, type is %s' %(j, col, type(col)))
        # assign columns to column in centroids
        # DOES NOT WORK, assigns to rows.
        centroids[j] = col
        print('   ==> centroids: \n', centroids)
    return centroids
这是输出。请注意,输出数组/应该是[[3.08、.434]、-1.36、.203]]

centroids:
 [[0. 0.]
 [0. 0.]]
 <class 'numpy.matrix'>
starting for loop for j in range(2)


col 0, min = [[-5.379713]], max = [[4.838138]], range = [[10.217851]]
column 0 is [[ 3.08228829 -1.35924539]], type is <class 'numpy.matrix'>
   ==> centroids:
 [[ 3.08228829 -1.35924539]
 [ 0.          0.        ]]


col 1, min = [[-4.232586]], max = [[5.1904]], range = [[9.422986]]
column 1 is [[ 0.4342251 -0.2026065]], type is <class 'numpy.matrix'>
   ==> centroids:
 [[ 3.08228829 -1.35924539]
 [ 0.4342251  -0.2026065 ]]
================

centroids follows:
[[3.08228829]
 [0.4342251 ]]
[[-1.35924539]
 [-0.2026065 ]]
以下是错误消息:

Traceback (most recent call last):
  File "run.py", line 68, in <module>
    centroids = randCent(dataList, 2)
  File "run.py", line 51, in randCent
    centroids[0:1,j] = col
ValueError: could not broadcast input array from shape (1,2) into shape (1,1)

指定给二维阵列:

In [500]: A = np.zeros((2,3), int)                                                                   
In [501]: A[0,:] = np.arange(3)                                                                      
In [502]: A[:,1] = [10,20]                                                                           
In [503]: A                                                                                          
Out[503]: 
array([[ 0, 10,  2],
       [ 0, 20,  0]])
In [504]: A = np.zeros((2,3), int)                                                                   
In [505]: A[0,:] = [1,2,3]                                                                           
In [506]: A[:,1] = [10,20]                                                                           
In [507]: A                                                                                          
Out[507]: 
array([[ 1, 10,  3],
       [ 0, 20,  0]])
np.matrix
上尝试同样的方法:

In [512]: M = np.matrix(np.zeros((2,3),int))                                                         
In [513]: M                                                                                          
Out[513]: 
matrix([[0, 0, 0],
        [0, 0, 0]])
In [514]: M[0,:] = [1,2,3]                                                                           
In [515]: M[:,1] = [10,20]                                                                           
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-515-e95a3ab21d7f> in <module>
----> 1 M[:,1] = [10,20]

ValueError: could not broadcast input array from shape (2) into shape (2,1)
In [516]: M[:,1] = [[10],[20]]                                                                       
In [517]: M                                                                                          
Out[517]: 
matrix([[ 1, 10,  3],
        [ 0, 20,  0]])
要为(2,1)形空间指定值,需要(2,1)值。广播只能在(1,2)中添加前导维度(2),而不能在(2,1)中添加前导维度

flatiter可用于分配1d阵列:

In [520]: M[:,1].flat                                                                                
Out[520]: <numpy.flatiter at 0x7f57b127dda0>
In [521]: M[:,1].flat = [100,200]                                                                    
In [522]: M                                                                                          
Out[522]: 
matrix([[  1, 100,   3],
        [  0, 200,   0]])                          
[520]中的
M[:,1]。平坦
出[520]:
[521]中:M[:,1]。平坦=[100200]
In[522]:M
出[522]:
矩阵([[1100,3],
[  0, 200,   0]])                          

这是一大堆代码。你确定所有这些对于一个最小的可复制的例子都是必要的吗?转置有什么错?您为什么要使用
mat
,而不是2d
ndarray
?@hrokr原始代码不是MRE,因为它缺少变量和导入,所以添加了代码以使其正常工作。谢谢。Python不是我的主要语言,但我经常使用它。我珍视这些疯狂的时刻。“弗拉蒂特”谁会“砰”的一声?
In [512]: M = np.matrix(np.zeros((2,3),int))                                                         
In [513]: M                                                                                          
Out[513]: 
matrix([[0, 0, 0],
        [0, 0, 0]])
In [514]: M[0,:] = [1,2,3]                                                                           
In [515]: M[:,1] = [10,20]                                                                           
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-515-e95a3ab21d7f> in <module>
----> 1 M[:,1] = [10,20]

ValueError: could not broadcast input array from shape (2) into shape (2,1)
In [516]: M[:,1] = [[10],[20]]                                                                       
In [517]: M                                                                                          
Out[517]: 
matrix([[ 1, 10,  3],
        [ 0, 20,  0]])
In [518]: A[:,1]                                                                                     
Out[518]: array([10, 20])
In [519]: M[:,1]                                                                                     
Out[519]: 
matrix([[10],
        [20]])
In [520]: M[:,1].flat                                                                                
Out[520]: <numpy.flatiter at 0x7f57b127dda0>
In [521]: M[:,1].flat = [100,200]                                                                    
In [522]: M                                                                                          
Out[522]: 
matrix([[  1, 100,   3],
        [  0, 200,   0]])