本文的目的是介绍过滤2Dnumpy数组的详细情况,特别关注numpy过滤出所需要的行的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解过滤2Dnumpy数组的机会,同时
本文的目的是介绍过滤2D numpy数组的详细情况,特别关注numpy过滤出所需要的行的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解过滤2D numpy数组的机会,同时也不会遗漏关于numpy数组-过滤数组、python numpy数组的numpy数组、python – 通过没有循环的2D索引数组索引2D numpy数组、从numpy数组列表创建numpy数组的Python方法的知识。
本文目录一览:- 过滤2D numpy数组(numpy过滤出所需要的行)
- numpy数组-过滤数组
- python numpy数组的numpy数组
- python – 通过没有循环的2D索引数组索引2D numpy数组
- 从numpy数组列表创建numpy数组的Python方法
过滤2D numpy数组(numpy过滤出所需要的行)
我想要一个numpy 2D ndarray的子数组(在最小和最大之间)
xy_dat = get_xydata() x_displayed = xy_dat[((xy_dat > min) & (xy_dat < max))]
最小值和最大值是浮点数,以便与数组xy_dat的第一个值进行比较
xy_dat是2D numpy数组:
[[ 735964. 1020. ] [ 735964.04166667 1020. ] [ 735964.08333333 1020. ] ..., [ 736613.39722222 1095. ] [ 736613.40416667 1100. ] [ 736613.41111111 1105. ]]
x_displayed已正确过滤,但我丢失了第二个值(现在是一维数组):
[ 735964.04166667 735964.08333333 735964.125 ..., 736613.39027778 736613.39722222 736613.40416667]
如何在第一个值上过滤并保留另一个值?
答案1
小编典典您应该仅在 第一 列上执行条件:
x_displayed = xy_dat[((xy_dat **[:,0]** > min) & (xy_dat **[:,0]** < max))]
我们在这里构造一个视图,其中仅考虑带有的第一列xy_dat[:,0]
。现在检查此1d是否在边界之间,我们构造一个应保留的行的 1D
布尔数组,现在我们将其用作xy_dat[..]
参数中的项来选择这些行。
numpy数组-过滤数组
可以使用一个numpy数组作为索引数组去过滤原数组,索引数组里为true的值,保留,为false的值去掉
import numpy as np
使用索引数组
a = np.array([1, 2, 3, 4])
b = np.array([True, True, False, False])
print a[b] #[1 2]
print a[np.array([True, False, True, False])] #[1 3]
通过对原数组进行向量化运算得到索引数组
a = np.array([1, 2, 3, 2, 1])
b = (a >= 2)
print a[b] #[2 3 2]
print a[a >= 2] #[2 3 2]
通过对某一数组进行向量化运算得到索引数组
a = np.array([1, 2, 3, 4, 5])
b = np.array([1, 2, 3, 2, 1])
print b == 2 #[False True False True False]
print a[b == 2] #[2 4]
一个例子:
# 20个学生在课程上所花费的时间
time_spent = np.array([
12.89697233, 0. , 64.55043217, 0. ,
24.2315615 , 39.991625 , 0. , 0. ,
147.20683783, 0. , 0. , 0. ,
45.18261617, 157.60454283, 133.2434615 , 52.85000767,
0. , 54.9204785 , 26.78142417, 0.
])
# 20个学生参加学习的天数
days_to_cancel = np.array([
4, 5, 37, 3, 12, 4, 35, 38, 5, 37, 3, 3, 68,
38, 98, 2, 249, 2, 127, 35
])
def mean_time_for_paid_students(time_spent, days_to_cancel):
''''''
计算参加课程大于等于7天的学生平均在课程上所花的时间
''''''
index_array = days_to_cancel >= 7
mean_time = time_spent[index_array].mean()
return mean_time
print(mean_time_for_paid_students(time_spent, days_to_cancel))
# 结果: 41.0540034855
python numpy数组的numpy数组
我在创建numpy数组的numpy数组时遇到问题。我将在一个循环中创建它:
a=np.array([])
while(...):
...
b= //a numpy array generated
a=np.append(a,b)
...
所需结果:
[[1,5,3],[9,10,1],...,[4,8,6]]
实际结果:
[1,3,9,1,... 4,6]
可能吗?我不知道数组的最终尺寸,因此无法使用固定尺寸对其进行初始化。
python – 通过没有循环的2D索引数组索引2D numpy数组
例如:
import numpy as np a = np.array([[0,3,4],[5,6,0],[0,1,9]]) inds = np.array([[0,1],[1,2],2]])
我想构建一个新数组,使得该数组中的每一行(i)都是数组a的一行(i),由数组inds(i)的行索引.我想要的输出是:
array([[ 0.,3.],# a[0][:,1]] [ 6.,0.],# a[1][:,2]] [ 0.,9.]]) # a[2][:,2]]
我可以用循环实现这个目的:
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
但我正在寻找一种纯粹的矢量化解决方案.
解决方法
array([[0,[2,2]])
由于广播,您可以使用上面的单个列,因此您可以使用np.arange(3)[:,None]是垂直范围,因为None会插入新轴:
>>> np.arange(3)[:,None] array([[0],[1],[2]])
最后,一起:
>>> a[np.arange(3)[:,None],inds] array([[0,3],# a[0,1]] [6,# a[1,2]] [0,9]]) # a[2,2]]
从numpy数组列表创建numpy数组的Python方法
我在循环中生成一维numpy数组的列表,然后将其转换为2d
numpy数组。如果我提前知道项目数,我会预先分配一个2d的numpy数组,但是我不知道,因此我将所有内容都放在了列表中。
模拟如下:
>>> list_of_arrays = map(lambda x: x*ones(2),range(5))
>>> list_of_arrays
[array([ 0.,0.]),array([ 1.,1.]),array([ 2.,2.]),array([ 3.,3.]),array([ 4.,4.])]
>>> arr = array(list_of_arrays)
>>> arr
array([[ 0.,0.],[ 1.,1.],[ 2.,2.],[ 3.,3.],[ 4.,4.]])
我的问题如下:
是否有一种更好的方法(性能上)来执行收集顺序数值数据(在我的情况下为numpy数组)的任务,而不是将它们放入列表中,然后从中制成numpy.array(我正在创建新的obj并复制数据)?经过良好测试的模块中是否有可用的“可扩展”矩阵数据结构?
我的2d矩阵的典型大小在100x10到5000x10浮动之间
编辑: 在此示例中,我正在使用地图,但是在我的实际应用程序中,我有一个for循环
关于过滤2D numpy数组和numpy过滤出所需要的行的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于numpy数组-过滤数组、python numpy数组的numpy数组、python – 通过没有循环的2D索引数组索引2D numpy数组、从numpy数组列表创建numpy数组的Python方法等相关知识的信息别忘了在本站进行查找喔。
本文标签: