Mecab 사용자 단어 사전 추가 in R, windows 10

개요

  • Mecab에서 사용자 단어를 추가하고 직접 사용하는 예제를 만들어본다.
  • 사전에 Mecab은 설치가 되어 있다고 가정한다. 만약 처음 설치한다면, 다음 참조 링크에서 설치를 진행한다.

문제점

  • 아래그림과 같이 ‘사회적경제’라는 단어가 잡히지 않는 것을 확인할 수 있다.

Untitled

해결방법

  • 먼저, 사전 위치를 찾는다.
    • mecab > user-dic 폴더 내에 nnp.csv 파일을 찾는다.

Untitled

Untitled

  • nnp.csv 파일을 메모장으로 열고 수정한다.

Untitled

Flask Heroku Pandas Postgres 튜토리얼

강의 홍보

개요

  • Flask 기본적인 작동 원리를 배운다.
  • Postgres와 SQLAlchemy를 활용한다.
  • Heroku에 배포를 진행한다.

사전준비

  • Github에 각 개인에게 맞는 Github Repo를 생성한다.
    • 주의 : 반드시 Unique하게 작성해야 한다.
  • 가상환경 설정을 진행한다.
  • PostgreSQL DB 설정은 다음을 참조한다.
virtualenv venv
  • 주요 라이브러리를 설치한다.
pip install Flask psycopg2 SQLAlchemy pandas gunicorn

배포

  • 프로젝트 시작 전에는 항상 선 배포를 하고 시작한다.
  • app.py를 만들어본다.
# -*- coding: utf-8 -*-

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello World!"
  • 3개의 파일이 필요하다.

Scikit-Learn ML Model with Java

강의 홍보

