Programming/Python

Python Numpy 1강 - 배열

상맹 2021. 10. 20. 15:01
반응형

1. dtype 종류

<공식 문서>

 

Data type objects (dtype) — NumPy v1.21 Manual

The generic hierarchical type objects convert to corresponding type objects according to the associations: Deprecated since version 1.19: This conversion of generic scalar types is deprecated. This is because it can be unexpected in a context such as arr.a

numpy.org

 

<배열 데이터타입 설명>

 

[파이썬 numpy] 배열 데이터타입 종류/정의/확인

[파이썬 numpy] 배열 데이터타입 종류/정의/확인 numpy 데이터타입 종류 데이터 종류는 크게 숫자형, 문자형, 논리형(부울형), 날짜시간형으로 나뉩니다. 파이썬에서 문자형은 string 입니다. (R과 다

pybasall.tistory.com

Basic Type Available Numpy Types
Boolean bool
Integer int8, int16, int32, int 64, int 128, int
Unsigned Integer uint8, uint16, uint32, uint64, uint128, uint
Float float32, float64, float, longfloat
Complex complex64, complex128, complex
Strings str, unicode
Object object
Records void

* # + 부호만 사용 시 → unit 사용


 Google Colab을 이용한 코딩 테스트

 

Google Colaboratory

 

colab.research.google.com


# 파이썬 문법 테스트

import numpy as np

temp1 = [1,3,5]
print(type(temp1))

temp2 = np.array(temp1)
print(type(temp2))

print("*"*50)

print(temp1)
print(temp2)


# pip install numpy
# pyhon -m pip3 install numpy

# numpy.py 파일이 있는것을 import 하겠다.
import numpy as np

temp1 = [1,2,3,4] # 파이썬 list
arr1 = np.array(temp1, dtype=np.uint32) 

print(arr1)
print(type(arr1))
print(arr1.shape )


temp2 = 2 # 스칼라(방향이 없는 데이터)
arr2 = np.array(temp2)
print(arr2)
print(type(arr2))
print(arr2.shape)


temp3 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
arr3 = np.array(temp3, dtype=np.uint0)
print(arr3)
print(arr3.shape)
print(arr3.size)


temp4 = [
    [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],
    [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],
    [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
]
arr4 = np.array(temp4, dtype=np.uint0)
print(arr4)
print(arr4.shape)
print(arr4.size)


temp5 = [
         [1,2,3,4],
         [1,2,5,8]
]

matrix1 = np.array(temp5)
print(matrix1.shape)

re1 = matrix1.reshape((8,)) # 8 * 1
print(re1)
print("*"*50)

re2 = matrix1.reshape((4,2)) # 4 * 2
print(re2)
print("*"*50)

re3 = matrix1.reshape((2,2,2)) # 2 * 2 * 2
print(re3)
print("*"*50)

re4 = matrix1.reshape((1,1,4,2))
print(re4)
print(re4.shape)


temp6 = [1,2,3,4,5,6]
vector1 = np.array(temp6)
print(vector1)
re1 = vector1.reshape((2, -1)) # -1은 앞에 값을 보고 동적으로 계산된다
print(re1)

re2 = re1.reshape((-1)) # flatten, flatMap 흩뿌리다
print(re2)

re3 = re2.flatten()
print(re3)

Flatten - 요소들을 하나씩 끄집어내서 흩뿌린다 => 전개


<알고리즘&데이터구조 Array 설명 잘되어있는 영상!>

 

반응형

'Programming > Python' 카테고리의 다른 글

Python Numpy 3강 - arange, zeros, ones  (0) 2021.10.20
Python Numpy 2강 - Slicing  (0) 2021.10.20
Python 8강 - 데이터 크롤링  (0) 2021.10.06
Python 7강 - HTML Parsing  (0) 2021.09.22
Python 6강 - Crawling  (0) 2021.09.20