博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何使用Numpy在Python中进行描述性统计
阅读量:2519 次
发布时间:2019-05-11

本文共 2988 字,大约阅读时间需要 9 分钟。

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 (but also SciPy and Numpy, see ) but now we are only going to use . 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中执行摘要/描述性统计信息的主题。 在上一篇文章中,我使用了 (还使用了SciPy和Numpy,请参阅 ),但是现在我们仅使用 。 我们将要计算的描述统计量是中心趋势(在这种情况下,仅是平均值),标准偏差,百分位数(25和75),最小值和最大值。

加载数据 (Loading the data)

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

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

import numpy as npdata_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)

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数据帧也更容易。

翻译自:

转载地址:http://grqwd.baihongyu.com/

你可能感兴趣的文章
Settings app简单学习记录
查看>>
SQLAlchemy
查看>>
多线程
查看>>
使用缓存的9大误区(下)转载
查看>>
appium键值对的应用
查看>>
MyEclipse 8.X 通用算法
查看>>
selenium.Phantomjs设置浏览器请求头
查看>>
分布式数据库如何选择,几种分布式数据库优缺点一览
查看>>
BZOJ 4443: 小凸玩矩阵【二分图】
查看>>
苹果 OS X制作u盘启动盘
查看>>
Jquery便利对象
查看>>
MVC: Connection String
查看>>
idea常用设置汇总
查看>>
Node.SelectNodes
查看>>
Lambda表达式语法进一步巩固
查看>>
Vue基础安装(精华)
查看>>
Git 提交修改内容和查看被修改的内容
查看>>
PAT - 1008. 数组元素循环右移问题 (20)
查看>>
请求出现 Nginx 413 Request Entity Too Large错误的解决方法
查看>>
配置php_memcache访问网站的步骤
查看>>