当内部值等于?

时间:2017-12-15 12:00:38

标签: python numpy

我的ndarray矩阵是X,尺寸(2536,72)是对象类型。我想将类型更改为float 64,但在我必须删除包含?。

的行之前

我使用过这段代码,但它不起作用 - >

import numpy as np
X1=np.delete(X,'?',axis=0)

__main__:2: DeprecationWarning: using a non-integer array as obj in delete will result in an error in the future
Traceback (most recent call last):

  File "<ipython-input-89-34091147b19c>", line 2, in <module>
    X1=np.delete(X,'?',axis=0)

  File "/anaconda3/envs/py36/lib/python3.6/site-packages/numpy/lib/function_base.py", line 4865, in delete
    obj = obj.astype(intp)

ValueError: invalid literal for int() with base 10: '?'

有任何建议吗?

3 个答案:

答案 0 :(得分:0)

numpy.delete仅适用于数字,因此X1=np.delete(X,'?',axis=0)无效(?不是数字)。你必须使用python list方法迭代你的第一个矩阵,创建一个新列表,然后将其转换为numpy float数组。这样的事情(不完全是):

x1 = []
for l in x:
    if not l.__contains__('?'):
        x1.append(l)

答案 1 :(得分:0)

试试这段代码:

rows_to_delete = []                  # a list of all rows to delete
for i in range(0, 3536):             # looping through rows
    for j in range(0, 72):           # scanning all columns in each row
        if (X[i][j] == '?'):
            rows_to_delete.append(i)
            break

for i in range(len(rows_to_be_deleted)):    # deleting unwanted lines 
    numpy.delete(X, (i), axis = 0)

答案 2 :(得分:0)

rows_to_delete = []#要删除的所有行的列表 对于范围内的i(0,2536):#循环遍历行     对于范围内的j(0,72):#扫描每行中的所有列         if(X [i] [j] ==&#39;?&#39;):             rows_to_delete.append(ⅰ)             破

表示范围内的i(len(rows_to_delete)):#删除不需要的行     np.delete(X,(i),axis = 0) X1 = np.delete(X,rows_to_delete,axis = 0)#创建带有不需要的行的新矩阵