我對編程了解不多,但我有大量資料要分析,而且必須用 Python 完成。假設我有兩個陣列:
import numpy as np
x=np.array([1,2,3,4,5,6,7,8,9,10])
y=np.array([25,18,16,19,30,5,9,20])
并說我想選擇 y 中大于 17 的值,并且只保留 x 中與 y 中的左值具有相同索引的值。例如,我想洗掉 y (25) 的第一個值,并相應地洗掉 x (1) 中的匹配值。我試過這個:
filter=np.where(y>17, 0, y)
但我不知道如何相應地過濾 x 值(實際資料是更長的陣列,因此“手動”基本上是不可能的)
uj5u.com熱心網友回復:
由于您的問題不完全清楚并且您沒有提供預期的輸出,這里有兩種可能性:
過濾
Nunique 陣列可以通過布爾陣列(可迭代)進行切片。
如果兩個陣列的長度相同,您可以執行以下操作:
x[y>17]
在這里,x比y所以我們首先需要使其長度相同:
import numpy as np
x=np.array([1,2,3,4,5,6,7,8,9,10])
y=np.array([25,18,16,19,30,5,9,20])
x[:len(y)][y>17]
輸出: array([1, 2, 4, 5, 8])
替代品
要根據條件在x和之間進行選擇y,請使用where:
np.where(y>17, x[:len(y)], y)
輸出:
array([ 1, 2, 16, 4, 5, 5, 9, 8])
uj5u.com熱心網友回復:
作為在 Numpy 方面幾乎沒有經驗的人,我在看到 @mozway 的優秀過濾答案之前寫了這個答案。我的答案適用于比 Numpy 的陣列更通用的容器,盡管因此它使用了更多的概念。我將嘗試足夠詳細地解釋每個概念,以使答案有意義。
特爾;博士:
請務必閱讀答案的其余部分,它會幫助您了解發生了什么。
import numpy as np
x = np.array([1,2,3,4,5,6,7,8,9,10])
y = np.array([25,18,16,19,30,5,9,20])
filtered_x_list = []
filtered_y_list = []
for i in range(min(len(x), len(y))):
if y[i] > 17:
filtered_y_list.append(y[i])
filtered_x_list.append(x[i])
filtered_x = np.array(filtered_x_list)
filtered_y = np.array(filtered_y_list)
# These lines are just for us to see what happened
print(filtered_x) # prints [1 2 4 5 8]
print(filtered_y) # prints [25 18 19 30 20]
預備知識
Python 容器(串列、陣列和一堆其他我不會涉及的東西)
讓我們看一下這條線:
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Python 在做什么?
它做的第一件事是創建一個串列:
[1, 2, 3] # and so on
在這個解決方案中,Python 中的串列有一些對我們有用的特性:
訪問元素:
x_list = [ 1, 2, 3 ]
print(x_list[0]) # prints 1
print(x_list[1]) # prints 2, and so on
添加元素到最后:
x_list = [ 1, 2, 3 ]
x_list.append(4)
print(x_list) # prints [1, 2, 3, 4]
迭代:
x_list = [ 1, 2, 3 ]
for x in x_list:
print(x)
# prints:
# 1
# 2
# 3
Numpy arrays are slightly different: we can still access and iterate elements in them, but once they're created, we can't modify them - they have no .append, and there are other modifications one can do with lists (like changing one value, or deleting a value) we can't do with numpy arrays.
So the filtered_x_list and the filtered_y_list are empty lists we're creating, but we're going to modify them by adding the values we care about to the end.
The second thing Python is doing is creating a numpy array, using the list to define its contents. The array constructor can take a list expressed as [...], or a list defined by x_list = [...], which we're going to take advantage of later.
A little more on iteration
In your question, for every x element, there is a corresponding y element. We want to test something for each y element, then act on the corresponding x element, too.
Since we can access the same element in both arrays using an index - x[0], for instance - instead of iterating over one list or the other, we can iterate over all indices needed to access the lists.
First, we need to figure out how many indices we're going to need, which is just the length of the lists. len(x) lets us do that - in this case, it returns 10.
What if x and y are different lengths? In this case, I chose the smallest of the two - first, do len(x) and len(y), then pass those to the min() function, which is what min(len(x), len(y)) in the code above means.
Finally, we want to actually iterate through the indices, starting at 0 and ending at len(x) - 1 or len(y) - 1, whichever is smallest. The range sequence lets us do exactly that:
for i in range(10):
print(i)
# prints:
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
So range(min(len(x), len(y))), finally, gets us the indices to iterate over, and finally, this line makes sense:
for i in range(min(len(x), len(y))):
Inside this for loop, i now gives us an index we can use for both x and y.
Now, we can do the comparison in our for loop:
for i in range(min(len(x), len(y))):
if y[i] > 17:
filtered_y_list.append(y[i])
Then, including xs for the corresponding ys is a simple case of just appending the same x value to the x list:
for i in range(min(len(x), len(y))):
if y[i] > 17:
filtered_y_list.append(y[i])
filtered_x_list.append(x[i])
The filtered lists now contain the numbers you're after. The last two lines, outside the for loop, just create numpy arrays from the results:
filtered_x = np.array(filtered_x_list)
filtered_y = np.array(filtered_y_list)
Which you might want to do, if certain numpy functions expect arrays.
While there are, in my opinion, better ways to do this (I would probably write custom iterators that produce the intended results without creating new lists), they require a somewhat more advanced understanding of programming, so I opted for something simpler.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/351772.html
下一篇:在C 中從陣列列印值