개요

  • scikit-learn 모델을 JAVA에서 구동 시켜야 한다.
  • 크게 3가지 방법론이 존재한다.(원문 참조 : Moving from Python to Java to deploy your machine learning model to production
    • embed : Java 코드 내에서 직접 Python 코드 구현 방법. Jython을 이용하지만, 문제는 Scikit-Learn은 지원하지 않는다. 따라서 일반적으로 Flask API를 통해서 지원하기도 한다.
    • transpile : Scikit-Learn 모델을 전달하는 방법. sklearn-porterm2cgen를 고려할 수 있다.
    • redevelop : Scikit-Learn 모델을 H20나 Spark의 MLib을 구현한 후 배포하는 방법이다.

새로운 제안

  • 그러나 다각도로 여러 시도 및 라이브러리가 존재함.
  • 오늘 소개할 라이브러니느 sklearn2pmml임.
  • JAVA가 설치가 되어 있어야 함.
    • 로컬 환경은 각자 환경변수를 추가해야 함. 이 부분은 생략함.
  • 구글 코랩에서는 아래와 같이 설치가 가능함.
  • Java 코드 참조 : Using scikit-learn model into Java app
!apt update -q
!apt-get install -q openjdk-11-jdk-headless

%env JAVA_HOME "/usr/lib/jvm/java-11-openjdk-amd64"
Hit:1 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64  InRelease
Get:2 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3,626 B]
Get:3 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Hit:4 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease
Hit:5 http://archive.ubuntu.com/ubuntu bionic InRelease
Get:6 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Ign:7 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  InRelease
Hit:8 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease
Hit:9 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Release
Hit:10 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease
Get:11 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Hit:12 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease
Get:14 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [2,867 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [1,075 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2,297 kB]
Get:17 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [3,302 kB]
Fetched 9,797 kB in 6s (1,534 kB/s)
Reading package lists...
Building dependency tree...
Reading state information...
49 packages can be upgraded. Run 'apt list --upgradable' to see them.
Reading package lists...
Building dependency tree...
Reading state information...
openjdk-11-jdk-headless is already the newest version (11.0.15+10-0ubuntu0.18.04.1).
The following package was automatically installed and is no longer required:
  libnvidia-common-460
Use 'apt autoremove' to remove it.
0 upgraded, 0 newly installed, 0 to remove and 49 not upgraded.
env: JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
  • 설치가 끝난 후에는 sklearn2pmml을 설치한다.
!pip install sklearn2pmml
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting sklearn2pmml
  Downloading sklearn2pmml-0.84.2.tar.gz (6.3 MB)
     |████████████████████████████████| 6.3 MB 5.1 MB/s 
[?25hRequirement already satisfied: joblib>=0.13.0 in /usr/local/lib/python3.7/dist-packages (from sklearn2pmml) (1.1.0)
Requirement already satisfied: scikit-learn>=0.18.0 in /usr/local/lib/python3.7/dist-packages (from sklearn2pmml) (1.0.2)
Requirement already satisfied: sklearn-pandas>=0.0.10 in /usr/local/lib/python3.7/dist-packages (from sklearn2pmml) (1.8.0)
Requirement already satisfied: numpy>=1.14.6 in /usr/local/lib/python3.7/dist-packages (from scikit-learn>=0.18.0->sklearn2pmml) (1.21.6)
Requirement already satisfied: scipy>=1.1.0 in /usr/local/lib/python3.7/dist-packages (from scikit-learn>=0.18.0->sklearn2pmml) (1.4.1)
Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from scikit-learn>=0.18.0->sklearn2pmml) (3.1.0)
Requirement already satisfied: pandas>=0.11.0 in /usr/local/lib/python3.7/dist-packages (from sklearn-pandas>=0.0.10->sklearn2pmml) (1.3.5)
Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.11.0->sklearn-pandas>=0.0.10->sklearn2pmml) (2022.1)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.11.0->sklearn-pandas>=0.0.10->sklearn2pmml) (2.8.2)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas>=0.11.0->sklearn-pandas>=0.0.10->sklearn2pmml) (1.15.0)
Building wheels for collected packages: sklearn2pmml
  Building wheel for sklearn2pmml (setup.py) ... [?25l[?25hdone
  Created wheel for sklearn2pmml: filename=sklearn2pmml-0.84.2-py3-none-any.whl size=6298569 sha256=f6a564303dd11e9ce38b6c7b4ec4e8a4632499c61d3ee69f40070a1f1983ef80
  Stored in directory: /root/.cache/pip/wheels/bb/e4/71/d3c8f75fae8d7f387f82099ec8cdd6b83cf1dccaeb3561c7b6
Successfully built sklearn2pmml
Installing collected packages: sklearn2pmml
Successfully installed sklearn2pmml-0.84.2

샘플 코드 작성

  • 간단한 모형을 구축한다.
from sklearn.datasets import load_diabetes
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn2pmml import PMMLPipeline, sklearn2pmml
import pandas as pd

# 데이터 불러오기
df = load_diabetes()
X = pd.DataFrame(columns = df.feature_names, data = df.get('data'))
y = pd.DataFrame(columns = ['target'], data = df.get('target'))

# 데이터셋 분리하기
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state=42)

# Pipeline 구축하기
pipeline = PMMLPipeline([ ('regressor', DecisionTreeRegressor()) ])

# 모형 학습 시키기
pipeline.fit(X_train, y_train)
PMMLPipeline(steps=[('regressor', DecisionTreeRegressor())])
  • 이번에는 모형을 테스트 한다.
from sklearn.metrics import mean_absolute_error
y_pred = pipeline.predict(X_test)
print('MAE: ', mean_absolute_error(y_pred, y_test))
MAE:  65.42222222222222
  • 이제 모형을 pmml 파일 형태로 저장한다.
# 모델 내보내기
sklearn2pmml(pipeline, 'model.pmml', with_repr = True)

Scikit-Learn Model into JAVA

  • 해당 모형을 이제 이제 Java에서 가져오도록 한다.
  • Java 코드에서는 pmml4s library를 이용한다.
import org.pmml4s.model.Model;

import java.util.*;

public class Main {

    private final Model model = Model.fromFile(Main.class.getClassLoader().getResource("model.pmml").getFile());

    public Double getRegressionValue(Map<String, Double> values) {
        Object[] valuesMap = Arrays.stream(model.inputNames())
                .map(values::get)
                .toArray();

        Object[] result = model.predict(valuesMap);
        return (Double) result[0];
    }

    public static void main(String[] args) {
        Main main = new Main();
        Map<String, Double> values = Map.of(
                "age", 20d,
                "sex", 1d,
                "bmi", -100d,
                "bp", -200d,
                "s1", 1d,
                "s2", 2d,
                "s3", 3d,
                "s4", 4d,
                "s5", 5d,
                "s6", 6d
        );

        double predicted = main.getRegressionValue(values);
        System.out.println(predicted);
    }
}

Oracle CSV File Upload using CMD

개요

  • iris.csv 파일을 오라클 명령어를 통해서 업로드를 진행한다.
  • 대부분의 명령어는 터미널에서 처리하였다.

SQLPLUS 접속

  • SQL에 접속한다.

Untitled

테이블 생성

  • 터미널에서 iris 테이블을 생성한다.

Untitled

CREATE TABLE iris (
	sepal_length INTEGER, 
	sepal_width  INTEGER, 
  petal_length INTEGER, 
  petal_width  INTEGER, 
  species      varchar2(20)
);

ctl 파일 생성

  • 메모장에 아래와 같이 ctl 파일을 생성한다.
  • 파일명은 control.ctl이다.
LOAD DATA
INFILE 'iris.csv'
INTO TABLE iris
fields terminated by ','
(
    sepal_length,
    sepal_width, 
    petal_length,
    petal_width,
    species
)

파일 업로드

  • sqlplus 접속을 종료한다.
  • control.ctl 파일이 있는 경로로 이동한다. 만약, control.ctl.txt 형태로 저장이 되어 있다면, vscode 편집기에서 수정하는 것을 권한다.

Untitled

MLFlow with Scikit-Learn

개요

  • Scikit-Learn 모델을 만든 후, MLFlow로 모델을 배포한다.
  • 머신러닝 코드에 대한 설명은 생략한다.
  • 가상환경 설정에 관한 내용도 생략한다.

라이브러리 불러오기

  • 기존 코드에서 mlflow 라이브러리만 추가한다.
%matplotlib inline

import numpy as np 
import pandas as pd 
import matplotlib as mpl
import matplotlib.pyplot as plt 
import sklearn
import seaborn as sns
import mlflow 
import mlflow.sklearn
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split, KFold
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import roc_auc_score, plot_roc_curve, confusion_matrix 

print(f"numpy version {np.__version__}")
print(f"pandas version {pd.__version__}")
print(f"matplotlib version {mpl.__version__}")
print(f"seaborn version {sns.__version__}")
print(f"sklearn version {sklearn.__version__}")
print(f"MLFlow version {mlflow.__version__}")
numpy version 1.23.1
pandas version 1.4.3
matplotlib version 3.5.2
seaborn version 0.11.2
sklearn version 1.1.1
MLFlow version 1.27.0

데이터 불러오기

  • 데이터를 불러오도록 한다.
DATA_PATH = "C:\\Users\\your_id\\Desktop\\mlops_tutorial\\data\\creditcard.csv"

df = pd.read_csv(DATA_PATH)
print(df.head())
   Time        V1        V2        V3        V4        V5        V6        V7  \
0   0.0 -1.359807 -0.072781  2.536347  1.378155 -0.338321  0.462388  0.239599   
1   0.0  1.191857  0.266151  0.166480  0.448154  0.060018 -0.082361 -0.078803   
2   1.0 -1.358354 -1.340163  1.773209  0.379780 -0.503198  1.800499  0.791461   
3   1.0 -0.966272 -0.185226  1.792993 -0.863291 -0.010309  1.247203  0.237609   
4   2.0 -1.158233  0.877737  1.548718  0.403034 -0.407193  0.095921  0.592941   

         V8        V9  ...       V21       V22       V23       V24       V25  \
0  0.098698  0.363787  ... -0.018307  0.277838 -0.110474  0.066928  0.128539   
1  0.085102 -0.255425  ... -0.225775 -0.638672  0.101288 -0.339846  0.167170   
2  0.247676 -1.514654  ...  0.247998  0.771679  0.909412 -0.689281 -0.327642   
3  0.377436 -1.387024  ... -0.108300  0.005274 -0.190321 -1.175575  0.647376   
4 -0.270533  0.817739  ... -0.009431  0.798278 -0.137458  0.141267 -0.206010   

        V26       V27       V28  Amount  Class  
0 -0.189115  0.133558 -0.021053  149.62      0  
1  0.125895 -0.008983  0.014724    2.69      0  
2 -0.139097 -0.055353 -0.059752  378.66      0  
3 -0.221929  0.062723  0.061458  123.50      0  
4  0.502292  0.219422  0.215153   69.99      0  

[5 rows x 31 columns]

로지스틱 모형 만들기

  • 기존 코드를 참조하여 모델을 만든다.
  • 데이터셋 분리를 한다.

normal = df[df['Class'] == 0].sample(frac=0.5, random_state=42).reset_index(drop=True)
anomaly = df[df['Class']==1]

normal_train, normal_test = train_test_split(normal, test_size = 0.2, random_state=42)
anomary_train, anomary_test = train_test_split(anomaly, test_size = 0.2, random_state=42)

normal_train, normal_validate = train_test_split(normal_train, test_size = 0.25, random_state=42)
anomary_train, anomary_validate = train_test_split(anomary_train, test_size = 0.25, random_state=42)

normal_train.shape, normal_validate.shape, anomary_train.shape, anomary_validate.shape
((85294, 31), (28432, 31), (294, 31), (99, 31))
  • 최종 학습, 테스트 및 검증 세트를 생성하기 위해 각각의 정상 및 이상 데이터 분할을 연결해야 한다.
X_train = pd.concat((normal_train, anomary_train))
X_test = pd.concat((normal_test, anomary_test))
X_validate = pd.concat((normal_validate, anomary_validate))

y_train = np.array(X_train["Class"])
y_test = np.array(X_test["Class"])
y_validate = np.array(X_validate["Class"])

X_train = X_train.drop("Class", axis = 1)
X_test = X_test.drop("Class", axis = 1)
X_validate = X_validate.drop("Class", axis = 1)

X_train.shape, X_validate.shape, X_test.shape, y_train.shape, y_validate.shape, y_test.shape
((85588, 30), (28531, 30), (28531, 30), (85588,), (28531,), (28531,))
  • 표준화를 진행한다.
scaler = StandardScaler()
scaler.fit(X_train)

X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
X_validate = scaler.transform(X_validate)

MLFlow를 통한 학습 및 평가

  • MLFlow 기능으로 검증에 사용하는 사용자 정의 함수를 만들어 본다.
  • 여기서 핵심은
    • mlflow.log_metric() 함수를 통해 지표를 로깅할 수 있음
    • mlflow.log_artifact() 함수를 통해 그래프를 저장할 수 잇음.
def train(sk_model, X_train, y_train):
    sk_model = sk_model.fit(X_train, y_train)
    train_acc = sk_model.score(X_train, y_train)
    mlflow.log_metric("train_acc", train_acc)
    
    print(f"Train Accuracy: (train_acc:.3%)")
    

def evaluate(sk_model, X_test, y_test):
    eval_acc = sk_model.score(X_test, y_test)
    preds = sk_model.predict(X_test)
    auc_score = roc_auc_score(y_test, preds)
    mlflow.log_metric("eval_acc", eval_acc)
    mlflow.log_metric("auc_score", auc_score)
    
    print(f"Auc Score : {auc_score:.3%}")
    print(f"Eval Score : {eval_acc:.3%}")
    roc_plot = plot_roc_curve(sk_model, X_test, y_test, name="Scikit-Learn ROC Curve")
    plt.savefig("sklearn_roc_plot.png")
    plt.show()
    plt.clf()
    conf_matrix = confusion_matrix(y_test, preds)
    ax = sns.heatmap(conf_matrix, annot=True, fmt='g')
    ax.invert_xaxis()
    ax.invert_yaxis()
    plt.ylabel("Actual")
    plt.xlabel("Predicted")
    plt.title("Confusion Matrix")
    plt.savefig("sklearn_conf_matrix.png")
    mlflow.log_artifact("sklearn_roc_plot.png")
    mlflow.log_artifact("sklearn_conf_matrix.png")

MLFlow 실행 로깅 및 확인

  • 실제 실험 이름을 설정하고, MLFlow 실행 시작. 해당 코드를 모두 실행한다.
# 모델 설정
sk_model = LogisticRegression(random_state=None, max_iter=400, solver='newton-cg')

# 실험 이름 설정
mlflow.set_experiment("sklearn_experiment")

# 해당 이름으로 실행 배치
with mlflow.start_run():
    train(sk_model, X_train, y_train)
    evaluate(sk_model, X_test, y_test)
    
    # 하나의 MLFlow 실행 컨텍스트에서 모든 코드를 묶을 수 있음. 
    # 참조 : https://mlflow.org/docs/latest/models.html#model-customization
    mlflow.sklearn.log_model(sk_model, 'log_reg_model')
    
    # 본질적으로 모델과 지표가 로깅되는 현재 실행을 가져오고 출력함. 
    print("Model run: ", mlflow.active_run().info.run_uuid)
mlflow.end_run()
Train Accuracy: (train_acc:.3%)
Auc Score : 86.355%
Eval Score : 99.888%


C:\Users\your_id\Desktop\mlops_tutorial\venv\lib\site-packages\sklearn\utils\deprecation.py:87: FutureWarning: Function plot_roc_curve is deprecated; Function :func:`plot_roc_curve` is deprecated in 1.0 and will be removed in 1.2. Use one of the class methods: :meth:`sklearn.metric.RocCurveDisplay.from_predictions` or :meth:`sklearn.metric.RocCurveDisplay.from_estimator`.
  warnings.warn(msg, category=FutureWarning)

png

주요 핵심 머신러닝 리뷰

강의 홍보

개요

  • 수강생들의 머신러닝을 활용한 웹 개발 프로젝트 전 복습 차원에서 준비함.
  • 주 내용은 주요 참고자료를 기반으로 작성하였으며, 참고자료에 없는 코드는 직접 작성하였음을 밝힘.

가장 인기 있는 모델

  • XGBoost와 LightGBM
  • 그 외, 선형회귀, 로지스틱 회귀, 결정 트리, 앙상블 학습, 랜덤 포레스트, XGBoost, LightGBM

선형 회귀

  • 선형 회귀식을 활용한 모델
  • 회귀 계수와 절편을 찾는 것이 중요
  • 기초통계에서 다루는 선형 회귀와 기본적인 개념에서는 동일하나, 기초통계에서와 예측 모델에서의 쓰임새는 다르다는 것을 기억한다.

데이터 생성

  • 단순 선형 회귀식 $y = 3x + 4$에 근사한 데이터 50개 생성
import numpy as np 
import pandas as pd 

np.random.seed(0) # 시드값 고정
intercept = 4 # 절편
slope = 3 # 기울기

# 변동성 주기 위해 노이즈 생성
noise = np.random.randn(50, 1)

# 50개의 x값 생성
x = 5 * np.random.rand(50, 1) # 0과 5사이의 실숫값 50개 생성
y = slope * x + intercept + noise

# 데이터 프레임 생성
data = pd.DataFrame({'X': x[:, 0], 'Y': y[:, 0]})
print(data.head())
          X          Y
0  0.794848   8.148596
1  0.551876   6.055784
2  3.281648  14.823682
3  0.690915   8.313637
4  0.982912   8.816293
  • 위 데이터를 시각화로 구현한다.
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(data['X'], data['Y'])
plt.show()

png

django Web 개발 - IRIS Prediction

개요

  • Python Django와 Sklearn을 활용하여 간단한 iris prediction 웹을 만들어본다.

사전준비

  • 머신러닝 기본 이론 및 원리는 어느정도 알고 있다고 가정한다.
  • Django 앱에 대해 어느정도 알고 있다고 가정한다.

무엇을 배우는가?

  • 머신러닝 모델을 활용하여 배포하는 과정을 배운다.

가상환경 설정

  • 가상환경을 생성한다.
$ virtualenv venv
created virtual environment CPython3.9.1.final.0-64 in 475ms
  creator CPython3Posix(dest=/Users/evan/Desktop/django-iris-tutorial/venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/evan/Library/Application Support/virtualenv)
    added seed packages: pip==22.1.1, setuptools==62.3.2, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
  • 만들어진 가상환경에 접속한다.
$ source venv/bin/activate
(venv) $
  • 크게 3개의 라이브러리를 설치한다.
    • jupyterlab : 머신러닝 개발 과정을 진행할 에디터로 활용한다.
    • sklearn : 머신러닝 개발 관련 라이브러리이다.
    • django : django 웹 프레임워크 라이브러리이다.
(venv) $ pip install jupyterlab sklearn django

머신러닝 개발

  • iris 데이터를 불러오고 sklearn을 활용하여 모형 개발을 진행한다.
  • 모형 개발 시, 주요 Feature Engineering 과정은 생략한다.

(1) 모형 개발

  • jupyterlab을 실행한다.
(venv) $ python -m jupyterlab
  • 아래와 같이 코드를 입력한다.
# Module 불러오기
from pandas import read_csv
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
import pandas as pd

# 데이터셋 불러오기
df = pd.read_csv("data/iris.csv")

# 독립변수와 종속변수 분리
X = df[['sepal_length','sepal_width','petal_length','petal_width']]
y = df['classification']

# 훈련데이터와 종속데이터 분리
X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.20, random_state=1)

