大佬教程收集整理的这篇文章主要介绍了NP。 max() 未在 Python 中的“if”中显示结果,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直试图从我导入的 CSV 文件中获取高于特定阈值的第一个值。当我使用 np.where()
时,我只得到第一个值第一次超过阈值的索引号,而不是值本身。我尝试使用 df.iloc[]
打印它,但即使直接提供索引号也不起作用。我做了 df.iloc[Index_Abs_Ax]
和 df.iloc[1000]
,其中 Index_Abs_Ax 是存储索引号 1000 的变量。但是有一个错误:No axis named 60081 for object type DataFrame
第二件事是 CSV 文件中的某些最大值低于阈值。所以在这种情况下,我想将阈值减半。为此,我使用 if 语句,如:if threshold_abs > np.max(resA_x1)
然后将阈值除以 2。但我可能会错误地说:InvalID Syntax。
所以我想知道以下三件事:
代码如下:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path
global df
df = pd.DataFrame([])
def function_plot(x,y,z,v,Plot_ShareY=TruE): #x,v are filenames for the same drop height
data_1 = Path(X) #stores the relative path LOCATIOn
data_2 = Path(y)
data_3 = Path(z)
data_4 = Path(v)
print(data_1) #prints the file LOCATIOn for each input argument
print(data_2)
print(data_3)
print(data_4)
x1 = pd.read_csv(data_1,delimiter=";",skiprows=(1),decimal = ",",na_values = ['no info','.']) #loads the csv file from the path created above
y1 = pd.read_csv(data_2,'.'])
z1 = pd.read_csv(data_3,'.'])
v1 = pd.read_csv(data_4,'.'])
time1 = x1.iloc[:,0] #stores time values of each file
time2 = y1.iloc[:,0]
time3 = z1.iloc[:,0]
time4 = v1.iloc[:,0]
resA_x1 = x1.iloc[:,1] #stores sensor output values for each sensor from each file
resB_x1 = x1.iloc[:,2]
resC_x1 = x1.iloc[:,3]
resD_x1 = x1.iloc[:,4]
resA_y1 = y1.iloc[:,1]
resB_y1 = y1.iloc[:,2]
resC_y1 = y1.iloc[:,3]
resD_y1 = y1.iloc[:,4]
resA_z1 = z1.iloc[:,1]
resB_z1 = z1.iloc[:,2]
resC_z1 = z1.iloc[:,3]
resD_z1 = z1.iloc[:,4]
resA_v1 = v1.iloc[:,1]
resB_v1 = v1.iloc[:,2]
resC_v1 = v1.iloc[:,3]
resD_v1 = v1.iloc[:,4]
threshold_abs = 0.03
if threshold_abs > np.max(resA_x1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resB_x1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resC_x1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resD_x1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resA_y1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resB_y1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resC_y1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resD_y1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resA_z1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resB_z1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resC_z1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resD_z1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resA_v1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resB_v1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resC_v1)
threshold_abs = threshold_abs/2
print(threshold_abs)
elif threshold_abs > np.max(resD_v1)
threshold_abs = threshold_abs/2
print(threshold_abs)
Index_Abs_Ax = np.where(resA_x1 > threshold_abs) [0][0] #prints the first index where the condition holds
Index_Abs_Bx = np.where(resB_x1 > threshold_abs) [0][0]
Index_Abs_Cx = np.where(resC_x1 > threshold_abs) [0][0]
Index_Abs_Dx = np.where(resD_x1 > threshold_abs) [0][0]
print(Index_Abs_AX)
LOCATIOn = df.iloc(Index_Abs_AX)
Index_Abs_Ay = np.where(resA_y1 > threshold_abs) [0][0] #prints the first index where the condition holds
Index_Abs_By = np.where(resB_y1 > threshold_abs) [0][0]
Index_Abs_Cy = np.where(resC_y1 > threshold_abs) [0][0]
Index_Abs_Dy = np.where(resD_y1 > threshold_abs) [0][0]
Index_Abs_Az = np.where(resA_z1 > threshold_abs) [0][0] #prints the first index where the condition holds
Index_Abs_Bz = np.where(resB_z1 > threshold_abs) [0][0]
Index_Abs_Cz = np.where(resC_z1 > threshold_abs) [0][0]
Index_Abs_Dz = np.where(resD_z1 > threshold_abs) [0][0]
Index_Abs_Av = np.where(resA_v1 > threshold_abs) [0][0] #prints the first index where the condition holds
Index_Abs_Bv = np.where(resB_v1 > threshold_abs) [0][0]
Index_Abs_Cv = np.where(resC_v1 > threshold_abs) [0][0]
Index_Abs_Dv = np.where(resD_v1 > threshold_abs) [0][0]
LOCATIOn_x1 = print(Index_Abs_Ax,Index_Abs_Bx,Index_Abs_Cx,Index_Abs_DX)
LOCATIOn_y1 = print(Index_Abs_Ay,Index_Abs_By,Index_Abs_Cy,Index_Abs_Dy)
LOCATIOn_z1 = print(Index_Abs_Az,Index_Abs_Bz,Index_Abs_Cz,Index_Abs_Dz)
LOCATIOn_v1 = print(Index_Abs_Av,Index_Abs_Bv,Index_Abs_Cv,Index_Abs_Dv)
fig,(ax1,ax2,ax3,ax4) = plt.subplots(4,1,figsize=(12,8)) #plots figure with 4 plots
fig.suptitle('Drops at 1 cm')
ax1.plot(time1,resA_x1,'r',resB_x1,'g',resC_x1,'b',resD_x1,'y')
ax1.set_ylabel('A [V]')
ax2.plot(time2,resA_y1,resB_y1,resC_y1,resD_y1,'y')
ax2.set_ylabel('B [V]')
ax3.plot(time3,resA_z1,resB_z1,resC_z1,resD_z1,'y')
ax3.set_ylabel('C [V]')
ax4.plot(time4,resA_v1,resB_v1,resC_v1,resD_v1,'y')
ax4.set_xlabel('Time [ms]')
ax4.set_ylabel('D [V]')
plt.show()
#function for plotTing called
function_plot('above-1-cm-A1-3.csv','above-1-cm-B2-1.csv','above-1-cm-C3-1.csv','above-1-cm-D4-1.csv')
Numpy 提供了许多 advanced ways of indexing。
如果您有一个索引数组,例如在您的示例中来自 numpy.when
,您可以使用该数组本身作为索引来获取相应的值:
>>> import numpy as np
>>> data = np.random.randint(1,1000,size=32)
>>> data
array([356,32,543,787,690,837,728,66,782,836,273,696,831,589,641,705,42,812,664,78,323,286,106,256,97,152,53,486,578,310,459,632])
>>> threshold = 800
>>> above = np.where(data > threshold)
>>> above
(array([ 5,9,12,17]),)
>>> data[above]
array([837,812])
至于为什么 if
语句不起作用,看起来您只是在条件子句之后缺少冒号:
>>> if 2 > 1:
... print('2 > 1')
...
2 > 1
更新@H_944_57@:以上通常也适用于多维 numpy
数组:
>>> data = np.random.random(size=(10,3))
>>> data
array([[0.569567,0.62124998,0.88555157],[0.66075234,0.78935382,0.18876415],[0.69428287,0.0953168,0.38439925],[0.95652594,0.15582194,0.53464335],[0.45203105,0.63865219,0.55559562],[0.49136462,0.15233917,0.5780308 ],[0.22046296,0.77791941,0.1071748 ],[0.60487268,0.09086179,0.37343351],[0.30512539,0.18934912,0.5882598 ],[0.95298127,0.25708113,0.53816788]])
>>> above = np.nonzero(data > 0.9)
>>> above
(array([3,9]),array([0,0]))
>>> data[above]
array([0.95652594,0.95298127])
此外,如果您只对值感兴趣,您还可以通过在分析之前flatten处理数据来简化问题。
您的最后一个子问题非常广泛,因为有很多方法可以概括和 DRY 一系列条件语句。也许这作为一个单独的问题会更好?
以上是大佬教程为你收集整理的NP。 max() 未在 Python 中的“if”中显示结果全部内容,希望文章能够帮你解决NP。 max() 未在 Python 中的“if”中显示结果所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。