Note

Numpy (27) 본문

Numpy

Numpy (27)

알 수 없는 사용자 2022. 8. 19. 19:00
728x90

How to convert a 1d array of tuples to a 2d numpy array?

# Input:
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_1d = np.genfromtxt(url, delimiter=',', dtype=None)

# Solution
# 1: Convert each row to a list and get the first 4 items
iris_2d = np.array([row.tolist()[:4] for row in iris_1d])
iris_2d[:4]

# 2: Import only the first 4 columns from source url
iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])
iris_2d[:4]

# output
array([[ 5.1,  3.5,  1.4,  0.2],
       [ 4.9,  3. ,  1.4,  0.2],
       [ 4.7,  3.2,  1.3,  0.2],
       [ 4.6,  3.1,  1.5,  0.2]])

'Numpy' 카테고리의 다른 글

Numpy (29)  (0) 2022.08.21
Numpy (28)  (0) 2022.08.20
Numpy (26)  (0) 2022.08.17
Numpy (25)  (0) 2022.08.16
Numpy (24)  (0) 2022.08.15
Comments