Vertex AI Workbench를 이용한 Custom ML 예제Custom ML Example using Vertex AI Workbench
개요Overview
이 예제에서는 Vertex AI Workbench의 관리형 노트북 환경에서 Python과 Scikit-learn 라이브러리를 사용하여 간단한 머신러닝 모델을 학습시키는 과정을 안내합니다.This example guides you through the process of training a simple machine learning model using Python and the Scikit-learn library in a managed notebook environment on Vertex AI Workbench.
대표적인 붓꽃(Iris) 데이터셋을 사용하여 품종을 분류하는 모델을 만들어 보겠습니다.We will create a model to classify iris species using the classic Iris dataset.
1단계: Vertex AI Workbench 노트북 생성Step 1: Create a Vertex AI Workbench Notebook
먼저, Google Cloud 콘솔에서 Vertex AI Workbench 인스턴스를 생성해야 합니다.First, you need to create a Vertex AI Workbench instance from the Google Cloud Console.
- Vertex AI Workbench 페이지로 이동합니다.Navigate to the Vertex AI Workbench page.
- [새 노트북]을 클릭합니다.Click [New Notebook].
- "Python 3" 환경을 선택하고, 머신 유형 등 필요한 사양을 설정합니다.Select the "Python 3" environment and configure the necessary specifications, such as machine type.
- [만들기]를 클릭하여 노트북 인스턴스를 생성합니다. 생성이 완료되면 [JupyterLab 열기]를 클릭합니다.Click [Create] to create the notebook instance. Once it's created, click [Open JupyterLab].
2단계: Python 코드로 모델 학습시키기Step 2: Train the Model with Python Code
JupyterLab 환경이 열리면, 새 노트북(.ipynb) 파일을 만들고 아래 코드를 셀에 입력하여 실행합니다.Once the JupyterLab environment opens, create a new notebook (.ipynb) file and enter the following code into a cell to run it.
코드: 붓꽃 데이터셋 로드 및 모델 학습Code: Load Iris Dataset and Train Model
# 필요한 라이브러리 가져오기
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# 1. 데이터 로드
# Scikit-learn에 내장된 예제용 붓꽃(Iris) 데이터셋을 로드합니다.
# 별도의 파일 없이 라이브러리에서 바로 데이터를 가져올 수 있습니다.
iris = load_iris()
X, y = iris.data, iris.target
print(f"데이터셋 크기: {X.shape}")
print(f"클래스 종류: {iris.target_names}")
# 2. 데이터 분할 (학습용 / 테스트용)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
print(f"학습 데이터 크기: {X_train.shape}")
print(f"테스트 데이터 크기: {X_test.shape}")
# 3. 모델 생성 및 학습 (결정 트리 모델)
model = DecisionTreeClassifier()
model.fit(X_train, y_train)
print("\n모델 학습이 완료되었습니다.")
# 4. 모델 예측 및 평가
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"\n모델 정확도: {accuracy:.4f}")
# 5. 새로운 데이터로 예측해보기
# 예시: 꽃받침 길이=5.1, 너비=3.5, 꽃잎 길이=1.4, 너비=0.2
new_sample = [[5.1, 3.5, 1.4, 0.2]]
prediction = model.predict(new_sample)
predicted_class = iris.target_names[prediction[0]]
print(f"\n새로운 샘플 예측 결과: '{predicted_class}' 품종입니다.")
결과 설명Result Explanation
위 코드를 실행하면 다음과 같은 결과가 출력됩니다. 각 항목의 의미는 아래와 같습니다.Running the code above will produce the following output. The meaning of each item is as follows.
데이터셋 크기: (150, 4)Dataset size: (150, 4)
// 총 150개의 붓꽃 샘플 데이터가 있으며, 각 샘플은 4개의 특성(꽃받침 길이/너비, 꽃잎 길이/너비)을 가집니다.// There are 150 iris sample data in total, and each sample has 4 features (sepal length/width, petal length/width).
클래스 종류: ['setosa' 'versicolor' 'virginica']Class types: ['setosa' 'versicolor' 'virginica']
// 모델이 분류해야 할 붓꽃의 품종이 3가지(setosa, versicolor, virginica)임을 의미합니다.// This means the model needs to classify 3 species of iris (setosa, versicolor, virginica).
학습 데이터 크기: (105, 4)Training data size: (105, 4)
// 전체 데이터 150개 중 70%인 105개를 모델 학습에 사용합니다.// 70% of the total data (105 out of 150) is used for model training.
테스트 데이터 크기: (45, 4)Test data size: (45, 4)
// 나머지 30%인 45개를 학습된 모델의 성능을 평가하는 데 사용합니다.// The remaining 30% (45) is used to evaluate the trained model's performance.
모델 정확도: 1.0000Model accuracy: 1.0000
// 학습된 모델이 테스트 데이터 45개의 품종을 100% 정확하게 예측했음을 의미합니다.// This means the trained model predicted the species of the 45 test data samples with 100% accuracy.
새로운 샘플 예측 결과: 'setosa' 품종입니다.New sample prediction result: It is a 'setosa' species.
// 코드에 입력된 새로운 샘플 정보를 바탕으로, 모델이 'setosa' 품종일 것이라고 예측한 결과입니다.// Based on the new sample information provided in the code, the model predicted it to be the 'setosa' species.
이처럼 Vertex AI Workbench를 활용하면 복잡한 인프라 설정 없이도 신속하게 커스텀 머신러닝 모델을 개발하고 테스트할 수 있습니다.As you can see, Vertex AI Workbench allows you to quickly develop and test custom machine learning models without complex infrastructure setup.