如何解决numpy插入函数中形状误差?

brqmpdu1  于 2023-04-30  发布在  其他
关注(0)|答案(2)|浏览(107)

我在解书上的算术题。写了代码。编写一个python程序,在多个插入点()处将元素插入2d数组的行和列中

## Q2
import numpy as np
# Write a python program to insert elements in row as well as in a column of a 2d array at multiple inserting points ()
# array of 5,5. with 2,4 row and 1,3 column missing
q2arr2d = np.arange(1,26).reshape(5,5)
# [[ 1  2  3  4  5]
#  [ 6  7  8  9 10] 
#  [11 12 13 14 15] 
#  [16 17 18 19 20] 
#  [21 22 23 24 25]]
q2arr2d = np.delete(q2arr2d,(1,3),axis=0)
q2arr2d = np.delete(q2arr2d,(0,2),axis=1)
print('Given array:\n',q2arr2d)
#  [[ 2  4  5]
#  [12 14 15]
#  [22 24 25]]
q2arr2d = np.insert(q2arr2d,(1,2),([6,8,10],[16,18,20]),axis=0)
print('Array after inserting rows:\n',q2arr2d)
#  [[ 2  4  5]
#  [ 6  8 10]
#  [12 14 15]
#  [16 18 20]
#  [22 24 25]]
q2arr2d = np.insert(q2arr2d,(0,2),([1,5,11,15,21],[3,7,13,17,23]),axis=1)
print('Array after inserting column:\n',q2arr2d)

## Q2
# Write a python program to insert elements in row as well as in a colum of a 2d array at multiple insertin points()
# array of 5,5 .with 2,4 row and 1,3 column missing
q2arr2d = np.arange(1,26).reshape(5,5)
# [[ 1  2  3  4  5]
#  [ 6  7  8  9 10] 
#  [11 12 13 14 15] 
#  [16 17 18 19 20] 
#  [21 22 23 24 25]]
q2arr2d = np.delete(q2arr2d,(1,3),axis=0)
q2arr2d = np.delete(q2arr2d,(0,2),axis=1)
print('Given array:\n',q2arr2d)
#  [[ 2  4  5]
#  [12 14 15]
#  [22 24 25]]
q2arr2d = np.insert(q2arr2d,(1,2),([6,8,10],[16,18,20]),axis=0)
print('Array after inserting rows:\n',q2arr2d)
#  [[ 2  4  5]
#  [ 6  8 10]
#  [12 14 15]
#  [16 18 20]
#  [22 24 25]]
q2arr2d = np.insert(q2arr2d,(0,2),([1,5,11,15,21],[3,7,13,17,23]),axis=1)
print('Array after inserting column:\n',q2arr2d)

执行后显示错误

Given array:
 [[ 2  4  5]
 [12 14 15]
 [22 24 25]]
Array after inserting rows:
 [[ 2  4  5]
 [ 6  8 10]
 [12 14 15]
 [16 18 20]
 [22 24 25]]
Traceback (most recent call last):
  File "e:\Codes\NumPy practice\ProjectAndQNA.py", line 61, in <module>
    q2arr2d = np.insert(q2arr2d,(0,2),([1,5,11,15,21],[3,7,13,17,23]),axis=1)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<__array_function__ internals>", line 180, in insert
  File "C:\Users\VinodKumarPunia\AppData\Local\Programs\Python\Python311\Lib\site-packages\numpy\lib\function_base.py", line 5377, in insert
    new[tuple(slobj)] = values
    ~~~^^^^^^^^^^^^^^
ValueError: shape mismatch: value array of shape (2,5) could not be broadcast to indexing result of shape (5,2)

如果你需要任何更多的信息有关的代码,我准备回答它,这个不匹配的错误是不是从这么多的时间得到解决。我已经尝试了整形,但数组中有不同的和扭曲的值。即使改变插入轴也不会有什么区别。

7xllpg7q

7xllpg7q1#

新值需要与数组形状一致,例如([1,5,11,15,21],[3,7,13,17,23])应为([[1,3], [5, 7], [11, 13], [15, 17], [21, 23]])

## Q2
import numpy as np
# Write a python program to insert elements in row as well as in a column of a 2d array at multiple inserting points ()
# array of 5,5. with 2,4 row and 1,3 column missing
q2arr2d = np.arange(1,26).reshape(5,5)
# [[ 1  2  3  4  5]
#  [ 6  7  8  9 10] 
#  [11 12 13 14 15] 
#  [16 17 18 19 20] 
#  [21 22 23 24 25]]
q2arr2d = np.delete(q2arr2d,(1,3),axis=0)
q2arr2d = np.delete(q2arr2d,(0,2),axis=1)
print('Given array:\n',q2arr2d)
#  [[ 2  4  5]
#  [12 14 15]
#  [22 24 25]]
q2arr2d = np.insert(q2arr2d,(1,2),([6,8,10],[16,18,20]),axis=0)
print('Array after inserting rows:\n',q2arr2d)
#  [[ 2  4  5]
#  [ 6  8 10]
#  [12 14 15]
#  [16 18 20]
#  [22 24 25]]
q2arr2d = np.insert(q2arr2d,(0,2),([[1,3], [5, 7], [11, 13], [15, 17], [21, 23]]),axis=1)
print('Array after inserting column:\n',q2arr2d)

## Q2
# Write a python program to insert elements in row as well as in a colum of a 2d array at multiple insertin points()
# array of 5,5 .with 2,4 row and 1,3 column missing
q2arr2d = np.arange(1,26).reshape(5,5)
# [[ 1  2  3  4  5]
#  [ 6  7  8  9 10] 
#  [11 12 13 14 15] 
#  [16 17 18 19 20] 
#  [21 22 23 24 25]]
q2arr2d = np.delete(q2arr2d,(1,3),axis=0)
q2arr2d = np.delete(q2arr2d,(0,2),axis=1)
print('Given array:\n',q2arr2d)
#  [[ 2  4  5]
#  [12 14 15]
#  [22 24 25]]
q2arr2d = np.insert(q2arr2d,(1,2),([6,8,10],[16,18,20]),axis=0)
print('Array after inserting rows:\n',q2arr2d)
#  [[ 2  4  5]
#  [ 6  8 10]
#  [12 14 15]
#  [16 18 20]
#  [22 24 25]]
q2arr2d = np.insert(q2arr2d,(0,2),([[1,3], [5, 7], [11, 13], [15, 17], [21, 23]]),axis=1)
print('Array after inserting column:\n',q2arr2d)
zazmityj

zazmityj2#

如果将索引对象更改为一个索引而不是元组,则代码可以正常工作。
替换:q2arr2d = np。insert(q2arr2d,(0,2),([1,5,11,15,21],3,7,13,17,23]),axis=1)
其中:q2arr2d = np。insert(q2arr2d,2,([1,5,11,15,21],[3,7,13,17,23]),axis=1)

相关问题