Numpy 배열에 열을 추가하는 Python 예제코드입니다. 2021. 12. 1 - 최초작성 import numpy as np array = np.array([[1,2], [3,4], [5,6]]) print(array) # 3 x 2 배열입니다. # [[1 2] # [3 4] # [5 6]] print() # 기존 배열에 3 x 1 배열을 결합합니다. array2 = np.append(array, [[1],[1],[1]], axis = 1) print(array2) # 3 x 3 배열이 되었습니다. # [[1 2 1] # [3 4 1] # [5 6 1]] 원본 코드 - https://www.delftstack.com/ko/howto/numpy/numpy-add-column/
Numpy 배열에서 0이 아닌 최소값을 찾는 예제입니다. 2021. 12. 1 최초작성 # -*- coding: utf-8 -*- import numpy as np import numpy.ma as ma a = np.array([-1, 0, 1, 2, 3, 4, 5]) print(a) min_value = np.min(ma.masked_where(a == 0, a)) print( "0이 아닌 최소값 :", min_value ) # the position/index of non-zero minimum value in the array min_value_idx = np.argmin(ma.masked_where(a == 0, a)) print( "0이 아닌 최소값의 인덱스 : ", min_value_idx )..

두 곡선의 교차점에 대응하는 x, y좌표를 구할 수 있는 Python 예제 코드입니다. 2021. 11. 30 - 최초작성 실행 결과입니다. 두 곡선의 교점을 초록색 점으로 표시해주고 있습니다. 터미널에서 교차점의 x,y 좌표를 확인할 수 있습니다. [(1.2831976623728205, -0.283620905396323), (4.425039547130342, 0.2818828030413486), (7.566358178278255, -0.281334885867327)] 전체 소스 코드입니다. # -*- coding: utf-8 -*- # 원본 코드 - https://stackoverflow.com/a/59120343 import numpy as np from matplotlib import pyplot ..
Python에서 List에 원소를 추가하는데 걸리는 시간과 Numpy에서 넘파이 배열에 원소를 추가하는데 걸리는 시간을 비교해봤습니다. 예상과 달리 넘파이 배열에 원소를 추가하는 시간이 더 오래 걸립니다. 2021. 11. 29 - 최초작성 Python List에 원소를 추가한 후, 넘파이 배열로 변환하는 방법과 빈 넘파이 배열에 원소를 추가하는 방법 두가지에 대한 코드와 결과입니다. Python의 List에 원소 추가 import numpy as np import time start = time.time() arr = [] for i in range(1000000): arr.append(i) arr = np.array(arr) print(arr.shape) print("list append time :..
Python OpenCV에서 이미지 크기(width, height)를 가져오는 방법입니다. 2021. 11. 14 컬러 이미지의 경우에는 shape 함수를 통해 height, width, channels를 얻을 수 있습니다. import numpy as np import cv2 img = cv2.imread('apple.png', cv2.IMREAD_COLOR) print('img.shape ', img.shape) h, w, c = img.shape print('height ', h) print('width ', w) print('channel ', c) img.shape (618, 641, 3) height 618 width 641 channel 3 흑백 이미지의 경우에는 shape 함수를 통해 he..
Python에서 JSON 포맷 파일을 로드하는 방법입니다. 2021. 11. 14 최초작성 test.json 이름으로 파일을 작성합니다. {"name":"Lee","messages":["msg 1","msg 2","msg 3"],"country":"korea"} 다음 파이썬 코드를 사용하여 json 파일을 로드할 수 있습니다. import json f = open('test.json') json_file = json.load(f) print(json_file) print(type(json_file)) # {'name': 'Lee', 'messages': ['msg 1', 'msg 2', 'msg 3'], 'country': 'korea'} # print(json.dumps(json_file)) print..
MATLAB의 mat 파일을 Python에서 불러오는 예제 코드입니다. 2021. 11. 14 최초작성 import scipy.io as sio arr = sio.loadmat('sample1.mat') print('arr') print(arr) print('\n\n') a = arr['a'] b = arr['b'] print('a') print(a) print('b') print(b) arr = sio.loadmat('sample1.mat') print('arr') print(arr) print('\n\n') loadmat으로 mat 파일을 로드하여 출력해보면 배열 ‘a’와 배열 ‘b’를 딕셔녀리에서 접근가능한걸 볼 수 있습니다. arr {'__header__': b'MATLAB 5.0 MAT-file..
Python에서 실수 출력 포맷을 지정하는 예제 코드입니다. 2021. 11. 10 - 최초작성 소수점 두번째 자리까지 출력하기 pi = 3.14159265359 print("{:.2f}".format(pi)) 소수점 두번째 자리까지 출력되었습니다. 3.14 5개의 문자 출력할 공간에 소수점 두번째 자리까지 실수 출력하기 pi = 3.14159265359 print("{:5.2f}".format(pi)) 소수점 포함해서 출력될 문자 개수가 4개라서(3.14) 앞에 빈 공백이 하나 추가되어 출력되었습니다. 3.14 다른 예제를 하나 더 살펴봅니다. 10개의 문자 출력할 공간에 소수점 다섯번째 자리까지 실수를 출력합니다. pi = 3.14159265359 print("{:10.5f}".format(pi)) ..

웹브라우저에 Hello, World!를 출력하는 Flask 예제코드입니다. Flask의 Quickstart 문서를 보며 진행한 과정을 작성한 글입니다. https://flask.palletsprojects.com/en/2.0.x/quickstart/ 부족한 부분이나 이상한 부분이 있을 수 있습니다. 개발 환경 구축은 다음 포스트를 참고하세요 Windows 10 환경에서 Visual Studio Code와 Miniconda를 사용한 Flask 개발 환경 만들기 https://webnautes.tistory.com/1522 Miniconda를 설치하지 않고 pip install flask 명령으로 해도 상관은 없습니다. 2021. 10. 9 - 최초작성 hello.py from flask import Fla..
CSV 파일을 읽어 순서 유지한채 무작위 샘플링하여 2개의 CSV 파일로 저장하는 예제입니다. Pandas를 사용하여 구현하였습니다. 테스트에 사용한 CSV 파일입니다. 주의할점은 csv 파일에 필드를 설명하는 헤더가 꼭 있어야 합니다. 여기에선 typeA, typeB입니다. typeA, typeB AA1,BB1 AA2,BB2 AA3,BB3 AA4,BB4 AA5,BB5 AA6,BB6 AA7,BB7 AA8,BB8 AA9,BB9 AA10,BB10 AA11,BB11 AA12,BB12 AA13,BB13 AA14,BB14 AA15,BB15 AA16,BB16 AA17,BB17 AA18,BB18 AA19,BB19 AA20,BB20 AA21,BB21 AA22,BB22 AA23,BB23 AA24,BB24 AA25,BB2..