# 모형 학습
model = SVC(gamma='auto')
model.fit(X_train, Y_train)

# 모형 예측
sepal_length = float(1.5)
sepal_width = float(5)
petal_length = float(4)
petal_width = float(3)

result = model.predict([[sepal_length,sepal_width,petal_length,petal_width]])  # input must be 2D array
print(result)
['Iris-virginica']
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sklearn/base.py:445: UserWarning: X does not have valid feature names, but SVC was fitted with feature names
  warnings.warn(

(2) 모형 저장

  • pickle을 통해 모형 저장을 할 수 있다.
pd.to_pickle(model, r'models/svc_model.pickle')

Django 시작

  • django 웹사이트 프로젝트를 시작한다.
(venv) $ django-admin startproject iris
(venv) $ cd iris
(venv) $ python manage.py startapp predict

settings.py

  • iris/settings.py를 열고 아래와 같이 수정한다.
.
.
# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'predict', 
]
.
.

urls.py

  • iris/urls.py를 열고 아래와 같이 수정한다.
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('predict.urls', namespace='predict'))
]
  • predict/urls.py를 새로 생성하고 아래와 같이 코드를 추가한다.
from django.urls import path
from . import views

app_name = 'predict'

urlpatterns = [
    path('', views.predict, name='predict'),
]

