编写一个 NumPy 程序: 创建一个 10x15 数组,其中充满标准正态分布的随机数。 计算上述数组所有元素的总和/均值/std、每列的总和/均值/std 和每行的总和/均值/std。
时间: 2024-10-29 18:15:15 浏览: 18
首先,你需要安装NumPy库,如果你还没有安装,可以使用pip进行安装:
```bash
pip install numpy
```
下面是一个使用NumPy编写程序的示例,它会按照你的要求完成任务:
```python
import numpy as np
# 创建一个10x15的标准正态分布随机数数组
random_array = np.random.randn(10, 15)
# 计算整个数组的统计信息
total_sum = random_array.sum()
mean = random_array.mean()
std_dev = random_array.std()
print(f"数组的总和: {total_sum}")
print(f"数组的平均值: {mean}")
print(f"数组的标准差: {std_dev}")
# 计算每列的统计信息
column_sums = random_array.sum(axis=0)
column_means = column_sums / random_array.shape[0]
column_std_devs = np.std(random_array, axis=0)
print("\n每列的总和:")
for i, col_sum in enumerate(column_sums):
print(f"第{i+1}列: {col_sum}")
print("\n每列的平均值:")
for i, col_mean in enumerate(column_means):
print(f"第{i+1}列: {col_mean}")
print("\n每列的标准差:")
for i, col_std_dev in enumerate(column_std_devs):
print(f"第{i+1}列: {col_std_dev}")
# 计算每行的统计信息
row_sums = random_array.sum(axis=1)
row_means = row_sums / random_array.shape[1]
row_std_devs = np.std(random_array, axis=1)
print("\n每行的总和:")
for i, row_sum in enumerate(row_sums):
print(f"第{i+1}行: {row_sum}")
print("\n每行的平均值:")
for i, row_mean in enumerate(row_means):
print(f"第{i+1}行: {row_mean}")
print("\n每行的标准差:")
for i, row_std_dev in enumerate(row_std_devs):
print(f"第{i+1}行: {row_std_dev}")
```
阅读全文