pgAdmin4 GUI installation on MacOS M1

pgAdmin 설치 및 서버 연결 (MacOS)

  • GUI 프로그램을 설치해본다.
  • Windows는 자동으로 설치가 되기 때문에 생략을 한다.
  • 먼저 해당 싸이트에 접속을 합니다.

  • macOS를 클릭한 뒤 다음 화면에서 pgAdmin 4 v5.3 최신버전을 다운로드 받도록 합니다.

  • 프로그램을 설치하면 첫 화면에서 Password를 입력하도록 합니다.

  • 새로운 서버를 생성하여 서버를 등록하도록 합니다. 그 후에 이름은 LocalTest라고 정합니다.

  • 그 후에, username은 postgres를 username으로 입력하고 Postgresql을 설치할 때 설정한 password를 입력합니다.

  • 실제 서버에 연결 되었는지 확인하도록 합니다.

Postgre SQL Installation on Windows 10

Windows

  • 필자는 13.3 버전을 선택하였습니다.

  • 이번에는 프로그램을 클릭하여 설치를 진행합니다.

  • 모든 값은 default로 진행합니다.

  • 비밀번호는 작성 후, 반드시 기억하시기를 바랍니다.

    • 필자는 temp라고 명명하였습니다.

  • 포트는 5432를 확인합니다.

  • 언어는 한국어로 선택하도록 합니다.

  • 몇번의 Next를 더 누르시면서, 설치를 진행합니다.

  • 설치가 완료되면 Stack Builder 체크 박스는 제 후 완료를 합니다. 해

  • 프로그램을 검색하여 PostgreSQL이 잘 설정되는지 확인을 하도록 합니다.

환경변수 추가

  • CMD에서 활용하려면 환경변수를 설정하도록 합니다.
  • 먼저 경로를 복사합니다.

[Python] 이미지 데이터 입출력

1줄 요약

  • OpenCV를 활용한 다양한 이미지 입출력에 대해 배우도록 한다.

Reading/Writing an image file

  • 이미지 관련 I/O
  • BMP, PNG, JPEG, and TIFF also supported.
import numpy as np
img = np.zeros((3, 3), dtype=np.uint8)
img
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]], dtype=uint8)
  • 각 픽셀은 8비트 int로 구성되어 있음.
  • 각 픽셀의 범위는 0-255, 0은 검은색, 255는 흰색을 의미함.
import cv2 
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
img
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]], dtype=uint8)
  • 3차원 배열을 의미. 각 채널은 Blue, Green, Red를 의미한다.

image Load

  • Convert PNG into JPEG
  • 사용할 이미지는 아래와 같다.

Postgre SQL Installation on MacOS M1

1줄 요약

  • MacOS M1에서 PostgreSQL 설치에서 중요한 건 환경변수만 추가한다.

M1의 구조

  • M1애서는 Intel, Silicon, Universal 3개의 시스템을 지원한다.
    • 그런데, PostgreSQL 프로그램은 기본적으로 Intel 기반으로 작동을 한다.

