我需要什么?[无序列表]
我看到了什么?
所以,请帮我选择.
编辑 A = B + C指的是元素加法,而不是列表串联
我有一个大的10,000,000长度数组,包含行.我需要单独洗牌那些行.例如:
[[1,2,3]
[1,2,3]
[1,2,3]
...
[1,2,3]]
Run Code Online (Sandbox Code Playgroud)
至
[[3,1,2]
[2,1,3]
[1,3,2]
...
[1,2,3]]
Run Code Online (Sandbox Code Playgroud)
我正在使用
map(numpy.random.shuffle, array)
Run Code Online (Sandbox Code Playgroud)
但它是一个python(而不是NumPy)循环,它占用了99%的执行时间.可悲的是,PyPy JIT没有实现numpypy.random,所以我运气不好.有没有更快的方法?我愿意用任何库(pandas,scikit-learn,scipy,theano,等,只要它使用一个numpy的ndarray或衍生物.)
如果没有,我想我会使用Cython或C++.
我发现很难找到解决以下问题的快速解决方案:
我有一个观察矢量,它表明观察某些现象的时间.
example <- c(0,0,0,1,0,1,1,0,0,0,-1,0,0,-1,-1,0,0,1,0,0);
Run Code Online (Sandbox Code Playgroud)
现在我想在特定观察之间消除零,假设某个现象被认为会继续,直到发现一个矛盾的观察结果,即,如果在第三次观察中观察到''1',我想只有''1 ''到第11个元素,当第一个'-1'被观察到时.所以我想要的输出看起来像:
desired.output <- c(0,0,0,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,1,1,1);
> print(cbind(example, desired.output))
example desired.output
[1,] 0 0
[2,] 0 0
[3,] 0 0
[4,] 1 1
[5,] 0 1
[6,] 1 1
[7,] 1 1
[8,] 0 1
[9,] 0 1
[10,] 0 1
[11,] -1 -1
[12,] 0 -1
[13,] 0 -1
[14,] -1 -1
[15,] -1 -1
[16,] 0 -1
[17,] 0 -1
[18,] 1 1
[19,] 0 1
[20,] 0 1
Run Code Online (Sandbox Code Playgroud)
我的蹩脚解决方案是
for (i in 1:length(example)){ …Run Code Online (Sandbox Code Playgroud) 我在使用该numpy.vectorize功能时遇到了一些问题.
我已经定义了一个适用于单个元素输入的函数,但矢量化版本返回不同的结果 - 我做错了什么?
码:
def c_inf_comp(z):
if z>0:
return np.exp(-1./(z*z))
else:
return 0
>>> x = np.array([-10., 10.])
>>> x
array([-10., 10.])
>>> c_inf_comp(x[0])
0
>>> c_inf_comp(x[1])
0.99004983374916811
>>> vfunz = np.vectorize(c_inf_comp)
>>> vfunz(x)
array([0, 0])
Run Code Online (Sandbox Code Playgroud) 有没有一种方法,我可以使用SciPy的功能就像在地方上一个(或)使用的变体,等等?norm.cdf numpy.arraypandas.DataFramenumpy.applynumpy.apply_along_axs
背景是,我有一个z得分值表,我想将其转换为规范分布的CDF值.我目前使用norm.cdf的scipy这一点.
我目前正在操纵具有非数字值的数据帧.
Name Val1 Val2 Val3 Val4
0 A -1.540369 -0.077779 0.979606 -0.667112
1 B -0.787154 0.048412 0.775444 -0.510904
2 C -0.477234 0.414388 1.250544 -0.411658
3 D -1.430851 0.258759 1.247752 -0.883293
4 E -0.360181 0.485465 1.123589 -0.379157
Run Code Online (Sandbox Code Playgroud)
(使Name变量成为索引是一种解决方案,但在我的实际数据集中,名称不是字母字符.)
要仅修改数字数据,我使用df._get_numeric_data()的私有函数返回包含数据框的数字数据的数据框.但是,没有任何set功能.因此,如果我打电话
norm.cdf(df._get_numeric_data)
Run Code Online (Sandbox Code Playgroud)
这不会改变df原始数据.
我试图通过应用norm.cdf到数字数据框来避免这种情况,因此这会改变我的原始数据集.
新的RyuJIT编译器是否曾生成向量(SIMD)CPU指令,何时?
附注:System.Numerics命名空间包含允许显式使用Vector操作的类型,这些操作可能会也可能不会生成SIMD指令,具体取决于CPU,CLR版本,JITer版本,是否直接编译为本机代码.这个问题具体是关于何时非矢量代码(例如在C#或F#中)将产生SIMD指令.
我必须使用一些数千行来处理整个数据帧,但我可以简化如下:
df = pd.DataFrame([
('a', 1, 1),
('a', 0, 0),
('a', 0, 1),
('b', 0, 0),
('b', 1, 0),
('b', 0, 1),
('c', 1, 1),
('c', 1, 0),
('c', 1, 0)
], columns=['A', 'B', 'C'])
print (df)
A B C
0 a 1 1
1 a 0 0
2 a 0 1
3 b 0 0
4 b 1 0
5 b 0 1
6 c 1 1
7 c 1 0
8 c 1 0
Run Code Online (Sandbox Code Playgroud)
我的目标是根据它们在"A"列中的标签来展平"B"和"C"列
A B_1 B_2 …Run Code Online (Sandbox Code Playgroud) 首先,如果在其他地方已经回答了这个问题,我深表歉意。我能找到的只是关于替换给定值的元素的问题,而不是多个值的元素。
我有几千个大型 np.arrays,如下所示:
# generate dummy data
input_array = np.zeros((100,100))
input_array[0:10,0:10] = 1
input_array[20:56, 21:43] = 5
input_array[34:43, 70:89] = 8
Run Code Online (Sandbox Code Playgroud)
在这些数组中,我想根据字典替换值:
mapping = {1:2, 5:3, 8:6}
Run Code Online (Sandbox Code Playgroud)
这时候,我使用了一个简单的循环,结合花哨的索引:
output_array = np.zeros_like(input_array)
for key in mapping:
output_array[input_array==key] = mapping[key]
Run Code Online (Sandbox Code Playgroud)
我的数组的维度为 2000 到 2000,字典有大约 1000 个条目,因此,这些循环需要永远。
是否有一个函数,它只需要一个数组和一个字典(或类似形式)形式的映射,并输出更改后的值?
非常感谢帮助!
我在 Ipython 中测试了各个解决方案,使用
%%timeit -r 10 -n 10
import numpy as np
np.random.seed(123)
sources = range(100)
outs = [a for a in range(100)]
np.random.shuffle(outs)
mapping = {sources[a]:outs[a] …Run Code Online (Sandbox Code Playgroud) 我实现了以下代码,将 32 个字节的输入转换为大写:
版本 1:
void to_upper(char* input) {
for (int i = 0; i < 32; ++i) {
input[i] = (input[i] >= 'a' && input[i] <= 'z') ? input[i] - 32 : input[i];
}
}
Run Code Online (Sandbox Code Playgroud)
版本 2:
void to_upper(char* input) {
for (int i = 0; i < 32; ++i) {
if (input[i] >= 'a' && input[i] <= 'z') {
input[i] = input[i] - 32; // same for: input[i] -= 32;
}
}
}
Run Code Online (Sandbox Code Playgroud)
第一个版本被自动矢量化,第二个没有。该行为在 clang 和 gcc 中是一致的。此外,我也在 …
我有一个双列矩阵M,包含一堆间隔的开始/结束索引:
startInd EndInd
1 3
6 10
12 12
15 16
Run Code Online (Sandbox Code Playgroud)
如何生成所有区间索引的向量:
v = [1 2 3 6 7 8 9 10 12 15 16];
Run Code Online (Sandbox Code Playgroud)
我正在使用循环进行上述操作,但我想知道是否有更优雅的矢量化解决方案?
v = [];
for i=1:size(M,1)
v = [v M(i,1):M(i,2)];
end
Run Code Online (Sandbox Code Playgroud)