-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathr2python.py
More file actions
84 lines (49 loc) · 1.48 KB
/
r2python.py
File metadata and controls
84 lines (49 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# this module are python equivalent of R functions
import numpy as np
def intersect(lst1, lst2):
return list(set(lst1) & set(lst2))
def unique(aa):
return list(set(aa))
# def sample(aa):
# change later to take multiple parameters
# return np.random.shuffle(aa)
def which(lst):
return list(np.where(lst)[0])
def rowSums(aa):
return np.sum(aa, axis=1)
def colSums(aa):
return np.sum(aa, axis=0)
def rowMeans(aa):
return np.mean(aa, axis=1)
def colMeans(aa):
return np.mean(aa, axis=0)
def rbind(aa, bb):
return np.concatenate((aa, bb), axis=0)
def cbind(aa, bb):
return np.concatenate((aa, bb), axis=1)
def cbind_array_mat(array, mat):
return cbind(np.tile(array, (len(mat), 1)), mat)
def sample(List, size=1, replace=False, prob=None):
'''
sample.int(n=n, size = s, replace = FALSE, prob = NULL)
'''
if prob is not None:
prob = prob / sum(prob)
return np.random.choice(List, size=size, replace=replace, p=prob)
def sample_int(n, size=1, replace=False, prob=None):
'''
sample.int(n=n, size = s, replace = FALSE, prob = NULL)
prob should be np.array
'''
List = range(n)
if prob is not None:
prob = prob / sum(prob)
return np.random.choice(List, size=size, replace=replace, p=prob)
def np_which_max(a):
return np.argmax(a)
def which_max(a):
return a.index(max(a))
def which_min(a):
return a.index(min(a))
def setdiff(a, b):
return list(set(a) - set(b))