首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将时间序列与Sklearn光学算法结合使用?

如何将时间序列与Sklearn光学算法结合使用?
EN

Stack Overflow用户
提问于 2022-02-02 12:24:58
回答 1查看 153关注 0票数 0

我在试着把时间序列聚在一起。我也想使用Sklearn光学。在文档中,它说输入向量X应该有维度(n_samples,n_features)。我的数组位于表单上(n_samples、n_time_stamps、n_features)。代码中的示例。

我的问题是如何利用光学中的拟合函数和时间序列。我知道人们使用光学和DBSCAN的时间序列。我只是不知道他们是怎么实现的。任何帮助都将不胜感激。

代码语言:javascript
运行
复制
[[[t00, x0], [t01, x01], ... [t0_n_timestamps, x0_n_timestamps]], 
 [[t10, x10], [t11, x11], ... [t1_n_timestamps, x1_n_timestamps]], 
.
.
.
 [[t_n_samples_0, x_n_samples_0], [[t_n_samples_1, x_n_samples_1], ... [t_n_samples_n_timestamps, x_n_samples_n_timestamps]]]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-02 14:40:12

给定以下np.array作为输入:

代码语言:javascript
运行
复制
data = np.array([
    [["00:00", 7], ["00:01", 37], ["00:02", 3]],
    [["00:00", 27], ["00:01", 137], ["00:02", 33]],
    [["00:00", 14], ["00:01", 17], ["00:02", 12]],
    [["00:00", 15], ["00:01", 123], ["00:02", 11]],
    [["00:00", 16], ["00:01", 12], ["00:02", 92]],
    [["00:00", 17], ["00:01", 23], ["00:02", 22]],
    [["00:00", 18], ["00:01", 23], ["00:02", 112]],
    [["00:00", 100], ["00:01", 200], ["00:02", 301]],
    [["00:00", 101], ["00:01", 201], ["00:02", 302]],
    [["00:00", 102], ["00:01", 203], ["00:02", 303]],
    [["00:00", 104], ["00:01", 207], ["00:02", 304]]])

我将按以下方式进行:

代码语言:javascript
运行
复制
    # save shape info in three separate variables
    x, y, z = data.shape
    # idea from https://stackoverflow.com/a/36235454/5050691
    output_arr = np.column_stack((np.repeat(np.arange(x), y), data.reshape(x * y, -1)))
    # create a df out of the arr
    df = pd.DataFrame(output_arr)
    # rename for understandability
    df = df.rename(columns={0: 'index', 1: 'time', 2: 'value'})
    # Change the orientation between rows and columns so that rows
    # that contain time info become columns
    df = df.pivot(index="index", columns="time", values="value")
    df.rename_axis(None, axis=1).reset_index()
    # get columns that refer to specific interval of time series
    temporal_accessors = ["00:00", "00:01", "00:02"]
    # extract data that will be used to carry out clustering
    data_for_clustering = df[temporal_accessors].to_numpy()

    # a set of exemplary params
    params = {
        "xi": 0.05,
        "metric": "euclidean",
        "min_samples": 3
    }
    clusterer = OPTICS(**params)
    fitted = clusterer.fit(data_for_clustering)
    cluster_labels = fitted.labels_
    df["cluster"] = cluster_labels
    # Note: density based algortihms have a notion of the "noise-cluster", which is marked with
    # -1 by sklearn algorithms. That's why starting index is -1 for density based clustering,
    # and 0 otherwise.

对于给定的数据和参数的选择,您将得到以下集群:0 0 1 0 0 0 1 1 1

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70955592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档