views.py

  • 이제 predict/views.py에서 predict 함수를 만들어 백엔드 처리를 진행하고, 최종 결괏값을 predict.html로 돌려주는 함수를 구현할 것이다.
from django.shortcuts import render

# Create your views here.
def predict(request):
    return render(request, 'predict.html', {})

predict.html

  • predict/templates 폴더를 만들고, predict.html 파일을 새로 생성한다.
hello
  • predict.html 파일이 잘 열리는지 확인한다.
(venv) iris$ python manage.py runserver 

MySQL Database 생성 및 권한 부여

개요

  • MySQL 관리자 계정인 root로 DB 관리시스템에 접속 후 DB를 생성한다.

사전준비

DB 생성

  • 콘솔창에서 MySQL 명령을 실행한다.
C:\Users\your_name>mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
  • DB 생성 명령을 실행한다. DB명은 homestead 로 지정했다.
mysql> CREATE DATABASE homestead;
Query OK, 1 row affected (0.01 sec)

DB사용자 생성

  • 해당 DB에 접근할 수 있는 계정을 생성한다.
    • {username}과 {password}에 각 개인이 원하는대로 지정한다.
mysql> CREATE USER '{username}'@'localhost' IDENTIFIED BY '{password}';
mysql> CREATE USER '{username}'@'%' IDENTIFIED BY '{password}';
  • 필자는 아래와 같이 했다.
