我知道计算熵的公式:
H(Y) = - ? (p(yj) * log2(p(yj)))
Run Code Online (Sandbox Code Playgroud)
在单词中,选择一个属性,并为每个值检查目标属性值...所以p(yj)是节点N的模式在类别yj中的一部分 - 一个用于目标值中的true,一个用于false.
但我有一个数据集,其中目标属性是价格,因此范围.如何计算这种数据集的熵?
(简称:http://decisiontrees.net/decision-trees-tutorial/tutorial-5-exercise-2/)
我有一个pandas DataFrame,其中包含一组列的重复值.例如:
df = pd.DataFrame({'Column1': {0: 1, 1: 2, 2: 3}, 'Column2': {0: 'ABC', 1: 'XYZ', 2: 'ABC'}, 'Column3': {0: 'DEF', 1: 'DEF', 2: 'DEF'}, 'Column4': {0: 10, 1: 40, 2: 10})
In [2]: df
Out[2]:
Column1 Column2 Column3 Column4 is_duplicated dup_index
0 1 ABC DEF 10 False 0
1 2 XYZ DEF 40 False 1
2 3 ABC DEF 10 True 0
Run Code Online (Sandbox Code Playgroud)
第(1)行和第(3)行相同.基本上,Row(3)是Row(1)的副本.
我正在寻找以下输出:
Is_Duplicate
,包含行是否重复[可以通过在数据帧列(Column2,Column3和Column4)上使用"重复"方法来完成]
Dup_Index
重复行的原始索引.
In [3]: df
Out[3]:
Column1 Column2 Column3 Column4 Is_Duplicate Dup_Index
0 …
Run Code Online (Sandbox Code Playgroud) 我有一个带有单行的pandas DataFrame:
10 20 30 70
data1: 2.3 5 6 7
Run Code Online (Sandbox Code Playgroud)
我想重新索引框架,以便列值(10,20,30,70)成为索引值,数据成为列:
data1:
10 2.3
20 5.0
30 6.0
70 7.0
Run Code Online (Sandbox Code Playgroud)
我该如何实现这一目标?
我想知道将列表理解转换为Javascript的最佳方法是什么(从Python).有什么能让这个可读而不是一团糟吗?
non_zero_in_square = [ grid[row][col]
for row in range(start_row, start_row+3)
for col in range(start_col, start_col+3)
if grid[row][col] is not 0
]
Run Code Online (Sandbox Code Playgroud)
这是列表理解的一个很好的例子,因为它有多个for
s和a if
.
我要补充的是,range
位覆盖这里(我不能没有range
).
假设我有一个函数层次结构,我希望能够访问(不更改!)父项范围.这是一个说明性的例子.
def f():
a = 2
b = 1
def g():
b = 2
c = 1
print globals() #contains a=1 and d=4
print locals() #contains b=2 and c=1, but no a
print dict(globals(), **locals()) #contains a=1, d=4 (from the globals), b=2 and c=1 (from g)
# I want a=2 and b=1 (from f), d=4 (from globals) and no c
g()
a = 1
d = 4
f()
Run Code Online (Sandbox Code Playgroud)
我可以f
从内部访问范围g
吗?
更新到原始帖子:一位同事指出我做错了什么.我会在帖子的底部给出解释,因为它可能对其他人有帮助.
我试图基本了解python程序的网络性能限制,并遇到异常现象.代码片段
while 1:
sock.sendto("a",target)
Run Code Online (Sandbox Code Playgroud)
将UDP数据包发送到目标计算机,速度与主机发送的速度一样快.我测量的发送速率仅为每秒4000个数据包,或每个数据包250个.这似乎很慢,即使对于像python这样的解释语言(该程序运行在2 GHz AMD opteron,Linux,python版本2.6.6).我在python for TCP中看到了更好的性能,所以我觉得这有点奇怪.
如果我在后台运行并运行top,我发现python只使用了25%的cpu,这表明python可能会人为地延迟UDP数据包的传输.
还有其他人经历过类似的事吗?有谁知道python是否确实限制了数据包传输速率,是否有办法将其关闭?
顺便说一句,类似的C++程序每秒可以发送超过200,000个数据包,因此它不是平台或操作系统的固有限制.
所以,事实证明我犯了一个愚蠢的新手错误.我忽略了明确地调用gethostbyname.因此,sendto命令中的目标地址包含符号名称.每次发送数据包时都会触发名称解析.解决此问题后,我测量的最大发送速率约为120,000 p/s.好多了.
我从这里下载了适用于Windows的"phantomjs-1.7.0-windows.zip" .
我甚至在环境变量中设置了解压缩文件夹的路径.但是当我尝试输入命令提示符phantomjs --version
中的任何命令时,我得到"解析错误" phantomjs.exe
.(我的Windows是64位.)
为什么会抛出错误?
在Python中进行字符串格式化时,我注意到%s
转换也是数字到字符串.
>>> a = 1
>>> b = 1.1
>>> c = 'hello'
>>> print 'Integer: %s; Float: %s; String: %s' % (a, b, c)
Integer: 1; Float: 1.1; String: hello
Run Code Online (Sandbox Code Playgroud)
我不知道其他变量类型,但这样使用是否安全%s
?
它肯定比每次总是指定类型更快.
我在熊猫中有两个系列.
系列1:
id count_1
1 3
3 19
4 15
5 5
6 2
Run Code Online (Sandbox Code Playgroud)
和系列2:
id count_2
1 3
3 1
4 1
5 2
6 1
Run Code Online (Sandbox Code Playgroud)
如何将ID组合在一起以形成下面的表格?
id count_1 count_2
1 3 3
3 19 1
4 15 1
5 5 2
6 2 1
Run Code Online (Sandbox Code Playgroud)