Numpy

1. 생성

A Visual Intro to NumPy and Data Representation

Load & Save

In [2]: np.savetxt('test1.txt', a, fmt='%d') # fmt='%d' for 3D
In [3]: b = np.loadtxt('test1.txt', dtype=int)

In [5]: a.tofile('test2.dat')
In [6]: c = np.fromfile('test2.dat', dtype=int)


In  [8]: np.save('test3.npy', a)    # .npy extension is added if not given
In  [9]: d = np.load('test3.npy')

새 col 추가

a = np.array([[1,2,3],[2,3,4],[1,2,3],[1,2,3]])
z = np.zeros((4,1))

np.concatenate((a, z), axis=1)
np.append(B, z, axis=1)
np.column_stack([a, z])
np.hstack([a, z])


"""
 array([[1., 2., 3., 0.],
        [2., 3., 4., 0.],
        [1., 2., 3., 0.],
        [1., 2., 3., 0.]])
 """

새 row 추가


a = np.array([[1,2,3],[2,3,4],[1,2,3],[1,2,3]])
z = np.zeros((1,3))

np.concatenate((a, z), axis=0)
np.append(a, z, axis=0)
np.row_stack([a, z])
np.vstack([a, z])

"""
array([[1., 2., 3.],
       [2., 3., 4.],
       [1., 2., 3.],
       [1., 2., 3.],
       [0., 0., 0.]])
 """

새 Dim 추가


a = np.array([[0,0,0],[0,0,0],[0,0,0]])  #3,3
b = np.array([[1,1,1],[1,1,1],[1,1,1]])  #3,3

c = np.stack([a,b]) #2,3,3

"""
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]]])
"""

a = np.array([[0,0,0],[0,0,0],[0,0,0]])
a.shape #(3, 3)

c = np.expand_dims(a, axis=0) #(1, 3, 3)
c = np.expand_dims(a, axis=1) #(3, 1, 3)
c = np.expand_dims(a, axis=2) #(3, 3, 1)
data = np.loadtxt(file_list[0])
data = np.expand_dims(data, axis=0)
batch_data = np.full([1, 1024, 3], 1, dtype=float)#np.nan)
batch_data[0,0:data.shape[1],0:3] = data
data = batch_data 


for i in range(len(file_list)-1):
    batch_data = np.full([1, 1024, 3], 1, dtype=float)#np.nan)
    tmp= np.loadtxt(file_list[i])

    tmp = np.expand_dims(tmp, axis=0)
    batch_data[0,0:tmp.shape[1],0:3] = tmp
    data = np.append(data, batch_data, 0)

batch 데이터 생성

batch_size = 32
num_point = 1024
num_channel = 3
num_classes = 1

data_list = glob.glob('./mobiltech/*.txt')

batch_image = np.zeros((batch_size, num_point, num_channel))
batch_label = np.zeros((batch_size, num_classes))

data_list = glob.glob('./mobiltech/*.txt')

    batch_per_epoch =  len(data_list) //BATCH_SIZE 



    for batch_n in range(batch_per_epoch):

        batch_data = data_list[batch_n * BATCH_SIZE : (batch_n + 1) * BATCH_SIZE]

        for n, path in enumerate(data_list[:BATCH_SIZE]):
            #print(n, path)
            image = np.loadtxt(path)
            cur_batch_data[n,0:image.shape[0],0:3] = image

            batch_data, batch_label = cur_batch_data, cur_batch_label

            sess.run(....)
#https://medium.com/trackin-datalabs/data-input-%EB%A7%8C%EB%93%A4%EA%B8%B0-74bb5c1ce52f

Indexing

조건을 만족(라벨별로) 하는 값만 뽑아 오기

label1 = concate[concate[:,3]==1]

마지막 열 제거 xyzRGB -< XYG

label1 = np.delete(label1, (3), axis=1)

해당 포인트 제거하기

extract = concate[concate[:,3] != 1]

update arra

z = np.zeros(shape=(1, 10, 4))
k = np.ones(shape=(5, 3))
z[0,0:k.shape[0],0:3] = k

"""
array([[[1., 1., 1., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]])

"""

#Slicing

nums = range(5) # range is a built-in function that creates a list of integers
print nums      #prints "[0,1,2,3,4]"
print nums[2:4] #prints "[2,3]"
print nums[2:]  #pirnts "[2,3,4]"
print nums[:2]  #prints "[0,1]"
print nums[:]   #prints "[0,1,2,3,4]"
print nums[:-1] #prints "[0,1,2,3]"
nums[2:4] = [8,9] #Assign a new sublist to a slice
print nums      #prints "[0,1,8,9,4]"

#Indexing, Slicing, Iterating

import numpy as np

a = np.array([1,2,3,4,5])
# array([1, 2, 3, 4, 5])

a[1:3]
# array([2, 3])

a[-1]
# 5

a[0:2] = 9

a   #array([9, 9, 3, 4, 5])

#################################

b = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
# array([ 1, 2, 3, 4],
#       [ 5, 6, 7, 8],
#       [ 9, 10, 11, 12]])

b[:, 1]
# array ([ 2, 6, 10])

b[-1]
# array([9, 10, 11, 12])

b[-1, :]
# array([9,10,11,12])

b[0:2, :]
#array ([[1,2,3,4],
#       [5,6,7,8]])

results matching ""

    No results matching ""