首页 > 解决方案 > 如何使用 .query() 将数据集过滤到一定数量的行

问题描述

所以,我开始进行拓扑数据分析。我刚刚开始学习,我对 numpy、pandas、openml 或以下代码中使用的任何东西没有太多经验:

from openml.datasets import get_dataset
banana = get_dataset('shapes').get_data(dataset_format='dataframe')[0]
import numpy as np
data = np.asarray(
 [
     banana.query("target == @shape")[["x", "y", "z"]].values
     for shape in banana["target"].unique()
 ])

问题是,我不知道如何为其他数据集复制这段代码。或者更确切地说,我不知道为什么它不适用于其他数据集。“形状”数据集(https://www.openml.org/d/42710)有 4 列,x,y,z,target 和 16000~ 行。这给出了一个 numpy 数组作为输出,其中包含以下条目:

[[0.341007 0.318606 0.096725]...[0.35871  0.751543 0.102706]]

和形状 (40,400,3) (这正是我想要的,3 个变量的点云列表)。

对于 'volcanoes-a1' 数据集(https://www.openml.org/d/1527),我已经尝试过,这是我发现的第一个与形状数据集远程相似的数据集。我可以将四个特征(V1、V2、V3、Class)分组(按类别)作为三个变量中的点云(V1、V2、V3 都是数字特征):

from openml.datasets import get_dataset
volcanoes = get_dataset('volcanoes-a1').get_data(dataset_format='dataframe')[0]
import numpy as np
data =np.asarray(
[
    volcanoes.query("Class == @type")[["V1", "V2", "V3"]].values
    for type in np.unique(volcanoes["Class"])
],dtype=float)

但是这次我得到的输出是一个 numpy 数组,其中包含如下条目:

array([[2.78000e+02, 1.60000e+01, 3.84472e-01],
        [3.86000e+02, 1.70000e+01, 3.87792e-01],
        [4.11000e+02, 1.90000e+01, 4.36546e-01],
        ...,
        [7.46000e+02, 1.00100e+03, 4.29247e-01],
        [5.16000e+02, 1.00000e+03, 4.08470e-01],
        [9.24000e+02, 1.00200e+03, 4.21968e-01]])

和形状(5,)(这没有意义,它必须类似于(5,?,3)。3252是数据集的总行数,据说它刚刚被类过滤(5类) .

我真的很困惑。我应该怎么办?我觉得它将“数组”视为字符串而不是实际数组(我也猜这是因为科学记数法),因此,我不能将它用作点云数组来计算 Vietoris-Rips复杂的。

PS:如果我的英语不好,我深表歉意,如果问题有一个非常简单的解决方案,我深表歉意。PS2:忽略“香蕉”变量。我尝试了许多数据集,但无法在其中任何一个上工作,香蕉就是其中之一。

编辑:我错过了火山上的 unique() 。不过,还是不行。

编辑 2:在 np.asarray() 上添加了 dtype = float 并得到了这个错误:

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (5,) + inhomogeneous part.

这也没有意义,因为数据集没有缺失值,如果我理解代码实际做了什么,它应该是一个多维数组。

这也没有意义,因为这段代码:

data = np.asarray(
    [
        volcanoes.query("Class == '1'")[["V1","V2","V3"]].values
    ])

为 "Class" 的所有五个值给出正确的格式和形状:

[[[2.78000e+02 1.60000e+01 3.84472e-01]
  [3.86000e+02 1.70000e+01 3.87792e-01]
  [4.11000e+02 1.90000e+01 4.36546e-01]
  ...
  [7.46000e+02 1.00100e+03 4.29247e-01]
  [5.16000e+02 1.00000e+03 4.08470e-01]
  [9.24000e+02 1.00200e+03 4.21968e-01]]]
(1, 2952, 3)

我尝试了五次 np.append() 作为破解它的方法(没有解决),但这似乎并没有达到我想要的效果。我尝试使用 ['1','2','3','4','5'] 而不是 np.unique(volcanoes["Class"]) ,但这给出了与开始时相同的结果。如果您不想检查数据集以了解它的情况,那么这就是数据集。

         V1      V2        V3 Class
0     291.0   802.0  0.532117     2
1     234.0   657.0  0.622671     2
2     332.0   697.0  0.445459     2
3     352.0   772.0  0.524428     2
4      41.0   808.0  0.750236     2
...     ...     ...       ...   ...
3247  402.0   999.0  0.461003     1
3248  682.0   996.0  0.358702     1
3249  746.0  1001.0  0.429247     1
3250  516.0  1000.0  0.408470     1
3251  924.0  1002.0  0.421968     1

编辑3:我修好了。排序。

from openml.datasets import get_dataset
volcanoes = get_dataset('volcanoes-a1').get_data(dataset_format='dataframe')[0]
import numpy as np
print(volcanoes)
data= np.asarray([
      volcanoes.query("Class == @tp").sample(58)[["V1","V2","V3"]].values
      for tp in np.unique(volcanoes["Class"])#58 is the number of elements on the class with the least elements to have consistent shape
      ])
print(data.shape)

标签: pythonpandasnumpydatasetopenml

解决方案


推荐阅读