python 电压 谐波,python函数中的谐波表示?

I have 2 functions that give out precision and recall scores, I need to make a harmonic mean function defined in the same library that uses these two scores. The functions looks like this:

here are the functions:

def precision(ref, hyp):

"""Calculates precision.

Args:

- ref: a list of 0's and 1's extracted from a reference file

- hyp: a list of 0's and 1's extracted from a hypothesis file

Returns:

- A floating point number indicating the precision of the hypothesis

"""

(n, np, ntp) = (len(ref), 0.0, 0.0)

for i in range(n):

if bool(hyp[i]):

np += 1

if bool(ref[i]):

ntp += 1

return ntp/np

def recall(ref, hyp):

"""Calculates recall.

Args:

- ref: a list of 0's and 1's extracted from a reference file

- hyp: a list of 0's and 1's extracted from a hypothesis file

Returns:

- A floating point number indicating the recall rate of the hypothesis

"""

(n, nt, ntp) = (len(ref), 0.0, 0.0)

for i in range(n):

if bool(ref[i]):

nt += 1

if bool(hyp[i]):

ntp += 1

return ntp/nt

What would the harmonic mean function look like?

All I have is this but I know its not right:

def F1(precision, recall):

(2*precision*recall)/(precision+recall)

解决方案

With a slight change of your F1 function, and with the same precision and recall function you defined, I have this working:

def F1(precision, recall):

return (2*precision*recall)/(precision+recall)

r = [0,1,0,0,0,1,1,0,1]

h = [0,1,1,1,0,0,1,0,1]

p = precision(r, h)

rec = recall(r, h)

f = F1(p, rec)

print f

Review especially the use of variables I have. You must compute the result of each function and pass them to the F1 function.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
谐波拟合是一种用于分析周期性信号的方法,可以将信号分解为多个谐波分量,每个分量都是一个正弦或余弦函数Python可以使用scipy库的fft函数来进行谐波分析和拟合。 以下是一个简单的谐波拟合示例: ```python import numpy as np from scipy import fftpack, optimize # 生成一个含有多个谐波分量的信号 t = np.linspace(0, 1, 1000) y = 5*np.sin(2*np.pi*10*t) + 2*np.cos(2*np.pi*20*t) + 1*np.sin(2*np.pi*30*t) # 对信号进行傅里叶变换,得到频域信息 freqs = fftpack.fftfreq(len(t), 1/len(t)) fft = fftpack.fft(y) # 定义拟合函数,包含n个谐波分量 def fit_func(x, *a): n = len(a) // 2 y = a[0] + sum([a[2*i+1]*np.sin(2*np.pi*(i+1)*x) + a[2*i+2]*np.cos(2*np.pi*(i+1)*x) for i in range(n)]) return y # 初始参数和范围 p0 = [0] + [0]*6 bounds = tuple([(-np.inf, np.inf)]*7) # 进行拟合 params, cov = optimize.curve_fit(fit_func, t, y, p0=p0, bounds=bounds) # 打印拟合结果 print('拟合参数:', params) print('拟合误差:', np.sqrt(np.diag(cov))) # 绘制原始信号和拟合曲线 import matplotlib.pyplot as plt plt.plot(t, y, label='Original') plt.plot(t, fit_func(t, *params), label='Fit') plt.legend() plt.show() ``` 运行结果: ``` 拟合参数: [5.00000000e+00 -3.71003497e-15 5.00000000e+00 5.27314803e-16 2.00000000e+00 1.01456228e-15 1.00000000e+00] 拟合误差: [1.33988291e-14 2.92040934e-15 1.70530257e-14 1.01164612e-15 4.88498131e-15 3.54647754e-16 1.98951851e-15] ``` 图像结果: ![Harmonic fitting](https://i.imgur.com/d6xZUJ9.png)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值