Python 请解释下面提到的这一行代码

Python 请解释下面提到的这一行代码,python,pandas,machine-learning,data-science,Python,Pandas,Machine Learning,Data Science,首先,“clicks”数据帧通过函数“get_data_splits”传递,该函数返回列车、有效和测试。此外,它通过列车模型传递 clicks = clicks.join(interactions) print("Score with interactions") train, valid, test = get_data_splits(clicks) _ = train_model(train, valid) 线索:您将创建的第一个功能是过去六小时内来自同一IP的事件数。经常访问的用户很可能

首先,“clicks”数据帧通过函数“get_data_splits”传递,该函数返回列车、有效和测试。此外,它通过列车模型传递

clicks = clicks.join(interactions)
print("Score with interactions")
train, valid, test = get_data_splits(clicks)
_ = train_model(train, valid)
线索:您将创建的第一个功能是过去六小时内来自同一IP的事件数。经常访问的用户很可能会下载该应用程序。实现一个函数count_pass_events,该函数接受一系列单击时间(时间戳),并返回另一个带有上一小时事件数的序列。 但我无法理解这些代码行

def count_past_events(series, time_window='6H'):
    series = pd.Series(series.index, index=series)
    past_events = series.rolling(time_window).count() - 1
    return past_events

大家好,欢迎来到论坛!可悲的是,你写的方法不起作用。滚动方法的time_window参数定义了移动窗口的大小,但对于而言,用于计算统计数据的观察数。(见此处:)

.resample与.rolling类似,但它用于基于时间的偏移。这就是你需要的!您可以直接使用数据帧。请参见下面的完整示例:

#Create a sample DataFrame
df=pd.DataFrame([["1/1/2016 12:00:20 AM", 1],
                 ["1/2/2016 5:03:20 AM", 2],
                 ["1/2/2016 5:06:20 AM", 3],
                 ["1/2/2016 5:07:20 AM", 4],
                 ["1/2/2016 6:06:20 AM", 5],
                 ["1/3/2016 00:00:20 AM", 6]]
        ,columns=['date','event_id'])

#We convert the date column into datetime and set as the index
df['date'] = pd.to_datetime(df['date'])
df.index = df.date
del df['date']


#This is where the magic happens.    
df.resample('6H', label = 'right').count()
让我们再解释一下最后一行。我们正在将数据帧缩小为六个小时的存储箱——也就是说,每行将代表六个小时的数据。对于每一行,我们将计算那里的数据点数量,每一行的名称将是bin的右边缘。这是我们得到的输出:

           date     
2016-01-01 06:00:00     1
2016-01-01 12:00:00     0
2016-01-01 18:00:00     0
2016-01-02 00:00:00     0
2016-01-02 06:00:00     3
2016-01-02 12:00:00     1
2016-01-02 18:00:00     0
2016-01-03 00:00:00     0
2016-01-03 06:00:00     1
如您所见,截至2016-01-02 06:00:00,在前六个小时内发生了三起事件


快乐的黑客!如果你有更多的疑问,请告诉我。如果您认为这回答了您的问题,请单击我答案左侧的复选标记。

尽管当前的答案提供了一个很好的解决方法,但它并没有真正解释问题中的代码是如何工作的:

def count_past_events(series, time_window='6H'):
    series = pd.Series(series.index, index=series)
    past_events = series.rolling(time_window).count() - 1
    return past_events
让我们从另一个问题创建一个玩具系列:

import pandas as pd
input_serie = pd.Series(["1/1/2016 12:00:20 AM",
                         "1/2/2016 5:03:20 AM",
                         "1/2/2016 5:06:20 AM",
                         "1/2/2016 5:07:20 AM",
                         "1/2/2016 6:06:20 AM",
                         "1/3/2016 00:00:20 AM"],
                         name='date',
                         dtype = 'datetime64[ns]'
                       ) 
它看起来像这样:

