data=no.array([[1,2,3],[4,5,6],[7,8,9]])使用axis对给定的二维数组的行和列分别求和
时间: 2024-06-14 18:04:50 浏览: 190
使用axis参数可以对给定的二维数组的行和列分别求和。具体方法如下:
1. 对行求和:
```python
import numpy as np
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
row_sum = np.sum(data, axis=1)
print("行求和结果:", row_sum) # 输出:[ 6 15 24]
```
2. 对列求和:
```python
import numpy as np
data = np.array([[1, 2, 3 [4, 5, 6], [7, 8, 9column_sum = np.sum(data, axis=0)
print("列求和结果: column_sum) # 输出:[12 15 18]
```
相关问题
程序执行提示AttributeError: 'point_cloud_generator' object has no attribute 'widthself',优化程序class point_cloud_generator(): def __init__(self, rgb_file, depth_file, save_ply, camera_intrinsics=[784.0, 779.0, 649.0, 405.0]): self.rgb_file = rgb_file self.depth_file = depth_file self.save_ply = save_ply self.rgb = cv2.imread(rgb_file) self.depth = cv2.imread(self.depth_file, -1) print("your depth image shape is:", self.depth.shape) self.width = self.rgb.shape[1] self.height = self.rgb.shape[0] self.camera_intrinsics = camera_intrinsics self.depth_scale = 1000 def compute(self): t1 = time.time() depth = np.asarray(self.depth, dtype=np.uint16).T # depth[depth==65535]=0 self.Z = depth / self.depth_scale fx, fy, cx, cy = self.camera_intrinsics X = np.zeros((self.width, self.height)) Y = np.zeros((self.width, self.height)) for i in range(self.width): X[i, :] = np.full(X.shape[1], i) self.X = ((X - cx / 2) * self.Z) / fx for i in range(self.height): Y[:, i] = np.full(Y.shape[0], i) self.Y = ((Y - cy / 2) * self.Z) / fy data_ply = np.zeros((6, self.width * self.height)) data_ply[0] = self.X.T.reshape(-1)[:self.widthself.height] data_ply[1] = -self.Y.T.reshape(-1)[:self.widthself.height] data_ply[2] = -self.Z.T.reshape(-1)[:self.widthself.height] img = np.array(self.rgb, dtype=np.uint8) data_ply[3] = img[:, :, 0:1].reshape(-1)[:self.widthself.height] data_ply[4] = img[:, :, 1:2].reshape(-1)[:self.widthself.height] data_ply[5] = img[:, :, 2:3].reshape(-1)[:self.widthself.height] self.data_ply = data_ply t2 = time.time() print('calcualte 3d point cloud Done.', t2 - t1)
在代码中有两处出现了 `self.widthself.height`,这应该是想要写成 `self.width * self.height`。你需要修改这两处,如下所示:
```
data_ply[0] = self.X.T.reshape(-1)[:self.width * self.height]
data_ply[1] = -self.Y.T.reshape(-1)[:self.width * self.height]
data_ply[2] = -self.Z.T.reshape(-1)[:self.width * self.height]
img = np.array(self.rgb, dtype=np.uint8)
data_ply[3] = img[:, :, 0:1].reshape(-1)[:self.width * self.height]
data_ply[4] = img[:, :, 1:2].reshape(-1)[:self.width * self.height]
data_ply[5] = img[:, :, 2:3].reshape(-1)[:self.width * self.height]
```
这样就可以避免出现 `AttributeError: 'point_cloud_generator' object has no attribute 'widthself'` 的错误了。
const processSaleBoundArr = async (saleBoundArr, tokenData, accountData) => { let saleOutBounds = [] if (saleBoundArr?.length > 0) { let customerIds = [], customArr = [] for (const item of saleBoundArr) { customerIds.push(item.customer_id) customerIds = Array.from(new Set(customerIds)); } for (const i of customerIds) { const customerDetailRes = await getCustomerDetail( { id: i }, { "app-token": tokenData['app-token'], "X-GW-Router-Addr": accountData?.domain } ); if (customerDetailRes.data.errcode === 0) { const customerData = customerDetailRes.data.data customArr.push(customerData) } } for (const s of saleBoundArr) { for (const c of customArr) { if (s.customer_id === c.customer_id) { s.account_open_addr = c.account_open_addr s.addr = c.addr s.bank = c.bank s.bank_account = c.bank_account s.taxpayer_no = c.taxpayer_no s.orgName = c.sale_dept_name saleOutBounds.push(s) } } } } return saleOutBounds } 优化代码
以下是对上述代码的优化建议:
1. 对于 `saleBoundArr?.length > 0` 这个条件判断,可以使用 `if (saleBoundArr && saleBoundArr.length)` 来代替,更加简洁明了。
2. 在获取客户详细信息的过程中,可以使用 `Promise.all()` 方法并行获取多个客户的详细信息,避免顺序执行耗费时间。
3. 可以使用 `Array.prototype.find()` 方法代替双重循环查找客户详细信息,提高查找效率。
4. 在获取客户详细信息时,可以使用 `map()` 方法将客户 `id` 数组转换为客户详细信息数组,避免使用多次 `push()` 方法。
下面是优化后的代码示例:
```javascript
const processSaleBoundArr = async (saleBoundArr, tokenData, accountData) => {
let saleOutBounds = [];
if (saleBoundArr && saleBoundArr.length) {
const customerIds = [...new Set(saleBoundArr.map(item => item.customer_id))];
const customerPromises = customerIds.map(id => {
return getCustomerDetail(
{ id },
{ "app-token": tokenData['app-token'], "X-GW-Router-Addr": accountData?.domain }
);
});
const customerDetails = await Promise.all(customerPromises);
saleOutBounds = saleBoundArr.map(saleBound => {
const customerDetail = customerDetails.find(detail => detail.data.errcode === 0 && detail.data.data.customer_id === saleBound.customer_id);
if (customerDetail) {
const customerData = customerDetail.data.data;
saleBound.account_open_addr = customerData.account_open_addr;
saleBound.addr = customerData.addr;
saleBound.bank = customerData.bank;
saleBound.bank_account = customerData.bank_account;
saleBound.taxpayer_no = customerData.taxpayer_no;
saleBound.orgName = customerData.sale_dept_name;
}
return saleBound;
});
}
return saleOutBounds;
};
```
这个优化后的代码使用了一些 JavaScript 的高级语法,如 `Promise.all()` 和 `Array.prototype.find()` 方法,使代码更加简洁高效。
阅读全文
相关推荐
















