0

I want to get the row that the third number is 0 or 1.

Here is data.txt:

34.62365962451697,78.0246928153624,0
30.28671076822607,43.89499752400101,0
35.84740876993872,72.90219802708364,0
60.18259938620976,86.30855209546826,1
79.0327360507101,75.3443764369103,1

After I load the .txt to numpy array:

data_np=np.loadtxt("ex2data1.txt", delimiter=',')

How can i do?

bharatk
  • 4,202
  • 5
  • 16
  • 30
zhan
  • 85
  • 1
  • 1
  • 9
  • 1
    Possible duplicate of [*Numpy array, how to select indices satisfying multiple conditions?*](https://stackoverflow.com/questions/3030480/numpy-array-how-to-select-indices-satisfying-multiple-conditions) – Alexandre B. Aug 09 '19 at 10:46

3 Answers3

1

Use boolean indexing and or the two conditions together:

rows_to_keep = data_np[data_np[:,2] == 0 | data_np[:,2] == 1]
GPhilo
  • 18,519
  • 9
  • 63
  • 89
0

Use usecols

Ex.

import numpy as np

data_np=np.loadtxt("ab.text", delimiter=',', usecols=(2))
print(data_np)

O/P:

[0. 0. 0. 1. 1.]

OR

Filter row that the third number is 0 or 1.

import numpy as np

def filter_lines(f):
    for i, line in enumerate(f):
        t_n = line.split(",")[2][0].strip()
        if t_n == '0' or t_n == '1':
            yield line

with open("ab.text") as f:
    data_np=np.loadtxt(filter_lines(f), delimiter=',')
    print(data_np)

O/P

[[34.62365962 78.02469282  0.        ]
 [30.28671077 43.89499752  0.        ]
 [35.84740877 72.90219803  0.        ]
 [60.18259939 86.3085521   1.        ]
 [79.03273605 75.34437644  1.        ]
 [99.03273605 95.34437644  1.        ]]

ab.text file

34.62365962451697,78.0246928153624,0
30.28671076822607,43.89499752400101,0
35.84740876993872,72.90219802708364,0
60.18259938620976,86.30855209546826,1
79.0327360507101,75.3443764369103,1
82.0327360507101,76.3443764369103,2
89.0327360507101,75.3443764369103,3
99.0327360507101,95.3443764369103,1
bharatk
  • 4,202
  • 5
  • 16
  • 30
0

You can try this:

    new_data_np = []
    for i in range(data_np.shape[0]):
        if data_np[i,2]==0 or data_np[i,2]==1:
            print(data_np[i,:])
            #store data_np[i,:]
            new_data_np.append(data_np[i,:])
Niaz Palak
  • 175
  • 1
  • 13