0   2016-01-01 00:00:20
1   2016-01-02 05:03:20
2   2016-01-02 05:06:20
3   2016-01-02 05:07:20
4   2016-01-02 06:06:20
5   2016-01-03 00:00:20
Name: date, dtype: datetime64[ns]
date
2016-01-01 00:00:20    0
2016-01-02 05:03:20    1
2016-01-02 05:06:20    2
2016-01-02 05:07:20    3
2016-01-02 06:06:20    4
2016-01-03 00:00:20    5
dtype: int64
date
2016-01-01 00:00:20    0.0 --> No event before
2016-01-02 05:03:20    0.0 --> first event of the day
2016-01-02 05:06:20    1.0 --> one event in the past 6 h
2016-01-02 05:07:20    2.0 --> two events in the past 6 h
2016-01-02 06:06:20    3.0 --> three events in the past 6 h
2016-01-03 00:00:20    0.0 --> next day, no event in the past 6 h
dtype: float64
现在我们可以一行一行地打断它:

series = pd.Series(series.index, index=series)
给定一个输入
Series
对象,您可以使用输入序列的值作为新序列的索引来构建一个新序列。这是因为默认情况下,滚动窗口应用于索引

系列
如下所示:

0   2016-01-01 00:00:20
1   2016-01-02 05:03:20
2   2016-01-02 05:06:20
3   2016-01-02 05:07:20
4   2016-01-02 06:06:20
5   2016-01-03 00:00:20
Name: date, dtype: datetime64[ns]
date
2016-01-01 00:00:20    0
2016-01-02 05:03:20    1
2016-01-02 05:06:20    2
2016-01-02 05:07:20    3
2016-01-02 06:06:20    4
2016-01-03 00:00:20    5
dtype: int64
date
2016-01-01 00:00:20    0.0 --> No event before
2016-01-02 05:03:20    0.0 --> first event of the day
2016-01-02 05:06:20    1.0 --> one event in the past 6 h
2016-01-02 05:07:20    2.0 --> two events in the past 6 h
2016-01-02 06:06:20    3.0 --> three events in the past 6 h
2016-01-03 00:00:20    0.0 --> next day, no event in the past 6 h
dtype: float64
转到下一行:

past_events = series.rolling(time_window).count() - 1
rolling
函数是应用于数据帧/序列的聚合,它只考虑有限的行数或时间帧(在您的情况下)。 既然您的系列索引是一个日期时间,您可以在其上应用
rolling
,使用您选择的
time\u窗口(在您的情况下为6小时),并且您需要该时间段内事件的
count()
。'-1'在这里只是为了避免计算当前事件,而只计算过去的事件

返回的结果如下所示:

0   2016-01-01 00:00:20
1   2016-01-02 05:03:20
2   2016-01-02 05:06:20
3   2016-01-02 05:07:20
4   2016-01-02 06:06:20
5   2016-01-03 00:00:20
Name: date, dtype: datetime64[ns]
date
2016-01-01 00:00:20    0
2016-01-02 05:03:20    1
2016-01-02 05:06:20    2
2016-01-02 05:07:20    3
2016-01-02 06:06:20    4
2016-01-03 00:00:20    5
dtype: int64
date
2016-01-01 00:00:20    0.0 --> No event before
2016-01-02 05:03:20    0.0 --> first event of the day
2016-01-02 05:06:20    1.0 --> one event in the past 6 h
2016-01-02 05:07:20    2.0 --> two events in the past 6 h
2016-01-02 06:06:20    3.0 --> three events in the past 6 h
2016-01-03 00:00:20    0.0 --> next day, no event in the past 6 h
dtype: float64

我希望您能更清楚地理解。

请在您的问题中添加详细信息。你到底不明白什么?很好的解释!但是滚动法不接受“6H”作为参数,对吗?这就是为什么我需要做这个变通方法。是的,如果索引是datetime类型,它需要:)。您可以指定所有类型的时间间隔,如天、秒……您的答案应该是正确的。我还以为密码根本没用呢。