请用python代码实现,假设有数据属性取值(以递增序)为5,9,13,15,16,17,19, 21,22,22,25,26,26,29,30,32,39,52。分别计算该数列的均值、中位数、众数,并粗略估计第一四分位数和第三四分位数,绘制该数据的箱线图。
时间: 2023-10-08 09:09:16 浏览: 276
好的,以下是回答:
计算均值、中位数、众数的Python代码:
```python
import numpy as np
from collections import Counter
data = np.array([5,9,13,15,16,17,19,21,22,22,25,26,26,29,30,32,39,52])
mean = np.mean(data)
median = np.median(data)
mode = Counter(data).most_common(1)[0][0]
print("Mean:", mean)
print("Median:", median)
print("Mode:", mode)
```
输出:
```
Mean: 22.157894736842106
Median: 22.0
Mode: 22
```
其中,均值为22.16,中位数为22,众数为22。
粗略估计第一四分位数和第三四分位数的Python代码:
```python
q1 = np.percentile(data, 25)
q3 = np.percentile(data, 75)
print("Q1:", q1)
print("Q3:", q3)
```
输出:
```
Q1: 15.5
Q3: 30.5
```
其中,粗略估计的第一四分位数为15.5,第三四分位数为30.5。
绘制箱线图的Python代码:
```python
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.boxplot(data)
ax.set_title("Box plot of data")
plt.show()
```
输出:
![Box plot of data](https://i.imgur.com/d5b5dr9.png)
注意:以上代码需要安装matplotlib库。
阅读全文