Python Forum
Explain the python code in this definition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Explain the python code in this definition
#1
I found the following python o online and it looks very useful to me.

def get_mean_features(sensor_list, df):
    """
    Function to get mean val of 10min window
    """

    new_features = {}

    # looping over all sensors and calculating mean of 10 values
    for sensor in sensor_list:
        new_features[sensor] = df[sensor].rolling(10).mean().values

    labels = []
    # creating list with None value
    labels = [None] * (df.shape[0])

    for i in range(0, df.shape[0]-10):
        labels[i+9] = df['label'][i+10]

    new_features['label'] = labels

    # creating dataframe
    new_df = pd.DataFrame(new_features)
    # removing first 9 rows with null values
    new_df.drop(new_df.head(9).index, inplace=True)
    # resetting index 
    new_df.reset_index(inplace=True, drop=True)
    # dropping last row with null value
    new_df.drop(new_df.tail(1).index, inplace=True)
    return new_df
I have several questions on this code. I just want to start off by asking why these two lines.

    new_features = {}

    labels = []
   
why the two different delimiters brackets and braces? Why not just one set of brackets or braces?

Any help appreciated.

Respectfully,

LZ
Reply
#2
new_features = {} makes an empty dictionary.
labels = [] makes an empty list

But you know that already. What is the real question?

I think I've responded to 2 or 3 of your posts showing how to do a rolling mean. One of those times I even presented the results of a timing test showing that using rolling() was 4000 times faster than the method you used to compute a rolling mean. Now you think this code looks like it could be useful, sounding like it is something completely new to you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 271 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  mutable argument in function definition akbarza 1 494 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  error occuring in definition a class akbarza 3 725 Nov-26-2023, 09:28 AM
Last Post: Yoriz
  determine parameter type in definition function akbarza 1 596 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  [split] Explain the python code in this definition Led_Zeppelin 1 752 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,025 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  Sudoku Solver in Python - Can someone explain this code ? qwemx 6 2,152 Jun-27-2022, 12:46 PM
Last Post: deanhystad
  meaning of -> syntax in function definition DrakeSoft 5 1,977 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
  Can someone explain this small snippet of code like I am a 5 year old? PythonNPC 3 1,255 Apr-08-2022, 05:54 PM
Last Post: deanhystad
  Could you explain each part of the code? Tsushida 2 1,524 Mar-20-2022, 08:19 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020