Good day.
日安。
I intend to implement the summation function shown in the figure below to a data frame in Python:
我打算将下图中的求和函数实现为一个Python中的数据框:
I have tried the code below as a simple working example but I am not getting the results intended:
我已经尝试了下面的代码作为一个简单的工作示例,但我没有得到预期的结果:
D_data = [0.1, 0.22, 0.353, 4.25, 12.5];
max_state=5;
total_numerator_1 = 0
total_numerator_2 = 0
total_denominator = 0
for i, coefficient in enumerate(D_data):
for j in range(1,max_state):
total_numerator_2 += j * coefficient**2
total_numerator_1 += j * coefficient
total_denominator += I
print('lambda1:', total_numerator_1 / total_denominator)
print('lambda2:', total_numerator_2 / total_numerator_1)
For instance, the expected output for lambda1 and lambda2 are :
例如,lambda1和lambda2的预期输出为:
lambda1= ((0.1*1)+(0.22*2)+(0.353*3)+(4.25*4)+(12.5*5))/(0.1+0.22+0.353+4.25+12.5)= 4.655(Approx.)
lambda2= ((0.1^2 X 1)+(0.22^2 X 2)+(0.353^2 X 3)+(4.25^2 X 4)+(12.5^2 X 5))/(0.1+0.22+0.353+4.25+12.5)= 10.530(Approx.)
Any timely help suggestions for the solution will be hugely appreciated.
对于解决方案的任何及时帮助建议,我们都将不胜感激。
更多回答
you will get the same results, because you are dividing l2.sum() with l1.sum()
您将得到相同的结果,因为您将l2.sum()除以l1.sum()
What is d_result['D']
?
D_RESULT[‘D’]是什么?
The idea is that suppose d_result['D']=[1,2,3,4,5] then: l1=(1×1)+(2×2)+(3×3)+... and l2= (1×1^2)+(2×2^2)+(3×3^2)+...From ,this simple working example, lambda1 and lamba 2 should be different.
其思想是:设d_Result[‘D’]=[1,2,3,4,5],则:L1=(1×1)+(2×2)+(3×3)+…和L2=(1×1^2)+(2×2^2)+(3×3^2)+...在这个简单的工作示例中,lambda1和lamba 2应该是不同的。
Are you looking to do these in a pandas dataframe? is D_data
the data in your dataframe?
您是否希望在熊猫数据帧中实现这些功能?数据帧中的数据是D_DATA吗?
Also your lambda2
calculation is wrong, it should be 49.014557022326805
另外,您的lambda2计算是错误的,应该是49.014557022326805
优秀答案推荐
Given a list D
, it looks like you want to calculate l1 = (D[0] * 1 + D[1] * 2 + D[2] * 3 + ...) / (D[0] + D[1] + D[2] + ...)
, and l2 = (D[0]**2 * 1 + D[1]**2 * 2 + D[2]**2 * 3 + ...) / (D[0] + D[1] + D[2] + ...)
给定一个列表D,看起来您需要计算L1=(D[0]*1+D[1]*2+D[2]*3+...)/(D[0]+D[1]+D[2]+...)和L2=(D[0]**2*1+D[1]**2*2+D[2]**2*3+...)/(D[0]+D[1]+D[2]+...)
In pure python, you can write these operations as:
在纯Python中,您可以将这些操作编写为:
total = sum(D)
l1 = sum(d * i for i, d in enumerate(D, 1)) / total # 4.654709292314756
l2 = sum(d**2 * i for i, d in enumerate(D, 1)) / total # 49.014557022326805
If you're using pandas for your dataframes, you can use pandas's sum
to outsource the calculation to the backend:
如果你的数据帧使用熊猫,你可以使用熊猫的总和将计算外包给后端:
import numpy as np
import pandas as pd
D_df = pd.DataFrame(D, columns=["data"])
multipliers = (np.arange(len(D_df)) + 1).reshape((-1, 1))
total = D_df.sum(axis=0)
lambda1 = (D_df * multipliers).sum(axis=0) / total
lambda2 = (D_df**2 * multipliers).sum(axis=0) / total
multipliers
is simply a column-vector (array of shape (*, 1)
) containing the numbers [1, 2, 3, ..., len(D_df)]
乘数只是一个包含数字[1,2,3,...,len(D_Df)]的列矢量(形状数组(*,1))
You're trying to print lambda1 instead of lambda2
您正在尝试打印lambda1而不是lambda2
更多回答
Thank you for the swift response @Abinash. The results obtained for lambda1 and lambda2 were unexpectedly equal in terms of value. That was my challenge.
感谢您的快速回复@Abinash。对lambda1和lambda2获得的结果在价值上出人意料地相等。这就是我的挑战。
我是一名优秀的程序员,十分优秀!