Postgre SQL 다운로드

  • 해당 웹 페이지로 간다. (URL: https://postgresapp.com/)

  • 다운로드 받은 후 Postgres-2.4.3-13.dmg (2021.5.31일 기준) 설치 파일을 클릭한 후, 아래 화면이 나오면, 설치를 진행합니다.

  • 설치 진행이 완료가 되면 아래 화면에서 Initialize 또는 Start 버튼을 클릭하면 설치는 끝이 납니다.

환경변수 설정

  • 그런데, 환경변수 설정을 하지 않으면 터미널에서 실행이 되지 않습니다.
$ psql
-bash: psql: command not found

Pandas 속도 비교 - with or without Dictionary

강의 홍보

1줄 요약

  • Dictionary를 활용한 값 변경의 속도가 훨씬 빠르다.

데이터 불러오기

  • diamonds 데이터셋을 불러온다.
import pandas as pd
import seaborn as sns

diamonds = sns.load_dataset('diamonds')
print(diamonds)
       carat        cut color clarity  depth  table  price     x     y     z
0       0.23      Ideal     E     SI2   61.5   55.0    326  3.95  3.98  2.43
1       0.21    Premium     E     SI1   59.8   61.0    326  3.89  3.84  2.31
2       0.23       Good     E     VS1   56.9   65.0    327  4.05  4.07  2.31
3       0.29    Premium     I     VS2   62.4   58.0    334  4.20  4.23  2.63
4       0.31       Good     J     SI2   63.3   58.0    335  4.34  4.35  2.75
...      ...        ...   ...     ...    ...    ...    ...   ...   ...   ...
53935   0.72      Ideal     D     SI1   60.8   57.0   2757  5.75  5.76  3.50
53936   0.72       Good     D     SI1   63.1   55.0   2757  5.69  5.75  3.61
53937   0.70  Very Good     D     SI1   62.8   60.0   2757  5.66  5.68  3.56
53938   0.86    Premium     H     SI2   61.0   58.0   2757  6.15  6.12  3.74
53939   0.75      Ideal     D     SI2   62.2   55.0   2757  5.83  5.87  3.64

[53940 rows x 10 columns]
  • Color 데이터를 확인해보자.
diamonds['color'].value_counts()
G    11292
E     9797
F     9542
H     8304
D     6775
I     5422
J     2808
Name: color, dtype: int64

color 데이터 값 변경하기

  • D, E, F는 A로 바꿉니다.
  • G, H는 B로 바꿉니다.
  • I, J는 C로 바꿉니다.

Without Dictionary

  • 먼저 첫번째 방법입니다.
import time 

start_time = time.time()
diamonds['color'].replace('D', 'A', inplace=True)
diamonds['color'].replace('E', 'A', inplace=True)
diamonds['color'].replace('F', 'A', inplace=True)
diamonds['color'].replace('G', 'B', inplace=True)
diamonds['color'].replace('H', 'B', inplace=True)
diamonds['color'].replace('I', 'C', inplace=True)
diamonds['color'].replace('J', 'C', inplace=True)

print("Time using .replace() only: {} sec".format(time.time() - start_time))
print("---")
print(diamonds['color'].value_counts())
Time using .replace() only: 0.025814056396484375 sec
---
A    26114
B    19596
C     8230
Name: color, dtype: int64

With Dictionary

  • 이번에는 Dictionary를 활용합니다.
diamonds = sns.load_dataset('diamonds')

start_time = time.time()
diamonds.replace({'color': {'D':'A', 'E':'A', 'F':'A', 'G':'B', 'H':'B', 'I':'C', 'J':'C'}}, inplace=True)

print("Time using .replace() only: {} sec".format(time.time() - start_time))
print("---")
print(diamonds['color'].value_counts())
Time using .replace() only: 0.005134105682373047 sec
---
A    26114
B    19596
C     8230
Name: color, dtype: int64
  • 동일한 결괏값이 나왔지만, 속도 차이가 0.02초 vs 0.005초 차이로 매우 큼을 확인할 수 있다.
  • 즉, 값을 변경한다면, Dictionary를 사용한다.

[MLOps] Weight & Biases 소개 및 사용 방법

인프런 강의

1줄 요약

  • wandb로 MLOps를 배워봅니다.

References

초기 설정

Pandas 속도 비교 - loc vs replace(2)

강의 홍보

1줄 요약

  • 값을 변경할 때에는 .replace 메서드를 사용합니다.

개요

  • Replace 속도를 측정해보자.
  • 이번에는 multiple 값을 변경하는 방법에 대해 알아봅니다.

비교 1 .loc vs .replace

  • 값을 바꾸는 방법은 다음과 같다.
    • data['column'].loc[data['column'] == 'Old Value'] = 'New Value'
import pandas as pd
import seaborn as sns
diamonds = sns.load_dataset('diamonds')
print(diamonds)
       carat        cut color clarity  depth  table  price     x     y     z
0       0.23      Ideal     E     SI2   61.5   55.0    326  3.95  3.98  2.43
1       0.21    Premium     E     SI1   59.8   61.0    326  3.89  3.84  2.31
2       0.23       Good     E     VS1   56.9   65.0    327  4.05  4.07  2.31
3       0.29    Premium     I     VS2   62.4   58.0    334  4.20  4.23  2.63
4       0.31       Good     J     SI2   63.3   58.0    335  4.34  4.35  2.75
...      ...        ...   ...     ...    ...    ...    ...   ...   ...   ...
53935   0.72      Ideal     D     SI1   60.8   57.0   2757  5.75  5.76  3.50
53936   0.72       Good     D     SI1   63.1   55.0   2757  5.69  5.75  3.61
53937   0.70  Very Good     D     SI1   62.8   60.0   2757  5.66  5.68  3.56
53938   0.86    Premium     H     SI2   61.0   58.0   2757  6.15  6.12  3.74
53939   0.75      Ideal     D     SI2   62.2   55.0   2757  5.83  5.87  3.64

[53940 rows x 10 columns]
diamonds.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 53940 entries, 0 to 53939
Data columns (total 10 columns):
 #   Column   Non-Null Count  Dtype   
---  ------   --------------  -----   
 0   carat    53940 non-null  float64 
 1   cut      53940 non-null  category
 2   color    53940 non-null  category
 3   clarity  53940 non-null  category
 4   depth    53940 non-null  float64 
 5   table    53940 non-null  float64 
 6   price    53940 non-null  int64   
 7   x        53940 non-null  float64 
 8   y        53940 non-null  float64 
 9   z        53940 non-null  float64 
dtypes: category(3), float64(6), int64(1)
memory usage: 3.0 MB

비교 2. .loc vs .replace

  • cut Column에 있는 값 중, PremiumIdeal 모두 Best로 변경합니다.
import time

diamonds = sns.load_dataset('diamonds')
diamonds['cut'] = diamonds['cut'].astype('object')

start_time = time.time()
diamonds['cut'].loc[(diamonds['cut'] == 'Premium') | (diamonds['cut'] == 'Ideal')] = 'Best'
print('Time using .loc[]: {} sec'.format(time.time() - start_time))
print(diamonds)
Time using .loc[]: 0.008001089096069336 sec
       carat        cut color clarity  depth  table  price     x     y     z
0       0.23       Best     E     SI2   61.5   55.0    326  3.95  3.98  2.43
1       0.21       Best     E     SI1   59.8   61.0    326  3.89  3.84  2.31
2       0.23       Good     E     VS1   56.9   65.0    327  4.05  4.07  2.31
3       0.29       Best     I     VS2   62.4   58.0    334  4.20  4.23  2.63
4       0.31       Good     J     SI2   63.3   58.0    335  4.34  4.35  2.75
...      ...        ...   ...     ...    ...    ...    ...   ...   ...   ...
53935   0.72       Best     D     SI1   60.8   57.0   2757  5.75  5.76  3.50
53936   0.72       Good     D     SI1   63.1   55.0   2757  5.69  5.75  3.61
53937   0.70  Very Good     D     SI1   62.8   60.0   2757  5.66  5.68  3.56
53938   0.86       Best     H     SI2   61.0   58.0   2757  6.15  6.12  3.74
53939   0.75       Best     D     SI2   62.2   55.0   2757  5.83  5.87  3.64

[53940 rows x 10 columns]


/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py:1636: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_single_block(indexer, value, name)
  • 이번에는 replace 메서드를 사용해본다.
    • data['column'].replace('old value', 'new value', inplace = True
diamonds = sns.load_dataset('diamonds')

start_time = time.time()
diamonds.replace(['Premium', 'Ideal'], 'Best', inplace=True)
print('Time using replace(): {} sec'.format(time.time() - start_time)) 
Time using replace(): 0.0011608600616455078 sec
  • 기존 코드에서, GoodVery GoodLow로 변경코드를 추가합니다.
diamonds = sns.load_dataset('diamonds')
diamonds['cut'] = diamonds['cut'].astype('object')

start_time = time.time()
diamonds['cut'].loc[(diamonds['cut'] == 'Premium') | \
                    (diamonds['cut'] == 'Ideal')] = 'Best'
diamonds['cut'].loc[(diamonds['cut'] == 'Very Good') | \
                    (diamonds['cut'] == 'Good')] = 'Low'
print('Time using .loc[]: {} sec'.format(time.time() - start_time))
print(diamonds)
Time using .loc[]: 0.013423681259155273 sec
       carat   cut color clarity  depth  table  price     x     y     z
0       0.23  Best     E     SI2   61.5   55.0    326  3.95  3.98  2.43
1       0.21  Best     E     SI1   59.8   61.0    326  3.89  3.84  2.31
2       0.23   Low     E     VS1   56.9   65.0    327  4.05  4.07  2.31
3       0.29  Best     I     VS2   62.4   58.0    334  4.20  4.23  2.63
4       0.31   Low     J     SI2   63.3   58.0    335  4.34  4.35  2.75
...      ...   ...   ...     ...    ...    ...    ...   ...   ...   ...
53935   0.72  Best     D     SI1   60.8   57.0   2757  5.75  5.76  3.50
53936   0.72   Low     D     SI1   63.1   55.0   2757  5.69  5.75  3.61
53937   0.70   Low     D     SI1   62.8   60.0   2757  5.66  5.68  3.56
53938   0.86  Best     H     SI2   61.0   58.0   2757  6.15  6.12  3.74
53939   0.75  Best     D     SI2   62.2   55.0   2757  5.83  5.87  3.64

[53940 rows x 10 columns]


/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py:1636: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_single_block(indexer, value, name)
diamonds = sns.load_dataset('diamonds')

start_time = time.time()
diamonds.replace([['Premium', 'Ideal'], ['Very Good', 'Good']], ['Best', 'Low'], inplace=True)
print('Time using replace(): {} sec'.format(time.time() - start_time)) 
print(diamonds)
Time using replace(): 0.002335071563720703 sec
       carat   cut color clarity  depth  table  price     x     y     z
0       0.23  Best     E     SI2   61.5   55.0    326  3.95  3.98  2.43
1       0.21  Best     E     SI1   59.8   61.0    326  3.89  3.84  2.31
2       0.23   Low     E     VS1   56.9   65.0    327  4.05  4.07  2.31
3       0.29  Best     I     VS2   62.4   58.0    334  4.20  4.23  2.63
4       0.31   Low     J     SI2   63.3   58.0    335  4.34  4.35  2.75
...      ...   ...   ...     ...    ...    ...    ...   ...   ...   ...
53935   0.72  Best     D     SI1   60.8   57.0   2757  5.75  5.76  3.50
53936   0.72   Low     D     SI1   63.1   55.0   2757  5.69  5.75  3.61
53937   0.70   Low     D     SI1   62.8   60.0   2757  5.66  5.68  3.56
53938   0.86  Best     H     SI2   61.0   58.0   2757  6.15  6.12  3.74
53939   0.75  Best     D     SI2   62.2   55.0   2757  5.83  5.87  3.64

[53940 rows x 10 columns]

Pandas 속도 비교 - loc vs replace

강의 홍보

개요

  • loc and Replace 속도를 비교 측정해본다..

방법 1. .loc vs .replace

  • 값을 바꾸는 방법은 다음과 같다.
    • data['column'].loc[data['column'] == 'Old Value'] = 'New Value'
import pandas as pd
import seaborn as sns
diamonds = sns.load_dataset('diamonds')
print(diamonds)
       carat        cut color clarity  depth  table  price     x     y     z
0       0.23      Ideal     E     SI2   61.5   55.0    326  3.95  3.98  2.43
1       0.21    Premium     E     SI1   59.8   61.0    326  3.89  3.84  2.31
2       0.23       Good     E     VS1   56.9   65.0    327  4.05  4.07  2.31
3       0.29    Premium     I     VS2   62.4   58.0    334  4.20  4.23  2.63
4       0.31       Good     J     SI2   63.3   58.0    335  4.34  4.35  2.75
...      ...        ...   ...     ...    ...    ...    ...   ...   ...   ...
53935   0.72      Ideal     D     SI1   60.8   57.0   2757  5.75  5.76  3.50
53936   0.72       Good     D     SI1   63.1   55.0   2757  5.69  5.75  3.61
53937   0.70  Very Good     D     SI1   62.8   60.0   2757  5.66  5.68  3.56
53938   0.86    Premium     H     SI2   61.0   58.0   2757  6.15  6.12  3.74
53939   0.75      Ideal     D     SI2   62.2   55.0   2757  5.83  5.87  3.64

[53940 rows x 10 columns]
  • cut Column에 있는 값 중, Premium을 Best로 바꿔보도록 한다.
import time
start_time = time.time()

diamonds['cut'].loc[diamonds['cut'] == 'Premium'] == 'Best'
print('Time using .loc[]: {} sec'.format(time.time() - start_time))
Time using .loc[]: 0.0020329952239990234 sec
  • 이번에는 replace 메서드를 사용해본다.
    • data['column'].replace('old value', 'new value', inplace = True
diamonds = sns.load_dataset('diamonds')

start_time = time.time()
diamonds.replace('Premium', 'Best', inplace=True)
print('Time using replace(): {} sec'.format(time.time() - start_time))
Time using replace(): 0.00027108192443847656 sec

Pandas 속도 비교 - iloc and loc

강의 홍보

1줄 요약

  • .loc[]와 .iloc[] 인덱스의 속도 차이를 측정해본다.

개요

  • 시간이 허락한다면, Pandas 속도를 비교하는 게시글을 자주 작성하려고 한다.
    • Pandas가 상대적으로 속도가 느리기 때문에, 조금 더 효율적인 코드를 작성하는 쪽에 초점을 맞춰본다.
  • .loc[] : index name locator를 의미한다.
  • iloc[] : index number locator를 의미한다.

행 선택시 속도 비교

  • 먼저 행을 선택할 때의 속도 차이를 확인하도록 합니다.
import pandas as pd
import time
import seaborn as sns

diamonds = sns.load_dataset("diamonds")
diamonds.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 53940 entries, 0 to 53939
Data columns (total 10 columns):
 #   Column   Non-Null Count  Dtype   
---  ------   --------------  -----   
 0   carat    53940 non-null  float64 
 1   cut      53940 non-null  category
 2   color    53940 non-null  category
 3   clarity  53940 non-null  category
 4   depth    53940 non-null  float64 
 5   table    53940 non-null  float64 
 6   price    53940 non-null  int64   
 7   x        53940 non-null  float64 
 8   y        53940 non-null  float64 
 9   z        53940 non-null  float64 
dtypes: category(3), float64(6), int64(1)
memory usage: 3.0 MB
  • 먼저 .loc 속도 측정을 해봅니다.
row_nums = range(0, 10000)

start_time = time.time()
rows = diamonds.loc[row_nums]
end_time = time.time()

print("Time using .loc[]: {} sec".format(end_time - start_time))
Time using .loc[]: 0.0029916763305664062 sec
  • 이번에는 동일하게 .iloc를 적용해봅니다.
start_time = time.time()
rows = diamonds.iloc[row_nums]
end_time = time.time()

print("Time using .iloc[]: {} sec".format(end_time - start_time))
Time using .iloc[]: 0.001990079879760742 sec

열 선택시 속도 비교

  • 이번에는 iloc를 활용하여 열을 선택합니다.
iloc_start_time = time.time()
cols = diamonds.iloc[:, [0, 2, 4, 6, 8]]
iloc_end_time = time.time()

print("Time using .iloc[]: {} sec".format(iloc_end_time - iloc_start_time))
Time using .iloc[]: 0.0009975433349609375 sec
  • 이번에는 Column명을 입력해서 추출하도록 합니다.
name_start_time = time.time()
cols = diamonds[['carat', 'color', 'depth', 'price', 'y']]
name_end_time = time.time()

print("Time using selection by name : {} sec".format(name_end_time - name_start_time))
Time using selection by name : 0.000997304916381836 sec

Reference

[Python] PyCaret Windows 10 아나콘다 설치 방법

강의 홍보

1줄 요약

  • 관리자 실행해서 아나콘다 가상 환경을 만든 후, 새로운 패키지를 설치한다.

PyCaret 설치 방법 (Windows 10)

  • 윈도우 10 환경에서 PyCaret 패키지를 설치해봅니다.
  • 아나콘다 설치에 관한 내용은 생략합니다. 다만, 이 때, 필요한 것은 환경변수에 추가가 되어 있어야 합니다.

가상환경 설정

  • 새로운 가상환경을 만듭니다. (이게 제일 편합니다.)
  • 명령프롬프트를 관리자로 실행합니다.