首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSON列表中的Dataframe行,用于使用scikit训练ML

JSON列表中的Dataframe行,用于使用scikit训练ML
EN

Stack Overflow用户
提问于 2020-10-22 22:34:24
回答 1查看 144关注 0票数 2

我试图用斯考尔对一组作为实验组织的JSON文件进行多元分类。

输入的结构如下:

代码语言:javascript
复制
[
  { v: 431, t: 2, d1: 986000, d2: 434000, X: 0 },
  { v: 77, t: 0, d1: 47000, d2: 613000, X: 0 },
  { v: 58, t: 1, d1: 197000, d2: 47000, X: 0 },
  { v: 77, t: 0, d1: 260000, d2: 213000, X: 0 }
]

用于分类的标签被设置为具有形状的DataFrame (len(文件),1)。下面是我的六个文件的实现。X的结果形状是(9528,5),应该是六行,每一行都包含文件的JSON:

代码语言:javascript
复制
import json
import pandas as pd
import numpy as np
from pandas import json_normalize
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sktime.classification.compose import ColumnEnsembleClassifier
from sktime.classification.compose import TimeSeriesForestClassifier
from sktime.classification.dictionary_based import BOSSEnsemble
# from sktime.classification.shapelet_based import MrSEQLClassifier
from sktime.datasets import load_basic_motions
from sktime.transformers.series_as_features.compose import ColumnConcatenator
from sklearn.model_selection import train_test_split


controls = [
    '_clean_control01.json',
    '_clean_control02.json',
    '_clean_control03.json',
]

exp = [
    '_clean_exp01.json',
    '_clean_exp02.json',
    '_clean_exp03.json',
]

testsets = {
    'control': controls,
    'exp': exp
}

map_experiments = {
    'control': 0,
    'exp': 1
}

normalized_data = {
    'control': [],
    'exp': []
}

experiments = pd.DataFrame()
labels = {'exp': []}

for experiment in testsets:
    files = testsets[experiment]
    arr = normalized_data[experiment]
    for file in files:
        tmp = pd.read_json(file)
        experiments = experiments.append(tmp, ignore_index=True)
        label = map_experiments[experiment]
        labels['exp'].append(label)

labels = pd.DataFrame(labels)

X, y = experiments, labels
X_train, X_test, y_train, y_test = train_test_split(
    X, y, shuffle=False, stratify=None)
print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)
print(X_train.head())
np.unique(y_train)

clf = ColumnEnsembleClassifier(estimators=[
    ("TSF0", TimeSeriesForestClassifier(n_estimators=100), [0]),
    ("BOSSEnsemble3", BOSSEnsemble(max_ensemble_size=5), [3]),
])
clf.fit(X_train, y_train)
print(clf.score(X_test, y_test))

我很难找到如何构建数据格式的信息,其中每一行表示编码或未编码的JSON或CSV的列表,或者表示没有时间戳的时间序列的其他对象。我看到了一些示例,其中JSON被编码为数字键,而其他示例则有字符串。到目前为止,我找不到任何东西可以帮助我在一系列文件上使用这些列表构建数据框架。

EN

回答 1

Stack Overflow用户

发布于 2020-10-23 17:22:56

原来,我在DataFrame中寻找嵌套的ndarray,如下所示:

代码语言:javascript
复制
    experiments = pd.DataFrame(['exp'])
    for file in files:
        tmp = pd.read_json(file).to_numpy()
        experiments = experiments.append({'exp': tmp}, ignore_index=True)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64491443

复制
相关文章

相似问题

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