【问题标题】:remove list from numpy从 numpy 中删除列表
【发布时间】:2018-03-30 04:50:36
【问题描述】:

我有一个名为genes 的numpy 数组,由于某种原因它有标识符列表。我将如何继续删除列表部分并简单地保留元素。这是基因的输出:

array([[list(['0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0'])], 
       [list(['0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '1', '0', '0', '0', '1', '0', '0', '0'])]
      ], dtype=object)

如果没有列表,所需的输出将完全一样。其余的将保持不变:

array([['0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0'], 
       ['0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '1', '0', '0', '0', '1', '0', '0', '0']
      ], dtype=object)

这是我得到 numpy 数组的方法:

import pandas as pd
import numpy as np

np.set_printoptions(threshold=np.nan)


url = "https://archive.ics.uci.edu/ml/machine-learning-databases/molecular-biology/splice-junction-gene-sequences/splice.data"
file = pd.read_csv(url, delimiter=',', header=None,dtype='str')

X = file[2]
y = file[0]

df = np.array(X)
newDf = df.astype(str)

newerDf = newDf.reshape(( newDf.size, 1))

newestDf = np.char.strip(newerDf)

genes = np.char.array(newestDf)

genes = np.char.replace(genes, 'A', '0001')
genes = np.char.replace(genes, 'C', '0010')
genes = np.char.replace(genes, 'G', '0100')
genes = np.char.replace(genes, 'T', '1000')
genes = np.char.join(',', genes)
genes = np.char.split(genes, ',')

genes

【问题讨论】:

  • 数据从何而来?
  • 只需运行命令np.char.array(genes)。您最终会在倒数第二个轴上获得一个额外的维度,之后您可以通过reshape 将其删除。
  • @droooze 它给了我以下错误:使用序列设置数组元素
  • @StephenRauch 我更新了最初的帖子,以展示我是如何获取和塑造数据的

标签: numpy jupyter-notebook


【解决方案1】:

我最近看到过这样的事情。通常,创建具有匹配大小的列表元素的对象 dtype 数组并不容易。例如,第一行的复制粘贴会产生一个(2, 1, 240) 形状数组。但是那个案例和你的一样,使用了char.split

how to convert a np array of lists to a np array

一个更简单的例子:

In [138]: x = np.array(['abc def','hij klm'])
In [139]: np.char.split(x)
Out[139]: array([list(['abc', 'def']), list(['hij', 'klm'])], dtype=object)
In [140]: x = np.array(['abc def','hij klm'])
In [141]: y = np.char.split(x)
In [142]: y
Out[142]: array([list(['abc', 'def']), list(['hij', 'klm'])], dtype=object)
In [143]: np.stack(y)
Out[143]: 
array([['abc', 'def'],
       ['hij', 'klm']], dtype='<U3')
In [144]: _.shape
Out[144]: (2, 2)

更仔细地观察你的显示器,我看到了另一个维度,更像这样:

In [145]: x = np.array([['abc def'],['hij klm']])
In [146]: x.shape
Out[146]: (2, 1)
In [147]: x
Out[147]: 
array([['abc def'],
       ['hij klm']], dtype='<U7')
In [148]: y = np.char.split(x)
In [149]: y
Out[149]: 
array([[list(['abc', 'def'])],
       [list(['hij', 'klm'])]], dtype=object)
In [150]: y.shape
Out[150]: (2, 1)

np.stack 没有帮助,但如果我们先将 y 展平,它就可以工作:

In [152]: np.stack(y.ravel())
Out[152]: 
array([['abc', 'def'],
       ['hij', 'klm']], dtype='<U3')

【讨论】:

  • 用我的代码尝试 np.stack(genes.ravel()) ,它给了我以下错误:所有输入数组必须具有相同的形状。我正在仔细检查数据集,但它们的长度似乎都是 60
【解决方案2】:

您可以通过tolist:

>>> A
array([[list(['0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0'])],
       [list(['0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '1', '0', '0', '0', '1', '0', '0', '0'])]],
      dtype=object)
>>> 
>>> np.array(A.tolist(), object)
array([[['0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1',
         '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0',
         '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1',
         '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1',
         '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0',
         '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0',
         '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0',
         '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0',
         '0', '1', '0', '0', '0', '0', '0', '1', '0', '1', '0', '0',
         '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0',
         '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1', '0',
         '1', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0',
         '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0',
         '0', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0',
         '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '1', '0',
         '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0', '0',
         '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '0', '1',
         '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0',
         '0', '0', '0', '1', '0', '1', '0', '0', '1', '0', '0', '0',
         '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0']],

       [['0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0',
         '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0',
         '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0',
         '0', '0', '0', '1', '1', '0', '0', '0', '0', '0', '1', '0',
         '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '1',
         '0', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', '0',
         '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0',
         '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0',
         '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0',
         '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '0',
         '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0',
         '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0',
         '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '0',
         '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '1', '0',
         '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0',
         '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '1', '0',
         '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1',
         '0', '0', '0', '1', '0', '1', '0', '0', '1', '0', '0', '0',
         '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '1', '0',
         '0', '0', '0', '1', '1', '0', '0', '0', '1', '0', '0', '0']]],
      dtype=object)

您可能想要压缩结果,因为存在多余的尺寸(形状为 (2, 1, 240)。您想要的输出看起来像 (2, 240)

【讨论】:

  • 我尝试运行它,它仍然显示列表标识符。如果您尝试使用我的代码,您可能会得到一个更好的主意,因为也许我忽略了一些东西
  • @ankit 这是因为列表的长度不同。试试np.where(np.vectorize(len)(genes) != 240)
  • @ankit 深入挖掘newestDf 中有意想不到的字符:np.unique(newestDf.view('U1')) 给出array(['', 'A', 'C', 'D', 'G', 'N', 'R', 'S', 'T'], dtype='&lt;U1')
  • 在尝试查找长度不是 240 的行时,我遇到了意外的值,因此我假设您是正确的,并且由于某种原因存在意外字符。我走得更远,发现这些值被添加到数组 df 中。但是,我仍然找不到源,因为到目前为止,我只在 url 数据中找到了 ACGT
猜你喜欢
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 2015-03-01
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多