2012年9月12日星期三

转:R language for t-tests

t-tests

The t.test( ) function produces a variety of t-tests. Unlike most statistical packages, the default assumes unequal variance and applies the Welsh df modification.# independent 2-group t-test
t.test(y~x) # where y is numeric and x is a binary factor

# independent 2-group t-test
t.test(y1,y2) # where y1 and y2 are numeric

# paired t-test
t.test(y1,y2,paired=TRUE) # where y1 & y2 are numeric

# one samle t-test
t.test(y,mu=3) # Ho: mu=3

You can use the var.equal = TRUE option to specify equal variances and a pooled variance estimate. You can use the alternative="less" or alternative="greater" option to specify a one tailed test.

2012年9月8日星期六

箱线图(boxplot)介绍

箱线图(boxplot)介绍
2012年08月28日 Glossary ⁄ 共 1431字 评论数 2 ⁄ 被围观 96 views+
箱线图(Boxplot)也称箱须图(Box-whisker Plot),是利用数据中的五个统计量:最小值、第一四分位数、中位数、第三四分位数与最大值来描述数据的一种方法。它也可以粗略地看出数据是否具有有对 称性,分布的离散程度等信息;特别适用于对几个样本的比较。
A boxplot is a way of summarizing a set of data measured on an interval scale. It is often used in exploratory data analysis. It is a type of graph which is used to show the shape of the distribution, its central value, and variability. The picture produced consists of the most extreme values in the data set (maximum and minimum values), the lower and upper quartiles, and the median.
可以通过箱线图的绘制过程来了解箱线图的意义:
  1. 绘制数轴
  2. 计算上四分位数(Q3),中位数,下四分位数(Q1)。
  3. 计算上四分位数和下四分位数之间的差值(Q3-Q1),即四分位数差(IQR,interquartile range)。
  4. 绘制箱线图的矩形,上限为上四分位数,下限为下四分位数。在矩形内部中位数的位置画一条横线(中位线)。
  5. 在Q3+1.5IQR和Q1-1.5IQR处画两条与中位线一样的线段,这两条线段为异常值截断点,称为内限;在Q3+3IQR和Q1-3IQR 处画两条线段,称为外限。处于内限以外位置的点所表示的数据都是异常值(outliers),其中在内限与外限之间的异常值为温和的异常值(mild outliers),在外限以外的为极端的异常值(extreme outliers)。(注意:统计软件绘制的箱线图一般都没有标出内限和外限。)
  6. 在非异常值的数据中,最靠近上边缘和下边缘(即内限)的两个数值处,画横线,作为箱线图的触须。
  7. 从矩形的两端向外各画一条线段直到不是异常值的最远点(即上一步的触须),表示该批数据正常值的分布区间。
  8. 温和的异常值(即处于1.5倍-3倍四分位数差之间的异常值)用空心点表示;极端的异常值(即超出四分位数差3倍距离的异常值)用实心点(也可以用星号*)表示。
附上一张图以便利于理解:
boxplot
上图中:最小值(min)=0.5;下四分位数(Q1)=7;中位数(Med)=8.5;上四分位数(Q3)=9;最大值(max)=10;平均值=8;四分位数差(interquartile range,四分位间距)=Q3 − Q1=2。
下面额外补充几个箱线图,并作简单图示:
图例1:
箱线图boxplot
图例2:
箱线图
图例3:
箱线图
箱线图美中不足之处在于它不能提供关于数据分布偏态和尾重程度的精确度量;对于批量较大的数据集,箱线图反映的形状信息更加模糊;用中位数代表总体 平均水平有一定的局限性等等。所以,应用箱线图最好结合其它描述统计工具如均值、标准差、偏度、分布函数等来描述数据集的分布形状
更多关于箱线图(boxplot)的介绍请阅读:
什么是箱线图 , 箱线图(wiki) , 箱形图(wikipedia) ,Box plot ,Box Plot ,Boxplot ,Box Plots
本文来自:http://yixf.name/2011/02/13/%E7%AE%B1%E7%BA%BF%E5%9B%BE/ (有部分删改)

2012年8月4日星期六

Quote:How to add a Directory to the Path in Linux

Pre and Post Pathing Linux determines the executable search path with the $PATH environment variable. To add directory /data/myscripts to the beginning of the $PATH environment variable, use the following:
PATH=/data/myscripts:$PATH
To add that directory to the end of the path, use the following command:
PATH=$PATH:/data/myscripts
重启Terminal后生效;
若希望设置路径立即生效,可输入:
source .bashrc

Adding to a Single User's Path

To add a directory to the path of a single user, place the lines in that user's .bash_profile file. Typically, .bash_profile already contains changes to the $PATH variable and also contains an export statement, so you can simply add the desired directory to the end or beginning of the existing statement that changes the $PATH variable. However, if .bash_profile doesn't contain the path changing code, simply add the following two lines to the end of the .bash_profile file:
PATH=$PATH:/data/myscripts
export PATH