mysql> CREATE USER 'homestead'@'localhost' IDENTIFIED BY 'secret';
Query OK, 0 rows affected (0.02 sec)
  • 생성한 계정에 권한을 부여한다.
  • 첫번째 명령어는 해당 DB에 모든 권한을 부여한다는 뜻이다.
  • 두번째 명령어는 DBMS에 적용하라는 의미를 말하며, 반드시 실행해야 한다.
mysql> GRANT ALL PRIVILEGES ON homestead.* TO 'homestead'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

접속

  • 이제 homestead DB에 접속한다.
  • 기존 cmd 창은 root 계정이기 때문에 exit를 통해 선 종료 한다.
mysql> exit 
Bye
  • 직접 해당 DB에 접근하는 것은 다음과 같다.
mysql -h127.0.0.1 -u{username} -p {database} # 예시
mysql -h127.0.0.1 -uhomestead -p homestead # 적용
  • 실제로 적용하면 아래와 같이 실행될 것이다.
C:\Users\your_name>mysql -h127.0.0.1 -uhomestead -p homestead
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
  • 현재 DB의 종류를 조회해본다.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| homestead          |
| information_schema |
+--------------------+
2 rows in set (0.00 sec)

MySQL Workbench 사용

  • 이제 DB를 사용했으니, Workbench를 통해 접속해본다.
  • 윈도우 돋보기에서 MySQL을 조회 후, Workbench 8.0 CE를 실행한다.

