我有一个3GB的CSV文件,我尝试用python读取,我需要明智的中间列.
from numpy import *
def data():
return genfromtxt('All.csv',delimiter=',')
data = data() # This is where it fails already.
med = zeros(len(data[0]))
data = data.T
for i in xrange(len(data)):
m = median(data[i])
med[i] = 1.0/float(m)
print med
Run Code Online (Sandbox Code Playgroud)
我得到的错误是这样的:
Python(1545) malloc: *** mmap(size=16777216) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Traceback (most recent call last):
File "Normalize.py", line 40, in <module>
data = data()
File "Normalize.py", line 39, in data
return genfromtxt('All.csv',delimiter=',') …Run Code Online (Sandbox Code Playgroud) 我从未见过这种复杂性的符号:Õ(n).
它出现在随机算法学习的背景下.
有人知道这个符号吗?你不能完全google这个......
编辑:已解决
我想人们已经在下面指出了正确的答案.在我的例子中,Õ()用于隐藏树的指数增长.
我有以下代码,适当地转换为cython:
from numpy import *
## returns winning players or [] if undecided.
def score(board):
scores = []
checked = zeros(board.shape)
for i in xrange(len(board)):
for j in xrange(len(board)):
if checked[i,j] == 0 and board[i,j] !=0:
... do stuf
Run Code Online (Sandbox Code Playgroud)
我尝试转换为cython:
import numpy as np
cimport numpy as np
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
## returns winning players or [] if undecided.
def score(np.ndarray[int, ndim=2] board):
scores = []
cdef np.ndarray[int, ndim = 2 ] checked
checked = np.zeros(board.shape)
for i in xrange(len(board)): …Run Code Online (Sandbox Code Playgroud) 我正在尝试构造一个python类型的矩阵int,一个64位有符号整数.
cdef matrix33():
return np.zeros((3,3),dtype=int)
cdef do_stuf(np.ndarray[int, ndim=2] matrix):
...
return some_value
def start():
print do_stuf(matrix33())
Run Code Online (Sandbox Code Playgroud)
它编译正确,但是当我运行它时,我不断收到此错误:
ValueError: Buffer dtype mismatch, expected 'int' but got 'long'
Run Code Online (Sandbox Code Playgroud)
我无法使用python long,但我不知道如何正确转换为64 int.
UPDATE
好.我很确定我正确地使用了Cython.我写的代码是在捕获go/atari go的游戏中进行minmax搜索.
到目前为止,最常被称为的功能是:
cdef isThere_greedy_move(np.ndarray[np.int64_t, ndim=2]board, int player):
cdef int i, j
for i in xrange(len(board)):
for j in xrange(len(board)):
if board[i,j] == 0:
board[i,j] = player
if player in score(board):
board[i,j] = 0
return True
board[i,j] = 0
return False
# main function of the scoring system.
# returns …Run Code Online (Sandbox Code Playgroud) 我有这个任意函数,我需要用不同的变量多次调用.顺便说一句,这是SWI-Prolog
perform(V1,V2,V3,Function,Result):-
%
% do little stuf.
%
Function(Arg1,Arg2,Result).
Run Code Online (Sandbox Code Playgroud)
这给出了语法错误.
但是将函数作为变量传递而不添加参数可以正常工作,如下面的代码所示:
perform(Function):-
Function.
sayHello:-
write('hello').
:-perform(sayHello).
Run Code Online (Sandbox Code Playgroud)
那么如何为变量函数添加参数呢?
我正在尝试在OCaml中构建一个trie:
type ('a, 'b) trie = Nil | Cons of 'a * 'b option * ('a, 'b) trie list;;
(* find place to insert key in a list of tries *)
let rec findInsert key x =
match x with
[] -> Nil
| x::xs -> let Cons(k, _, _) = x in
if key = k then x else findInsert key xs;;
(* inser pair in a trie *)
let rec insert ( key, value ) trie …Run Code Online (Sandbox Code Playgroud) 为什么以下代码给出"无"?我该如何解决这个问题?
def f1(list1):
f2(list1.append(2))
def f2(list1):
print(list1)
f1([1])
Run Code Online (Sandbox Code Playgroud)
什么也行不通:
def f1(list1):
arg1 = list1.append(2)
f2(arg1)
Run Code Online (Sandbox Code Playgroud) 我正试图在狮子座上安装cython,但这就是我得到的:
$ export CC=gcc-4.2
$ gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)
$ python setup.py install
error: command 'gcc-4.2' failed with exit status 255
Run Code Online (Sandbox Code Playgroud)
我不确定llvm-gcc是对的,我安装了Xcode4.1但是它仍然没有用.
谁知道如何解决这个问题?