Adding to All Users' Paths (except root)


You globally set a path in /etc/profile. That setting is global for all users except user root. Typical /etc/profile files extensively modify the $PATH variable, and then export that variable. What that means is you can modify the path by appending or prepending the desired directory(s) in existing statements modifying the path. Or, you can add your own path modification statements anywhere before the existing export statement. In the very unlikely event that there are no path modification or export statements in /etc/profile, you can insert the following 2 lines of code at the bottom of /etc/profile:

PATH=$PATH:/data/myscripts
export PATH

Adding to the Path of User root

User root is a special case, at least on Mandrake systems. Unlike other users, root is not affected by the path settings in /etc/profile. The reason is simple enough. User root's path is set from scratch by its .bash_profile script. In order to add to the path of user root, modify its .bash_profile.

Summary

A fundimental administration task is adding directories to the execution paths of one or more users. The basic code to do so is:
PATH=$PATH:/data/myscripts
export PATH

Place that code, or whatever part of that code isn't already incorporated, in one of the following places:

User ClassScript to modify
One user$HOME/.bash_profile
All users except root/etc/profile
root/root/.bash_profile
export PATH
Quoted from: http://www.troubleshooters.com/linux/prepostpath.htm

2012年7月29日星期日

转:Python: How to Sort a List

Python: How to Sort a List【转】


很多时候,我们需要对List进行排序,Python提供了两个方法
对给定的List L进行排序,
方法1.用List的成员函数sort进行排序
方法2.用built-in函数sorted进行排序(从2.4开始)
这两种方法使用起来差不多,以第一种为例进行讲解:
从Python2.4开始,sort方法有了三个可选的参数,Python Library Reference里是这样描述的
cmp:cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: 
"cmp=lambda x,y: cmp(x.lower(), y.lower())" 
key:key specifies a function of one argument that is used to extract a comparison key from each list element: "key=str.lower"reverse:
reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.In general, the key and reverse conversion processes are much faster than specifying an 
equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once.
以下是sort的具体实例。
实例1:
>>>L = [2,3,1,4]
>>>L.sort()
>>>L
>>>[1,2,3,4]
实例2:
>>>L = [2,3,1,4]
>>>L.sort(reverse=True)
>>>L
>>>[4,3,2,1]
实例3:
>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>L.sort(cmp=lambda x,y:cmp(x[1],y[1]))
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
实例4:
>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>L.sort(key=lambda x:x[1])
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
实例5:
>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>import operator
>>>L.sort(key=operator.itemgetter(1))
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
实例6:(DSU方法:Decorate-Sort-Undercorate)
>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>A = [(x[1],i,x) for i,x in enumerate(L)] #i can confirm the stable sort
>>>A.sort()
>>>L = [s[2] for s in A]
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
以上给出了6中对List排序的方法,其中实例3.4.5.6能起到对以List item中的某一项
为比较关键字进行排序.
效率比较:
cmp < DSU < key
通过实验比较,方法3比方法6要慢,方法6比方法4要慢,方法4和方法5基本相当
多关键字比较排序:
实例7:
>>>L = [('d',2),('a',4),('b',3),('c',2)]
>>> L.sort(key=lambda x:x[1])
>>> L
>>>[('d', 2), ('c', 2), ('b', 3), ('a', 4)]
我们看到,此时排序过的L是仅仅按照第二个关键字来排的,如果我们想用第二个关键字
排过序后再用第一个关键字进行排序呢?有两种方法
实例8:
>>> L = [('d',2),('a',4),('b',3),('c',2)]
>>> L.sort(key=lambda x:(x[1],x[0]))
>>> L
>>>[('c', 2), ('d', 2), ('b', 3), ('a', 4)]
实例9:
>>> L = [('d',2),('a',4),('b',3),('c',2)]
>>> L.sort(key=operator.itemgetter(1,0))
>>> L
>>>[('c', 2), ('d', 2), ('b', 3), ('a', 4)]
为什么实例8能够工作呢?原因在于tuple是的比较从左到右之一比较的,比较完第一个,如果
相等,比较第二个
转自:http://blog.chinaunix.net/u2/71210/showart_1888878.html
补充:sort函数有三个参数例如sort(key=lambda x:x[1],reverse=True,cmp=None)
例1:>>> L = [('d',2),('a',2),('b',3),('c',2)]
         >>> L = [('d',2),('a',2),('b',3),('c',2)]
         >>> L.sort(key=lambda x:x[0]) #按0键值排序默认是升序
         >>> L
         [('a', 2), ('b', 3), ('c', 2), ('d', 2)]
        #如果要让0键值降序可以使用:
        >>> L.sort(key=lambda x:x[0],reverse=True) #true第一个字母要大写
        #使用1键值排序时候由于L中的1键值有3个2结果如下所示
        >>> L.sort(key=lambda x:x[1])
        >>> L
        [('d', 2), ('c', 2), ('a', 2), ('b', 3)]#1键值为2的tuples的0键值上的字母是降序排列的如果要按升序排列如下
        >>> L.sort(key=lambda x:(x[1],x[0]))
        >>> L
        [('a', 2), ('c', 2), ('d', 2), ('b', 3)]