Untitled

MySQL 설치 및 환경변수 설정 Windows 11

개요

  • Windows 11에 MySQL을 설치합니다.

MySQL

  • 챗봇 시스템의 학습 데이터 관리 위해 MySQL을 사용함
  • 설치 주소 : https://dev.mysql.com/downloads/
    • MySQL Installer for Windows 파일을 선택함

tutorial_01.png

  • MSI Installer를 다운로드 받는다.

Untitled

  • 다운로드 받은 파일을 순차적으로 설치 한다.

Untitled

Untitled

Untitled

Untitled

Untitled

Untitled

  • 비밀번호는 잃어버리면 안된다. (비번 : 1234)

Untitled

Untitled

Untitled

Untitled

Untitled

Untitled

Untitled

  • 앞서 설정한 비밀번호를 입력하고 체크 버튼을 누른다.

Untitled

Untitled

Untitled

Untitled

  • 정상적으로 설치가 완료되었다.

Untitled

  • 윈도우에서 돋보기 모양을 누른 후, MySQL 8.0 Command Line Client를 클릭한다.
  • root 계정 비밀번호를 입력 후 접속한다.

Untitled

S3 with Python Basic Tutorial

Bucket 만들기

  • Bucket을 만들어보도록 한다.
import boto3
print(boto3.__version__)
1.23.5
bucket = boto3.resource('s3')

