我得到了一個原始的浮點數串列,其中整數值表示實驗的型別,小數值表示實驗的次數。
我的作業是洗掉所有整數值出現少于3次的浮點數。
這是我第三次使用Numpy,所以我在網上搜索了一下,所有關于這個錯誤的答案基本上都是這么說的。它們必須是相同的資料型別,并且它們必須具有相同的維度(形狀?
我研究了這兩個陣列(needToBeRemoved和newId,)都是:
*Datatypes of 32int *1D形狀。
"DeprecationWarning: elementwise comparison failed; this will raise an error in future.
indexOfRemoval = np.where(newId == needToBeRemoved) #找到所有需要被洗掉的數字的索引"。
那我能做什么來解決這個問題呢?
我的代碼:
import numpy as np
import math
import warnings
def removeIncomplete(id) 。
needToBeRemoved = []
maxMinOne = math.floor(max(id)
newId = np.array([math. floor(y) for y in id if 0< y and y<(maxMinOne 1) ]) #將串列轉為整數。
print(f "NewId : {newId}"/span>)
for i in newId:
print(i)
occurences = np.count_nonzero(newId == i)
print(f" This is occurences: {occurences}")#figures out how many times each element appears in this list。
if occurences<3。#Makes sure the element only appears in the needToBeRemoved list once.
needToBeRemoved = needToBeRemoved [i] #創建一個出現少于3次的所有數字串列。
print(f" 需要被洗掉的數字。{needToBeRemoved}")
needToBeRemoved = np.array(needToBeRemoved)
indexOfRemoval = np.where(newId == needToBeRemoved) #Finds the index of all numbers that need to be removed.
id = np.delete(id,indexOfRemoval) #洗掉這些索引位置的所有元素。
return id
arr = np.array( [1.3, 2.2, 2. 3, 4.2, 5.1, 3。 2, 5.3, 3.3, 2。 1, 1.1, 5.2, 3.1] )
removeIncomplete(arr)
注意:根據賦值,輸出必須是一個np.array。
uj5u.com熱心網友回復:
用你的arr(id):
在[322]。import 數學
在 [323]: maxMinOne = math.floor(max(arr))
在[324]: newId = np.array([math. floor(y) for y in arr if 0< y and y<(maxMinOne 1) ])
在[325]:newId
輸出[325]。array([1, 2, 2, 4, 5, 3, 5, 3, 2, 1, 5, 3] )
將陣列與標量或單元素陣列相比較:
在[326]: newId==np.array([2] )
Out[326]。
array([False, True, True, False, False, False, False, False, True,
False, False, False] )
但是將其與一個不同大小的陣列進行比較會產生:
在[327]: newId==np.array([2,3] )
<ipython-input-327-c00ae167502e>:1。DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
newId==np.array([2,3])
輸出[327]。False]。
你也可以把它和另一個相同大小的陣列進行比較:
在[328]: arr.shape
輸出[328]。(12, )
在[329]: newId==np.ones(12,int)*2]
Out[329]。
array([False, True, True, False, False, False, False, False, True,
False, False, False] )
為了將其與不同大小的陣列進行比較,我們可以使用一個廣播式的等價物:
在 [342]: newId==np. array([2,3])[:,None]
輸出[342]。
array([[False, True, True, False。False, False, False, False, True,
False, False, False]。
[False, False, False, False, False, True, False, True, False,
False, False, True] ])
并在任何一行中找到它是真的:
在任何一行中找到它是真的。
在 [343]: (newId==np. array([2,3])[:,None]).any( axis=0)
Out[343]。
array([False, True, True, False, False, True, False, True, True,
False, False, True] )
和索引:
在[346]: idx=np.where((newId==np. array([2,3])[:,None]).any(axis=0)
在[347]:idx
輸出[347]。(array([ 1, 2, 5, 7, 8, 11],)
在[348]:newId[idx] 。
輸出[348]。array([2, 2, 3, 3, 2, 3] )
在[349]: np.delete(arr, idx)
Out[349]: array([1.3, 4.2, 5. 1, 5.3, 1.1, 5.2] )
isin也可以使用
在[351]: np.isin(newId, np.array([2,3] )
輸出[351]。
array([False, True, True, False, False, True, False, True, True,
False, False, True] )
計數
在 [356]: u,c=np.unique(newId, return_counts=True)
在[357]: u
輸出[357]。array([1, 2, 3, 4, 5] )
在[358]: c
輸出[358]。array([2, 3, 3, 1, 3] )
在[359]: u[c<3]
Out[359]: array([1, 4] )
與你的回圈相比:
在[360]。def foo(newId)。
...: needToBeRemoved = [] 。
...: for i in newId:
...: occurences = np.count_nonzero(newId == i)
...: if occurences<3:
...: needToBeRemoved = needToBeRemoved [i].
...: return np.array(needToBeRemoved)
...:
在[361]: foo(newId)
Out[361]: array([1, 4, 1] )
uj5u.com熱心網友回復:
你不需要使用numpy模塊。做到以下幾點 :
arr = [1.3, 2. 2, 2.3, 4.2, 5。 1, 3.2, 5.3, 3.3, 2。 1, 1.1, 5.2, 3.1]
new_arr = []
for i in arr :
if int(i) >=3 :
new_arr.append(i)
注意:對你有用的資料是在new_arr
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/326869.html
標籤:
上一篇:為N個體的問題制作一個跟蹤影片
下一篇:Numpy從一個串列中進行追加