例2:
    
01a = [[12, 32, 12], [11, 33, 10], [13, 31, 11], [3, 40, 12]] 
02# sorting with cmp function  
03def c(item1, item2):          
04    print 'cmp'    
05    return cmp(item1[2], item2[2])  
06# sorting with keys  
07def k(item):     
08    print 'key'     
09    return (item[2]) 
10a.sort(c)
11a.sort(key=k)
12  
13上面是list的sort方法分别使用cmp和key来排序。试下可以知道使用cmp来排序会打印六次。
14而使用 key来排序则打印四次。可见key的效率比cmp的效率高

2012年7月16日星期一

转: python random模块

 Python中的random模块用于生成随机数。下面介绍一下random模块中最常用的几个函数。

random.random

random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0

random.uniform

  random.uniform的函数原型为:random.uniform(a, b),用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成的随机数n: a <= n <= b。如果 a <b, 则 b <= n <= a。
  1. print random.uniform(10,20)  
  2. print random.uniform(20,10)  
  3. #---- 结果(不同机器上的结果不一样)  
  4. #18.7356606526  
  5. #12.5798298022  

random.randint

  random.randint()的函数原型为:random.randint(a, b),用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b
  1. print random.randint(12,20)  #生成的随机数n: 12 <= n <= 20  
  2. print random.randint(20,20)  #结果永远是20  
  3. #print random.randint(20, 10)   #该语句是错误的。下限必须小于上限。  

random.randrange

  random.randrange的函数原型为:random.randrange([start], stop[, step]),从指定范围内,按指定基数递增的集合中 获取一个随机数。如:random.randrange(10, 100, 2),结果相当于从[10, 12, 14, 16, ... 96, 98]序列中获取一个随机数。random.randrange(10, 100, 2)在结果上与 random.choice(range(10, 100, 2) 等效。

random.choice

  random.choice从序列中获取一个随机元素。其函数原型为:random.choice(sequence)。参数sequence表示一个有序类型。这里要说明 一下:sequence在python不是一种特定的类型,而是泛指一系列的类型。list, tuple, 字符串都属于sequence。有关sequence可以查看python手册数据模型这一章,也可以参考:http://www.17xie.com/read-37422.html 。下面是使用choice的一些例子:
  1. print random.choice("学习Python")   
  2. print random.choice(["JGood","is", "a","handsome", "boy"])  
  3. print random.choice(("Tuple","List", "Dict"))  

random.shuffle

  random.shuffle的函数原型为:random.shuffle(x[, random]),用于将一个列表中的元素打乱。如:
  1. p = ["Python","is", "powerful","simple", "and so on..."]  
  2. random.shuffle(p)  
  3. print p  
  4. #---- 结果(不同机器上的结果可能不一样。)  
  5. #['powerful', 'simple', 'is', 'Python', 'and so on...']  

random.sample

  random.sample的函数原型为:random.sample(sequence, k),从指定序列中随机获取指定长度的片断。sample函数不会修改原有序列。
  1. list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
  2. slice = random.sample(list, 5)  #从list中随机获取5个元素,作为一个片断返回  
  3. print slice  
  4. print list #原有序列并没有改变。  
  上面这些方法是random模块中最常用的,在Python手册中,还介绍其他的方法。感兴趣的朋友可以通过查询Python手册了解更详细的信息。

例子:
  1. import random  
  2. result = random.random()  
  3. print result   #生成0-1的随机数  
  4.   
  5. print random.uniform(10,12)  #10-12的随机数  
  6.   
  7. print random.randint(30,50)  #30-50的随机整数   
  8.   
  9. print random.randrange(10,100,2#从10开始到100结束,步长为2的序列中,随机选一个  
  10.   
  11. list = [1,2,5,6,7,8,8]  
  12. print random.choice(list)   #从序列中随机选一个  
  13.   
  14.   
  15.   
  16. random.shuffle(list)     #重新排列序列  
  17. print list  
  18.   
  19. list = [12345678910]     
  20. slice = random.sample(list, 5)   #从序列中取样  
  21. print slice     
结果:
0.782366976492
11.5582702631
42
88
7
[1, 5, 8, 6, 7, 2, 8]
[10, 2, 9, 7, 8]