scipy.stats.circmean 方法比较适用于中 /小规模高计算精度要求。大数据允许一定小偏差可以用 circular mean = main angle of 1st harmonic of density kernel 的方法搞定:
1. 求密度分布:
d, _ = histogram(t, bins=1000, range=(0,24)) # 24 小时制,划 1000 个格子,精度为 24/1000 小时
2. 求 1st harmonics:
w = np.exp(-2j*np.pi*np.arange(1000) / 1000)
F1 = w.dot(d)
3. 求主幅角 (0, 2pi)
angle = np.angle(F1.conj())
if angle < 0:
....angle += 2*np.pi
4. 映射回平均值
t_mean = 24 * angle / (2 * np.pi) # 起始点是 0
这样空间复杂度小,算得快,数据量大的话这个方法好一些。
抱歉,是向量集 (cos(x), sin(x)), arctan2(mean_sin, mean_cos) 这样调用。写差了。
流程是先投影数据到角度 X->x in (0,2pi),然后求向量集 (sin(x),cos(x)) 的平均,然后用 arctan2 函数返回角度,根据周期再逆投影回数据。
@
ideacco 那个东西就叫 circular mean,具体看喂鸡百科。想自己写用 arctan2 函数。
from scipy.stats import circmean
circmean(t, high=12,low=0)
或者 24 小时制:
circmean(t, high=24,low=0)
用 get(),如果列有名字则用名字,否则用序号,get 如果没有则返回 None,方便处理。
In [2]: import pandas as pd
In [3]: df = pd.DataFrame(np.random.randn(8, 4))
In [4]: df
Out[4]:
0 1 2 3
0 -0.727670 -0.182557 -0.957270 -0.153352
1 -0.340649 -0.313155 -1.219515 0.082472
2 0.023527 0.496896 0.443117 -0.391405
3 -0.522745 0.879736 -1.358356 0.177883
4 -0.314936 -1.795936 -1.510872 1.039757
5 0.000243 -0.826999 -0.365514 -0.907249
6 0.058694 -0.521912 -0.863121 0.842308
7 0.846951 0.325337 -0.821745 0.111492
In [5]: df.get(0)
Out[5]:
0 -0.727670
1 -0.340649
2 0.023527
3 -0.522745
4 -0.314936
5 0.000243
6 0.058694
7 0.846951
Name: 0, dtype: float64
In [6]: df.get(1)
Out[6]:
0 -0.182557
1 -0.313155
2 0.496896
3 0.879736
4 -1.795936
5 -0.826999
6 -0.521912
7 0.325337
Name: 1, dtype: float64
In [7]: df.get(3)
Out[7]:
0 -0.153352
1 0.082472
2 -0.391405
3 0.177883
4 1.039757
5 -0.907249
6 0.842308
7 0.111492
Name: 3, dtype: float64
In [8]: df.get(4)
In [9]:
或者列有名字:
In [10]: df = pd.DataFrame(np.random.randn(8, 4), columns=['A', 'B', 'C', 'D'])
In [11]: df
Out[11]:
A B C D
0 -1.521750 -0.704144 -0.565343 -0.389537
1 -0.634391 0.672338 0.857965 0.294724
2 -0.764034 0.907585 -1.454368 -0.637835
3 -1.218633 -1.473434 1.441891 1.554465
4 -1.100643 -2.303968 -1.788275 -0.382192
5 1.476041 -0.735864 -0.359389 0.896467
6 1.662332 -0.944238 0.308855 -0.013283
7 1.357332 0.529256 1.169877 0.745932
In [12]: df.get('E')
In [13]: df.get('B')
Out[13]:
0 -0.704144
1 0.672338
2 0.907585
3 -1.473434
4 -2.303968
5 -0.735864
6 -0.944238
7 0.529256
Name: B, dtype: float64
实在不行还可以用 try except 吧。
先看独占和 R 星。没有独占和 R 星需求的话,建议 xbox x,个人感觉效果好,手柄爽一些。其实非常不推荐主机。买主机之后就发现游戏打折力度不高,FPS 游戏不买键鼠转换会打到想死,据不可靠评测,好像体验也不是很好,不知道 xbox 说官方出了会怎么样。希望楼主抑制住了冲动没入坑……游戏还是买个好点的电脑吧,PC 端的打折力度大,活动多,键鼠操作方便,想加手柄兼容性也高。
from scipy.special import loggamma
import numpy as np
def p(n, m):
return np.exp(loggamma(n+1)-loggamma(m+1))
@
iPhonePKAndroid 全排列:
from math import gamma
def p(n,m):
return gamma(n)/gamma(m)
@
cyy564 我在 #2 已经说了,这个需要先排序。pandas 可以无视顺序。所以数据量小考虑直接 python sorted + itertools.groupby,数据量大一些考虑 pandas.DataFrame.groupby,如果超超超大就考虑 #1 的办法。
Sorry,
ret = {}
for i in l:
....if not ret.get(i['type']):
........ret[i['type']] = 0
...ret[i['type']] +=1
@
cyy564 百分比很好统计:
ret = {}
for i in l:
....if not ret.get(i['type']):
........ret[i['type']] = 0
...ret.get(i['type']) +=1
基本上在不知道 type 有多少的情况下也能轻松统计
from itertools import groupby
[ list(g) for c, g in groupby(lst, key=(lambda x : x.get('type'))) ]
数据少的话:
lst = sorted(l, key=(lambda x : x.get('type')))
ret = [[]]
for prv, nxt in zip(lst[:-1], lst[1:]):
....tmp = ret[-1]
....tmp.append(prv)
....if prv['type']!=nxt['type']:
........ret.append([])
tmp = ret[-1]
tmp.append(t[-1])
然后取 ret 里最多的,或者直接用 groupby
[ list(g) for c, g in groupby(lst, key=(lambda x : x.get('type'))) ]
但是都需要排序。
或者用 pandas:
import pandas as pd
l= [{'name': 'aa', 'type': '游戏'},
{'name': 'cc', 'type': '学习'},
{'name': 'bb', 'type': '游戏'}] # 可以不用考虑顺序
list(pd.DataFrame(l).groupby('type')) 可以搞定,输出是 n 个 categories 的 tuple 的 list
[(分组名 1,分组 1 数据的 dataframe),(分组名 2,分组 2 数据的 dataframe)...],数据大小可以用 dataframe 的 shape 来确定。
In [40]: list(pd.DataFrame(l).groupby('type'))
Out[40]:
[('学习', name type
1 cc 学习), ('游戏', name type
0 aa 游戏
2 bb 游戏)]
In [41]: p=list(pd.DataFrame(l).groupby('type'))[1][1]
In [42]: p.shape
Out[42]: (2, 2)
In [43]: p
Out[43]:
name type
0 aa 游戏
2 bb 游戏
对一定量的数据,pandas 就可以有很高的处理效率了,如果数据量再大,考虑上 #1 的方法吧。
直接用 * 是矩阵乘法吗……别的不知道,numpy 下是批量相乘吧……
numpy 里 x_cpu.dot(x_cpu) 才是矩阵乘法。
我发现很多网站因为有 google 的 js,字体等一系列东西会有一个非常非常长的加载时间……