response = bucket.create_bucket(
    Bucket = "your_bucket_name",
    ACL="private", # public-read
    
    CreateBucketConfiguration = {
        'LocationConstraint' : 'ap-northeast-2'
    }
)

print(response)
s3.Bucket(name='your_bucket_name')
  • 버킷 대시보드에서 실제 Bucket이 만들어졌는지 확인한다.

Client Bucket

  • 이번에는 client 버킷을 생성한다.
client = boto3.client('s3')

response = client.create_bucket(
    Bucket = "your_bucket_name",
    ACL = "private",
    
    CreateBucketConfiguration = {
        'LocationConstraint' : 'ap-northeast-2'
    }

)

print(response)
{'ResponseMetadata': {'RequestId': '1X0BAXRG653Q7Y61', 'HostId': 'WwKyxNBcd1V9x6D/WZn8twMKSWKBnkwVCPWtvarZvyNSSvqr7Q77J6OFAdWuYAwiv/nQfXoW/0U=', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amz-id-2': 'WwKyxNBcd1V9x6D/WZn8twMKSWKBnkwVCPWtvarZvyNSSvqr7Q77J6OFAdWuYAwiv/nQfXoW/0U=', 'x-amz-request-id': '1X0BAXRG653Q7Y61', 'date': 'Wed, 25 May 2022 03:16:52 GMT', 'location': 'http://your_bucket_name.s3.amazonaws.com/', 'server': 'AmazonS3', 'content-length': '0'}, 'RetryAttempts': 0}, 'Location': 'http://your_bucket_name.s3.amazonaws.com/'}

Resource와 Client의 차이

  • Resource
    • high-level, 객체지향적 인터페이스
    • resource description에 의해 만들어짐
    • 식별자(identifier)와 속성(attribute)을 사용
    • 자원에 대한 조작 위주
  • Client
    • low-level 인터페이스
    • service description에 의해 만들어짐
    • botocore 수준의 client를 공개(botocore는 AWS CLI와 boto3의 기초가 되는 라이브러리)
    • AWS API와 1:1 매핑됨
    • 메소드가 스네이크 케이스로 정의되어 있음

이미지 업로드 및 조회

  • 일반적으로 이미지를 업로드 하는 코드는 다음과 같다.
    • 단, Django나 Flask 등을 활용하여 이미지를 업로드할 때는 코드 작성 방법이 달라지니 참고용으로 확인한다.
  • 코드 실행 후, Web UI에서 직접 확인한다.