如何使用Numpy在Python中进行描述性统计

In this short post we are going to revisit the topic on how to carry out summary/descriptive statistics in Python. In the previous post, I used Pandas (but also SciPy and Numpy, see Descriptive Statistics Using Python) but now we are only going to use Numpy. The descriptive statistics we are going to calculate are the central tendency (in this case only the mean),  standard deviation, percentiles (25 and 75), min, and max.

在这篇简短的文章中,我们将重新讨论有关如何在Python中执行摘要/描述性统计信息的主题。 在上一篇文章中,我使用了Pandas (还使用了SciPy和Numpy,请参阅使用Python进行描述性统计 ),但是现在我们仅使用Numpy 。 我们将要计算的描述统计量是中心趋势(在这种情况下,仅是平均值),标准偏差,百分位数(25和75),最小值和最大值。

加载数据 (Loading the data)

In this example I am going to use the Toothgrowth dataset (download here). It is pretty easy to load a CSV file using the genfromtxt method:

在此示例中,我将使用Toothgrowth数据集( 在此处下载)。 使用genfromtxt方法加载CSV文件非常容易:

import numpy as np

data_file = 'ToothGrowth.csv'
data = np.genfromtxt(data_file, names=True, delimiter=",", dtype=None)

Notice the arguments we pass. The first row has the names and that is why we set the argument ‘names’ to True. One of the columns, further, has strings. Setting ‘dtype‘ to None enables us to load both floats and integers into our data.

注意我们传递的参数。 第一行包含名称,这就是为什么我们将参数“名称”设置为True的原因。 此外,其中一列具有字符串。 将'dtype'设置为None可使我们将浮点数和整数加载到我们的数据中。

使用Numpy进行描述性统计 (Descriptive statistics using Numpy)

In the next code chunk, below, we are going to loop through each level of the two factors (i.e., ‘supp’, and ‘dose’) and create a subset of the data for each crossed level. If you are familiar with Pandas, you may notice that subsetting a Numpy ndarray is pretty simple (data[data[yourvar] == level). The summary statistics are then appended into a list.

在下面的下一个代码块中,我们将遍历两个因素的每个级别(即“ supp”和“ dose”),并为每个交叉级别创建数据的子集。 如果您熟悉Pandas,您可能会注意到子集Numpy ndarray非常简单(data [data [yourvar] ==级别)。 然后将摘要统计信息添加到列表中。

summary_stats = []
for supp_lvl in np.unique(data['supp']):
    
    for dose_lvl in np.unique(data['dose']):
    
        # Subsetting
        data_to_sum = data[(data['supp'] == supp_lvl) & (data['dose'] == dose_lvl)]
        # Calculating the descriptives
        mean = data_to_sum['len'].mean()
        sd = data_to_sum['len'].std()
        max_supp = data_to_sum['len'].max()
        min_supp =  data_to_sum['len'].min()
        ps = np.percentile(data_to_sum['len'], [25, 75] )
        summary_stats.append((mean, sd, max_supp, min_supp, ps[0], ps[1], supp_lvl, dose_lvl))

结果 (The results)

From the list of data we are going to create a Numpy array. The reason for doing this is that it will get us a bit prettier output. Especially, when we are setting the print options (line 19, below).

从数据列表中,我们将创建一个Numpy数组。 这样做的原因是它将为我们带来更漂亮的输出。 特别是,当我们设置打印选项时(下面的第19行)。

results = np.array(summary_stats, dtype=None)
np.set_printoptions(suppress=True)
print(results)

如何使用Numpy在Python中进行描述性统计_第1张图片

That was it. I still prefer doing my descriptives statistics using Pandas. Primarily, because of that the output is much more nicer but it’s also easier to work with Pandas dataframes compared to Numpy arrays.

就是这样 我仍然更喜欢使用Pandas进行描述性统计。 首先,因为这样,输出要好得多,但与Numpy数组相比,使用Pandas数据帧也更容易。

翻译自: https://www.pybloggers.com/2017/03/how-to-do-descriptive-statistics-in-python-using-numpy/

你可能感兴趣的:(python,数据分析,java,numpy,大数据)