Thank you for browsing.
I am planning to read the Excel.csv file, calculate the root mean square (RMS) for every 100 data, and output it to another Excel.csv file.
I tried to make a program on my own, but I can't execute it.
The Excel.csv file to read is as follows.
There are about 4500 rows of data in columns A and B, and we want to RMS column B.
Thank you.
I cannot write a program.
 
ValueError Traceback (most recent call last)
<ipythoninput42b1f58483765>in<module>()
16 data = pd.read_csv ("P_emg1524717370.csv", index_col = "time")
17 df_emg1 = data.iloc [:, [0]]
>18 RMS1 = window_rms (df_emg1, WINDOW_SIZE)
19 df_RMS_emg1 = RMS1
20
<ipythoninput42b1f58483765>in window_rms (a, window_size)
11 a2 = np.power (a, 2)
12 window = np.ones (window_size)/float (window_size)
>13 return np.sqrt (np.convolve (a2, window, "same"))
14
15 Load #csv file
~ \ Anaconda3 \ lib \ sitepackages \ numpy \ core \ numeric.py in convolve (a, v, mode)
1034 raise ValueError ('v cannot be empty')
1035 mode = _mode_from_name (mode)
>1036 return multiarray.correlate (a, v [::1], mode)
1037
1038
ValueError: object too deep for desired array
Applicable source code
coding: utf8
import csv
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
import pandas as pd
WINDOW_SIZE = 100
def window_rms (a, window_size):
a2 = np.power (a, 2)
window = np.ones (window_size)/float (window_size)
return np.sqrt (np.convolve (a2, window,"same"))
data = pd.read_csv ("example.csv", index_col ="semit")
df_iwhr1 = data.iloc [:, [0]]
RMS1 = window_rms (df_iwhr1, WINDOW_SIZE)
df_RMS_iwhr1 = RMS1
csvfile = open ("new1.csv",'w', newline ="")
df_RMS_iwhr1.to_csv (csvfile)
python
Tried
Please describe what you tried for the problem here.
Supplemental information (FW/tool version etc.)Please provide more detailed information here.

Answer # 1

Answer # 2
df_iwhr1
of the caller isDataFrame
, soa2
in thewindow_rms
functionshape
becomes(12345,1)
and a presentation error has occurred.
In this presentation code, all rows are targeted, so it is not necessary to use.iloc
, anddata ['semit']
is used to set the column value toPass it as a Series
.
Reference: ValueError: object too deep for desired array while using convolutionimport numpy as np import pandas as pd def window_rms (a, window_size): a2 = np.power (a, 2) window = np.ones (window_size)/float (window_size) return np.sqrt (np.convolve (a2, window, "same")) WINDOW_SIZE = 3 data = pd.DataFrame ({'semit': [i + 1 for i in range (10)], 'iwhr1': [(i + 1) * 10 for i in range (10)]}) print (data) RMS1 = window_rms (data ['semit'], WINDOW_SIZE) data ['RMS'] = RMS1 print (data)

Answer # 3
Because it has been converted into a DataFrame, it is a code that makes full use of it.
data ['sq'] = np.sqrt (df_iwhr1) # square all elements data ['mean'] = data.rolling (window = WINDOW_SISE) .mean () ['sq'] # Moving average data ['rms'] = np.sqrt (data ['mean']) #Square root of the result
Since the progress of the calculation is also in the data, please take out what you need and output it to a csv file
 python  you may need to restart the kernel to use updated packages error
 php  coincheck api authentication doesn't work
 php  i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
 [php] i want to get account information using coincheck api
 the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
 javascript  how to check if an element exists in puppeteer
 python 3x  typeerror: 'method' object is not subscriptable
 i want to call a child component method from a parent in vuejs
 xcode  pod install [!] no `podfile 'found in the project directory
 dart  flutter: the instance member'stars' can't be accessed in an initializer error
RMS is calculated by moving average.
In
pandas
, you can write like this.[Supplement]
A brief explanation.
First, if there is a onedimensional array (pandas.Series) such as [1,2,3,4,5,6,7,8,9], RMS is
can be obtained, so the expression to calculate this RMS is functionalized with
lambda
It becomes.
Next, in the case of the code of this question, it is required to calculate the RMS of a certain interval instead of calculating the RMS of the entire column, so this part is
As inSeries.rolling ( )
.To explain
Series.rolling ()
with a simple sample,for a onedimensional array such as [1,2,3,4,5,6,7,8,9] Apply rolling (). sum ()
, you can find the total for each fixed interval (this time, the interval specified by window = 3).
In this case, I want to apply my own function (rms), not the total for each fixed interval, so use
Series.rolling (). Apply ()
It becomes.
Lastly, regarding the rolling parameters, in the above example, the result of
index = 0,1
isNaN
. This is because the number of input data for calculating this part does not satisfy the number of sections (Window = 3) and cannot be calculated. Therefore,min_periods = 1
is passed as a parameter, and it is specified to calculate if there is at least one input data. In the above example, the result of the input dataindex = 0,1,2
is inindex = 2
. Therefore, by passingcenter = True
, the result of index = 0,1,2 is output to index = 1.