问题描述
我正在尝试计算熊猫序列的熵。具体来说,我将Direction
中的字符串按顺序分组。具体来说,使用此功能:
diff_dir = df.iloc[0:,1].ne(df.iloc[0:,1].shift()).cumsum()
将返回Direction
中相同的字符串计数,直到更改为止。因此,对于相同的Direction
字符串的每个序列,我想计算X,Y
的熵。
使用代码对相同字符串进行排序:
0 1
1 1
2 1
3 1
4 1
5 2
6 2
7 2
8 3
9 3
该代码曾经可以使用,但是现在返回错误。我不确定这是否是升级之后。
import pandas as pd
import numpy as np
def ApEn(U,m = 2,r = 0.2):
'''
Approximate Entropy
Quantify the amount of regularity over time-series data.
Input parameters:
U = Time series
m = Length of compared run of data (subseries length)
r = Filtering level (tolerance). A positive number
'''
def _maxdist(x_i,x_j):
return max([abs(ua - va) for ua,va in zip(x_i,x_j)])
def _phi(m):
x = [U.tolist()[i:i + m] for i in range(N - m + 1)]
C = [len([1 for x_j in x if _maxdist(x_i,x_j) <= r]) / (N - m + 1.0) for x_i in x]
return (N - m + 1.0)**(-1) * sum(np.log(C))
N = len(U)
return abs(_phi(m + 1) - _phi(m))
def Entropy(df):
'''
Calculate entropy for individual direction
'''
df = df[['Time','Direction','X','Y']]
diff_dir = df.iloc[0:,1].shift()).cumsum()
# Calculate ApEn grouped by direction.
df['ApEn_X'] = df.groupby(diff_dir)['X'].transform(ApEn)
df['ApEn_Y'] = df.groupby(diff_dir)['Y'].transform(ApEn)
return df
df = pd.DataFrame(np.random.randint(0,50,size = (10,2)),columns=list('XY'))
df['Time'] = range(1,len(df) + 1)
direction = ['Left','Left','Right','Left']
df['Direction'] = direction
# Calculate defensive regularity
entropy = Entropy(df)
错误:
return (N - m + 1.0)**(-1) * sum(np.log(C))
ZeroDivisionError: 0.0 cannot be raised to a negative power
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)