numpy或scipy入门? [英] Getting started with numpy or scipy?

查看:79
本文介绍了numpy或scipy入门?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照前面帖子中有帮助的撰稿人的建议浏览了numpyscipy的描述.

I went through the descriptions of numpy and scipy as recommended by helpful contributors in the earlier posts.

由于我是Python的初学者,因此请您提供帮助:例如,我在c:\\programming文件夹中有一个文件(stats1.txt);该.txt文件具有一列包含1000个值;现在,我想获取这些值的描述性统计信息(平均值,中位数,最大值,最小值,标准差).

Since I am a beginner in Python, would you please assist me on this: for example, I have a file (stats1.txt) in a folder c:\\programming; this .txt file has one column with 1000 values; now I would like to get descriptive statistics of these values (mean, median, max, min, std).

如何开始使用numpyscipy编写代码以在同一文件或新文本文件中执行结果?

How do I begin writing a code using numpy or scipy to execute the result in either the same file or a new text file?

推荐答案

这是一种面向对象的语言,它将对python的工作方式有一些基本的了解.因此,您基本上可以解析输入文件, 由于您说文件只有一列,因此只需要将值转换为数字

It will take some basic understanding of how python works, it is an object oriented language. So you basically parse the input file, Since you say the file has only one column, you just need to convert the values to numbers

with open('stats1.txt') as f:
    lines=[float(i) for i in f.xreadlines()] 

这将为您提供一张表格,类似于使用电子表格打开该表格时所看到的表格,请注意,该表格假定所有值都是数字,如果您混入了文字,则将需要更多排序.然后将其变成一个numpy数组

That will get you a table similar to what you'd see if you opened it with a spreadsheet, note that it assumes all values are numbers, if you have text mixed in, it will take some more sorting. Then you turn that into a numpy array

nlines=numpy.array(lines)

然后,获取您的统计值,

Then, to get your statistical values,

stdev=nlines.std()
min_=nlines.min()
max_=nlines.max()
mean=nlines.mean()
import collections
mode=collections.Counter(nlines).most_common()[0]

这篇关于numpy或scipy入门?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