본문으로 바로가기
728x90
반응형

1. NumPy 예제

import numpy as np

# 1차원 배열 생성
a = np.array([1, 2, 3])
print(a)

# 2차원 배열 생성
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)

# 배열의 형태 확인
print(a.shape)
print(b.shape)

# 배열의 데이터 타입 확인
print(a.dtype)
print(b.dtype)

# 배열 연산
c = np.array([1, 2, 3])
d = np.array([4, 5, 6])

print(c + d)
print(c - d)
print(c * d)
print(c / d)


2.  Pandas 예제
import pandas as pd

# Series 생성
s = pd.Series([1, 3, 5, np.nan, 6, 8])
print(s)

# DataFrame 생성
df = pd.DataFrame({
    "name": ["John", "Mike", "Sarah"],
    "age": [25, 30, 35],
    "gender": ["male", "male", "female"]
})
print(df)

# DataFrame 정보 출력
print(df.info())

# DataFrame 통계 정보 출력
print(df.describe())

# DataFrame 정렬
print(df.sort_values(by="age"))

# DataFrame 필터링
print(df[df["age"] > 30])


3. Scikit-learn 예제
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

# iris 데이터셋 로드
iris = load_iris()

# 데이터셋 분할
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)

# Decision Tree 모델 생성 및 학습
model = DecisionTreeClassifier()
model.fit(X_train, y_train)

# 예측 결과 출력
y_pred = model.predict(X_test)
print(y_pred)

# 정확도 출력
accuracy = accuracy_score(y_test, y_pred)
print(accuracy)

728x90
반응형