我仍在學習 python,我正在撰寫代碼將陣列裁剪為最小值和最大值,但不使用任何回圈。
import numpy as np
import matplotlib.pyplot as plt
def clip(array, minimum, maximum):
return None
array = [1,2,3,4,5,6,7,8]
minimum = input ("Enter your minimum value")
maximum = input ("Enter your maximum value")
# min = minimum
# max = maximum
# mean = (min max)/2
result_arr = clip(array, minimum, maximum)
print (result_arr)
plt.plot(array, result_arr)
plt.show()
但我仍然沒有顯示結果圖。我需要修復什么?
uj5u.com熱心網友回復:
根據您的“剪輯”應該做什么,這里有一些關于“本地 python”的想法,即沒有匯入(可以使用 egnumpy或來完成pandas.Series):
#Remove all elements outside [mi,ma]
a = [1,2,3,4,5,6,7,8,9,10]
mi = 3 #min
ma = 7 #max
list(filter(lambda x: mi<x<ma,a)) # [4,5,6]
#Set elements greater than 7 to 7 and all elements less than 3 to three
def clip_to_min_max(x,mi,max):
if x<mi: #Number is less than "mi" set it to "mi"
return mi
if x>ma: #Number is greater han "max", set it to "ma"
return mx
return x #It is between "mi" and "ma" - do nothing
[clip_to_min_max(x,3,7) for x in a] #[3,3,3,4,5,6,7,7,7,7]
uj5u.com熱心網友回復:
你需要知道 numpy.where
def clip(v, vmin, vmax):
from numpy import where
return where(v<vmin, vmin, where(v>vmax, vmax, v))
...
a = a_from(b, c, d)
a_clipped = clip(a, amin, amax)
例如,
In [6]: def clip(v, vmin, vmax):
...: from numpy import where
...: return where(v<vmin, vmin, where(v>vmax, vmax, v))
...:
...: clip(np.arange(10, 30), 15, 25)
Out[6]:
array([15, 15, 15, 15, 15, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 25,
25, 25, 25])
當然也有,numpy.clip(a, amin, amax)但我的印象是你需要推出自己的......
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/335931.html
標籤:Python 麻木的 matplotlib
