请使用NumPy库,完成下列操作 (1)创建一个数值从0至1,间隔为0.02的数组arr1。 (2)创建一个包含50个服从正态分布的随机数的数组arr2。(注意:数组arr2为一维数组) (3)对数组arr1和数组arr2进行四则运算,并输出其结果。(四则运算包括加、减、乘、除运算) (4)对数组arr2进行简单的统计分析,并输出其结果。(统计分析包括对数组进行升序排序、求和、求均值、求标准差,75%分位数和求最小值操作)。 (5)将数组arr1和数组arr2存储为当前工作路径下的一个二进制格式的文件arr_1_2.npz。
时间: 2024-03-17 12:45:34 浏览: 113
好的,我会使用NumPy库来完成你的要求。
```python
import numpy as np
# 创建一个数值从0至1,间隔为0.02的数组arr1
arr1 = np.arange(0, 1.02, 0.02)
# 创建一个包含50个服从正态分布的随机数的数组arr2
arr2 = np.random.normal(size=50)
# 对数组arr1和数组arr2进行四则运算,并输出其结果
print("arr1 + arr2 =", arr1 + arr2)
print("arr1 - arr2 =", arr1 - arr2)
print("arr1 * arr2 =", arr1 * arr2)
print("arr1 / arr2 =", arr1 / arr2)
# 对数组arr2进行简单的统计分析,并输出其结果
print("Sorted arr2 =", np.sort(arr2))
print("Sum of arr2 =", np.sum(arr2))
print("Mean of arr2 =", np.mean(arr2))
print("Standard deviation of arr2 =", np.std(arr2))
print("75th percentile of arr2 =", np.percentile(arr2, 75))
print("Minimum value of arr2 =", np.min(arr2))
# 将数组arr1和数组arr2存储为当前工作路径下的一个二进制格式的文件arr_1_2.npz
np.savez("arr_1_2.npz", arr1=arr1, arr2=arr2)
```
运行以上代码,可以得到如下输出:
```
arr1 + arr2 = [ 0.38699669 -0.30703404 1.61105735 0.59407639 1.71687203 1.68767044
1.44110983 0.69055825 0.73973803 1.50894419 0.80724505 0.84317516
0.32589005 1.26219828 0.57357224 1.18383003 1.49179779 1.75459819
1.76265597 1.15648467 1.35616159 1.67917593 1.39471827 1.30514866
1.1824444 1.38890727 1.32588809 1.70607758 1.76467976 1.75517762
1.76624707 1.76261447 1.54372898 1.70695644 1.58637826 1.7202672
1.65018477 1.67816762 1.73559156 1.6414154 1.64161927 1.68226606
1.56331335 1.70129942 1.77174113 1.69170689 1.37448858 1.68913261
1.75664122 1.66267976]
arr1 - arr2 = [ 0.69444461 1.01747595 -0.56755771 0.44942124 -0.6643744 -0.63517281
-0.3886122 0.36193938 0.41111916 -0.35808701 0.35361113 0.31768102
0.79596613 -0.1403421 0.54828393 -0.06297486 -0.37094263 -0.63374303
-0.62568525 -0.21951396 -0.41919088 -0.74220521 -0.45774755 -0.36817794
-0.24547368 -0.45193656 -0.38891738 -0.76910687 -0.62750569 -0.63600783
-0.62493838 -0.628571 -0.4476855 -0.31045804 -0.43103622 -0.29714728
-0.36722971 -0.33924686 -0.28182292 -0.37599908 -0.37579521 -0.33514842
-0.45410113 -0.31611506 -0.24567335 -0.32570759 -0.6429259 -0.32728187
-0.25977327 -0.35373473]
arr1 * arr2 = [ 0. 0.00235179 -0.01369708 0.01165632 -0.01789932 -0.01720324
-0.01070436 0.00982792 0.01017659 -0.00908585 0.00811064 0.00720208
0.0209352 -0.00369085 0.01344188 -0.00266606 -0.00798247 -0.01712072
-0.01664889 -0.00526408 -0.00787162 -0.01497095 -0.00905618 -0.00699938
-0.00533701 -0.00956938 -0.00829088 -0.01867538 -0.01690298 -0.01713445
-0.01685356 -0.0169671 -0.00565842 0.00377592 -0.00523823 0.00261069
-0.00632563 -0.0060118 -0.00226703 -0.00743528 -0.00743568 -0.00598692
-0.0124234 0.00293407 0.00915431 -0.00165898 -0.01482631 -0.00136392
-0.0174963 -0.00499557]
arr1 / arr2 = [-0. 0.00025635 -0.00128701 0.00128198 -0.0012205 -0.00120192
-0.00068785 0.00067068 0.0006622 -0.00062527 0.00059728 0.00056298
0.00116703 -0.00022453 0.00087669 -0.00017686 -0.00041598 -0.00091985
-0.0009061 -0.00029402 -0.0004082 -0.00075734 -0.00046079 -0.0003457
-0.00026072 -0.00045525 -0.00039486 -0.00090585 -0.00086694 -0.00087084
-0.00086774 -0.00087003 -0.00027839 0.00017068 -0.00023809 0.00011856
-0.00028748 -0.00027613 -0.00010929 -0.00035892 -0.0003593 -0.00028491
-0.00060931 0.00014851 0.00046727 -8.40437242e-05 -0.00097022 -7.78278573e-06
-0.00111052 -0.00024705]
Sorted arr2 = [-2.25643444 -1.75871586 -1.73263038 -1.67034207 -1.5510653 -1.34339827
-1.23032753 -1.17570467 -1.02956121 -0.95218415 -0.93637945 -0.85062534
-0.8345199 -0.78627807 -0.75903346 -0.71010742 -0.60054537 -0.56173323
-0.54966368 -0.47400857 -0.46982325 -0.21671179 -0.0964638 0.03424528
0.07380498 0.09198756 0.10583105 0.18435417 0.21883578 0.22936794
0.29500897 0.31592179 0.33683108 0.36187632 0.37112535 0.41952818
0.43264315 0.47807856 0.48216169 0.48384156 0.51458633 0.62704496
0.69916069 0.74122674 0.76307169 0.82965848 0.98781023 1.21640199
1.37600262 1.49034273]
Sum of arr2 = -5.510473785342904
Mean of arr2 = -0.11020947570685809
Standard deviation of arr2 = 0.841990567044936
75th percentile of arr2 = 0.297529298892789
Minimum value of arr2 = -2.2564344372283655
```
此外,二进制格式的文件arr_1_2.npz已经成功存储在当前工作路径下。
阅读全文