例如,如果我这样做:
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) 我试图了解何时捕获MemoryErrorPython 有意义,我有两个场景:
场景1:成功捕获MemoryError.
import numpy as np
try:
a = np.ones(100000000000)
except MemoryError:
print 'got memory error, plan B'
a = np.ones(10) # this gets created
Run Code Online (Sandbox Code Playgroud)
场景2:我的程序冻结了
silly = []
c = 0
try:
while True:
silly.append((str(c))) # just increasing the list
c += 1
if c % 1000000 == 0:
print 'counter : {}'.format(c)
except MemoryError:
print 'oops' # never get here
silly.append('silly')
Run Code Online (Sandbox Code Playgroud)
我的猜测是在第一种情况下,python"知道"需要分配多少内存,从而引发MemoryError异常.虽然在第二种情况下,python并不"知道"我有多大意图silly.但是,list是一个动态数组 ; 因此,python应该知道将这个数组扩展一定量会导致a MemoryError,为什么异常没有提出呢?
我看过这个 …
我正在学习如何在Python中嵌入Rust函数,如果我的输入是ints而不是list ,一切正常.
如果我的lib.rs文件是:
#[no_mangle]
pub extern fn my_func(x: i32, y: i32) -> i32 {
return x + y;
}
Run Code Online (Sandbox Code Playgroud)
我可以使用如下:
In [1]: from ctypes import cdll
In [2]: lib = cdll.LoadLibrary("/home/user/RustStuff/embed/target/release/libembed.so")
In [3]: lib.my_func(5,6)
Out[3]: 11
Run Code Online (Sandbox Code Playgroud)
但是,如果我将我lib.rs改为:
#[no_mangle]
pub extern fn my_func(my_vec: Vec<i32>) -> i32 {
let mut my_sum = 0;
for i in my_vec {
my_sum += i;
}
return my_sum;
}
Run Code Online (Sandbox Code Playgroud)
我不能再在Python中使用它(这编译得很好):
In [1]: from ctypes import cdll
In [2]: lib = …Run Code Online (Sandbox Code Playgroud) 我有纬度和经度的数据,我需要计算包含位置的两个数组之间的距离矩阵.我用这本获得给定的纬度和经度两个位置之间的距离.
这是我的代码示例:
import numpy as np
import math
def get_distances(locs_1, locs_2):
n_rows_1 = locs_1.shape[0]
n_rows_2 = locs_2.shape[0]
dists = np.empty((n_rows_1, n_rows_2))
# The loops here are inefficient
for i in xrange(n_rows_1):
for j in xrange(n_rows_2):
dists[i, j] = get_distance_from_lat_long(locs_1[i], locs_2[j])
return dists
def get_distance_from_lat_long(loc_1, loc_2):
earth_radius = 3958.75
lat_dif = math.radians(loc_1[0] - loc_2[0])
long_dif = math.radians(loc_1[1] - loc_2[1])
sin_d_lat = math.sin(lat_dif / 2)
sin_d_long = math.sin(long_dif / 2)
step_1 = (sin_d_lat ** 2) + (sin_d_long ** 2) * …Run Code Online (Sandbox Code Playgroud) 我要寻找一个量化的方式来索引numpy.array的numpy.array索引.
例如:
import numpy as np
a = np.array([[0,3,4],
[5,6,0],
[0,1,9]])
inds = np.array([[0,1],
[1,2],
[0,2]])
Run Code Online (Sandbox Code Playgroud)
我想构建一个新数组,使得该数组中的每一行(i)都是数组的行(i)a,由数组inds(i)的行索引.我想要的输出是:
array([[ 0., 3.], # a[0][:,[0,1]]
[ 6., 0.], # a[1][:,[1,2]]
[ 0., 9.]]) # a[2][:,[0,2]]
Run Code Online (Sandbox Code Playgroud)
我可以用循环实现这个目的:
def loop_way(my_array, my_indices):
new_array = np.empty(my_indices.shape)
for i in xrange(len(my_indices)):
new_array[i, :] = my_array[i][:, my_indices[i]]
return new_array
Run Code Online (Sandbox Code Playgroud)
但我正在寻找一种纯粹的矢量化解决方案.
我是一个使用CATCH的新手,我想知道如何测试两个std::vectors是否相等.
我非常天真的尝试是这样的:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <vector>
TEST_CASE( "are vectors equal", "vectors")
{
std::vector<int> vec_1 = {1,2,3};
std::vector<int> vec_2 = {1,2,3};
REQUIRE (vec_1.size() == vec_2.size());
for (int i = 0; i < vec_1.size(); ++i)
REQUIRE (vec_1[i] == vec_2[i]);
}
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?像魔术一样的东西REQUIRE_VECTOR_EQUAL?
另外,我的上述解决方案是,如果一个数组包含双精度数则传递{1.0, 2.0, 3.0}; 如果因此而认为两个向量不相等就没问题.
我试图计算每行显示的数字np.array,例如:
import numpy as np
my_array = np.array([[1, 2, 0, 1, 1, 1],
[1, 2, 0, 1, 1, 1], # duplicate of row 0
[9, 7, 5, 3, 2, 1],
[1, 1, 1, 0, 0, 0],
[1, 2, 0, 1, 1, 1], # duplicate of row 0
[1, 1, 1, 1, 1, 0]])
Run Code Online (Sandbox Code Playgroud)
行[1, 2, 0, 1, 1, 1]显示3次.
一个简单的天真解决方案将涉及将我的所有行转换为元组,并应用collections.Counter,如下所示:
from collections import Counter
def row_counter(my_array):
list_of_tups = [tuple(ele) for ele in my_array]
return …Run Code Online (Sandbox Code Playgroud) 例如,我的输入是:
scala> val myList = List("7842", "abf45", "abd", "56")
myList: List[String] = List(7842, abf45, abd, 56)
Run Code Online (Sandbox Code Playgroud)
7842并且56可以转换为Int; 因此,我的预期产量是2.我们可以假设负整数不会发生,因此-67是不可能的.
这是我到目前为止:
scala> myList.map(x => Try(x.toInt).getOrElse(-1)).count(_ > -1)
res15: Int = 2
Run Code Online (Sandbox Code Playgroud)
这应该是正常的,但我觉得我错过了一个更优雅和可读的解决方案,因为我所要做的就是计算成功的数量.
以下代码运行完全正常:
package main
import (
"fmt"
)
func my_func(c chan int){
fmt.Println(<-c)
}
func main(){
c := make(chan int)
go my_func(c)
c<-3
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我改变
c<-3
Run Code Online (Sandbox Code Playgroud)
至
time.Sleep(time.Second)
c<-3
Run Code Online (Sandbox Code Playgroud)
我的代码没有执行.
我的直觉是main在my_func完成执行之前以某种方式返回,但似乎添加暂停应该没有任何效果.我完全迷失在这个简单的例子上,这里发生了什么?
我的理解是,当插入具有相同主键的另一行时,将覆盖行.
例如:
我有专栏(user_id int, item_id int, site_id int)和我的专栏PRIMARY KEY(user_id, item_id)
如果我有下表:
user_id, item_id, site_id
2 3 4
Run Code Online (Sandbox Code Playgroud)
我插入user_id : 2, item_id : 3, site_id : 10,我的新表将是:
user_id, item_id, site_id
2 3 10
Run Code Online (Sandbox Code Playgroud)
不
user_id, item_id, site_id
2 3 4
2 3 10
Run Code Online (Sandbox Code Playgroud)
这种简单的案例是否适用于所有情况?我可能没有注意到任何微妙之处吗?另外,我在文档中找不到这个并通过玩cassandra来得出这个结论,任何人都可以提供文档源吗?