我只是得到了Spark的悬念,我有需要映射到的函数rdd,但是使用了一个全局字典:
from pyspark import SparkContext
sc = SparkContext('local[*]', 'pyspark')
my_dict = {"a": 1, "b": 2, "c": 3, "d": 4} # at no point will be modified
my_list = ["a", "d", "c", "b"]
def my_func(letter):
return my_dict[letter]
my_list_rdd = sc.parallelize(my_list)
result = my_list_rdd.map(lambda x: my_func(x)).collect()
print result
Run Code Online (Sandbox Code Playgroud)
以上给出了预期的结果; 但是,我真的不确定我对全局变量的使用my_dict.似乎每个分区都会创建一个字典副本.它只是感觉不对..
看起来广播是我正在寻找的.但是,当我尝试使用它时:
my_dict_bc = sc.broadcast(my_dict)
def my_func(letter):
return my_dict_bc[letter]
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
TypeError: 'Broadcast' object has no attribute '__getitem__
Run Code Online (Sandbox Code Playgroud)
这似乎意味着我不能播放字典.
我的问题:如果我有一个使用全局字典的函数,需要将其映射到rdd,那么正确的方法是什么?
我的例子很简单,但在现实中my_dict,并my_list要大得多,而且my_func …
我正在寻找Python中的测试:
> survivors <- matrix(c(1781,1443,135,47), ncol=2)
> colnames(survivors) <- c('survived','died')
> rownames(survivors) <- c('no seat belt','seat belt')
> survivors
survived died
no seat belt 1781 135
seat belt 1443 47
> prop.test(survivors)
2-sample test for equality of proportions with continuity correction
data: survivors
X-squared = 24.3328, df = 1, p-value = 8.105e-07
alternative hypothesis: two.sided
95 percent confidence interval:
-0.05400606 -0.02382527
sample estimates:
prop 1 prop 2
0.9295407 0.9684564
Run Code Online (Sandbox Code Playgroud)
我最感兴趣的是p-value计算.
这里的例子就是这里的例子
我想知道是否有更好的方法来测试两个变量是否是协整的,而不是以下方法:
import numpy as np
import statsmodels.api as sm
import statsmodels.tsa.stattools as ts
y = np.random.normal(0,1, 250)
x = np.random.normal(0,1, 250)
def cointegration_test(y, x):
# Step 1: regress on variable on the other
ols_result = sm.OLS(y, x).fit()
# Step 2: obtain the residual (ols_resuld.resid)
# Step 3: apply Augmented Dickey-Fuller test to see whether
# the residual is unit root
return ts.adfuller(ols_result.resid)
Run Code Online (Sandbox Code Playgroud)
以上方法有效; 但是,效率不高.当我跑步时sm.OLS,会计算很多东西,而不仅仅是残差,这当然会增加运行时间.我当然可以编写自己的代码来计算残差,但我认为这也不会非常有效.
我正在寻找一种直接测试协整的内置测试.我在想Pandas,但似乎找不到任何东西.或者也许有一个聪明的人来测试协整而不运行回归或一些有效的方法.
我必须进行大量的协整测试,并且很好地改进我当前的方法.
我使用的是Spyder附带的python(x,y).昨天,Spyder坠毁我无法弄明白如何解决它.我卸载python(x,y)并重新安装,仍然是同样的问题.
如果我尝试打开Spyder,我会收到以下消息:
Spyder crashed during last session
If Spyder does not start at all and before submitting a bug report, please try to reset setting to defaults by running Spyder with the command line option '--reset:
python spyder --reset
Run Code Online (Sandbox Code Playgroud)
当然,我试着做上面的事情,但似乎我的Spyder似乎并不在我的道路上.当我试图放
python spyder --reset
Run Code Online (Sandbox Code Playgroud)
在我的命令提示符窗口中,我收到此错误消息:
python: can't open file 'spyder': [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)
我做了一些尝试将spyder添加到路径的尝试,但它们都失败了.我应该如何添加Spyder到路径?
我在32位系统上使用Windows Vista.
我的Spyder Crash报告看起来像这样:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\spyderlib\spyder.py", line 1547, in main
mainwindow = run_spyder(app, options)
File "C:\Python27\lib\site-packages\spyderlib\spyder.py", line 1472, …Run Code Online (Sandbox Code Playgroud) 我一直试图弄清楚如何在地图中插入一个值的向量.例如:
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main()
{
map <int, vector<int> > mymap;
mymap.insert(pair<int, vector<int> > (10, #put something here#));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不知道使用什么语法插入向量的值.我试过了{1,2},但那次失败了.我应该使用什么语法?
如果我提前声明一个矢量并给它起一个名字,那么一切都有效,但我不想这样做,因为我想拥有一个有很多矢量的地图.
先感谢您
我需要找到1D中最小的第n个元素numpy.array.
例如:
a = np.array([90,10,30,40,80,70,20,50,60,0])
Run Code Online (Sandbox Code Playgroud)
我想得到第五个最小的元素,所以我想要的输出是40.
我目前的解决方案是:
result = np.max(np.partition(a, 5)[:5])
Run Code Online (Sandbox Code Playgroud)
然而,找到5个最小的元素然后拿出最大的元素对我来说似乎不太笨拙.有没有更好的方法呢?我错过了一个能实现目标的功能吗?
有一些问题与这个相似的标题,但我没有看到任何回答我的问题.
编辑:
我本来应该提到它,但性能对我来说非常重要; 因此,heapq虽然好的解决方案对我不起作用.
import numpy as np
import heapq
def find_nth_smallest_old_way(a, n):
return np.max(np.partition(a, n)[:n])
# Solution suggested by Jaime and HYRY
def find_nth_smallest_proper_way(a, n):
return np.partition(a, n-1)[n-1]
def find_nth_smallest_heapq(a, n):
return heapq.nsmallest(n, a)[-1]
#
n_iterations = 10000
a = np.arange(1000)
np.random.shuffle(a)
t1 = timeit('find_nth_smallest_old_way(a, 100)', 'from __main__ import find_nth_smallest_old_way, a', number = n_iterations)
print 'time taken using partition old_way: {}'.format(t1) …Run Code Online (Sandbox Code Playgroud) 我写了一个简单的遗传算法,可以解决5个城市的旅行商问题.我想看看它如何处理更多城市的问题,如10,25,50,100,但我找不到问题的样本日期来尝试.基本上,我正在寻找城市之间距离的二维列表或矩阵.如果有解决方案会很好.我应该在哪里看?
先感谢您
例如,如果我这样做:
cdef np.ndarray[np.int64_t, ndim=1] my_array
Run Code Online (Sandbox Code Playgroud)
我的my_array存放在哪里?我认为,因为我没有告诉cython存储在堆上它会存储在堆栈上,但是在进行下面的实验之后,它似乎存储在堆上,或者以某种方式有效地管理内存.如何管理内存my_array?也许我错过了一些明显的东西,但我找不到任何文件.
import numpy as np
cimport cython
cimport numpy as np
from libc.stdlib cimport malloc, free
def big_sum():
# freezes up:
# "a" is created on the stack
# space on the stack is limited, so it runs out
cdef int a[10000000]
for i in range(10000000):
a[i] = i
cdef int my_sum
my_sum = 0
for i in range(10000000):
my_sum += a[i]
return my_sum
def big_sum_malloc():
# runs fine:
# "a" …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用cython调试器来设置一个断点:
这是我的代码:
cython_file.pyx
cimport cython
def big_sum():
cdef int a[10000]
for i in range(10000):
a[i] = i
# <==================== I want to put a break here
cdef int my_sum
my_sum = 0
for i in range(1000):
my_sum += a[i]
return my_sum
Run Code Online (Sandbox Code Playgroud)
python_file.py
from cython_file import big_sum
result = big_sum()
print result
Run Code Online (Sandbox Code Playgroud)
setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
cmdclass = {'build_ext': build_ext},
ext_modules = [Extension("cython_file",
["cython_file.pyx"], pyrex_gdb=True,
extra_compile_args=["-g"], extra_link_args=["-g"])]
)
Run Code Online (Sandbox Code Playgroud)
我按照这个指南: …
我试图断言两个词典几乎是平等的,但我似乎无法做到这一点.
这是一个例子:
>>> import nose.tools as nt
>>> nt.assert_dict_equal({'a' : 12.4}, {'a' : 5.6 + 6.8})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/unittest/case.py", line 838, in assertDictEqual
self.fail(self._formatMessage(msg, standardMsg))
File "/usr/lib/python2.7/unittest/case.py", line 413, in fail
raise self.failureException(msg)
AssertionError: {'a': 12.4} != {'a': 12.399999999999999}
- {'a': 12.4}
+ {'a': 12.399999999999999}
Run Code Online (Sandbox Code Playgroud)
我希望这会通过,就像那样:
>>> nt.assert_almost_equal(12.4, 5.6 + 6.8)
Run Code Online (Sandbox Code Playgroud)
我希望我遗漏一些简单的东西nt.assert_almost_dict_equal,或者也许我可以传递的参数nt.assert_dict_equal指定浮点数应该有多近,但我找不到任何东西.
当然,我可以循环遍历字典并使用nt.assert_almost_equal单独比较值; 但是,在我的情况下,字典更复杂,所以我希望避免这样.
断言两个字典几乎相等的最佳方法是什么?