【发布时间】:2021-10-01 20:42:37
【问题描述】:
如果只有 1 个传感器,即如果 col2 和 col3 在下面提供的示例数据中被删除,我当前的代码会运行并生成一个图表,留下一列。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
d = {'col1': [-2587.944231, -1897.324231,-2510.304231,-2203.814231,-2105.734231,-2446.964231,-2963.904231,-2177.254231, 2796.354231,-2085.304231], 'col2': [-3764.468462,-3723.608462,-3750.168462,-3694.998462,-3991.268462,-3972.878462,3676.608462,-3827.808462,-3629.618462,-1841.758462,], 'col3': [-166.1357692,-35.36576923, 321.4157692,108.9257692,-123.2257692, -10.84576923, -100.7457692, 89.27423077, -211.0857692, 101.5342308]}
df = pd.DataFrame(data=d)
sensors = 3
window_size = 5
dfn = df.rolling(window_size).corr(pairwise = True)
index = df.index #index of values in the data frame.
rows = len(index) #len(index) returns number of rows in the data.
sensors = 3
baseline_num = [0]*(rows) #baseline numerator, by default zero
baseline = [0]*(rows) #initialize baseline value
baseline = DataFrame(baseline)
baseline_num = DataFrame(baseline_num)
v = [None]*(rows) # Initialize an empty array v[] equal to amount of rows in .csv file
s = [None]*(rows) #Initialize another empty array for the slope values for detecting when there is an exposure
d = [0]*(rows)
sensors_on = True #Is the sensor detecting something (True) or not (False).
off_count = 0
off_require = 8 # how many offs until baseline is updated
sensitivity = 1000
for i in range(0, (rows)): #This iterates over each index value, i.e. each row, and sums the values and returns them in list format.
v[i] = dfn.loc[i].to_numpy().sum() - sensors
for colname,colitems in df.iteritems():
for rownum,rowitem in colitems.iteritems():
#d[rownum] = dfone.loc[rownum].to_numpy()
#d[colname][rownum] = df.loc[colname][rownum]
if v[rownum] >= sensitivity:
sensors_on = True
off_count = 0
baseline_num[rownum] = 0
else:
sensors_on = False
off_count += 1
if off_count == off_require:
for x in range(0, (off_require)):
baseline_num[colname][rownum] += df[colname][rownum - x]
elif off_count > off_require:
baseline_num[colname][rownum] += baseline_num[colname][rownum - 1] + df[colname][rownum] - (df[colname][rownum - off_require]) #this loop is just an optimization, one calculation per loop once the first calculation is established
baseline[colname][rownum] = ((baseline_num[colname][rownum])//(off_require)) #mean of the last "off_require" points
dfx = DataFrame(v, columns =['Sensor Correlation']) #converts the summed correlation tables back from list format to a DataFrame, with the sole column name 'Sensor Correlation'
dft = pd.DataFrame(baseline, columns =['baseline'])
dft = dft.astype(float)
dfx.plot(figsize=(50,25), linewidth=5, fontsize=40) # plots dfx dataframe which contains correlated and summed data
dft.plot(figsize=(50,25), linewidth=5, fontsize=40)
基本上,我想只为这个循环遍历每一列,而不是生成 1 个图表:
for colname,colitems in df.iteritems():
for rownum,rowitem in colitems.iteritems():
#d[rownum] = dfone.loc[rownum].to_numpy()
#d[colname][rownum] = df.loc[colname][rownum]
if v[rownum] >= sensitivity:
sensors_on = True
off_count = 0
baseline_num[rownum] = 0
else:
sensors_on = False
off_count += 1
if off_count == off_require:
for x in range(0, (off_require)):
baseline_num[colname][rownum] += df[colname][rownum - x]
elif off_count > off_require:
baseline_num[colname][rownum] += baseline_num[colname][rownum - 1] + df[colname][rownum] - (df[colname][rownum - off_require]) #this loop is just an optimization, one calculation per loop once the first calculation is established
我尝试了其他问题的其他解决方案,但似乎都没有解决这个问题。 到目前为止,我已经尝试过多次转换为列表和元组之类的东西,然后像这样称呼它们:
baseline_num[i,column] += d[i - x,column]
还有
baseline_num[i][column += d[i - x][column]
在循环中使用
for column in columns
但是,无论我如何安排解决方案,总是存在一些期望整数或切片索引的关键错误,以及其他错误。 有关实际数据的一列的预期/可能输出,请参见图片。具有不同的输入参数(灵敏度值和 off_require 在不同情况下会有所不同。) 一个不起作用的解决方案是来自此链接的循环方法:
https://www.geeksforgeeks.org/iterating-over-rows-and-columns-in-pandas-dataframe/
我也尝试过使用 iteritems 作为外循环创建一个循环。这也不起作用。
下面是各种敏感度值的可能图形输出的链接,以及我实际数据集中的窗口,只有一列。 (即我手动删除了其他列,并仅使用当前程序绘制了一个)
如果我遗漏了任何有助于解决此问题的内容,请告诉我,以便我立即更正。
查看我原来的 df.head 的这张图片: df.head
【问题讨论】:
-
这里的代码太多,逻辑太多,请提供一个最小的可重现示例,否则很遗憾您将得不到任何帮助
-
好的,谢谢。我会试着去做,只是看着它我不确定我可以遗漏什么来做一个更小的例子。这已经是我原始功能代码的“小”功能摘录。不过,我会尝试找出一种方法来制作一个像你说的那样的小例子。
-
此代码不运行。
NameError: name 'dfone' is not defined第 37 行 -
对不起。 dfone 是为一个图形运行它的线。它下面的注释行是用于一次运行多个的行。我已经编辑了代码以删除 dfone 行。
标签: python pandas dataframe numpy data-science