๐ ๋จธ์ ๋ฌ๋ ๋ ๋ฒ์งธ ์ฌ์ : ์์ธก๊ณผ ๋ถ๋ฅ์ ์ธ๊ณ๋ก
๋ค์ด๊ฐ๋ฉฐ
๋ถ์คํธ์ฝ์ค ๋จธ์ ๋ฌ๋ ํ์ด์ฌ ๊ณผ์ ์ ๋ ๋ฒ์งธ ํํธ๋ฅผ ๋ง์ณค๋ค. ์ ํ ํ๊ท, ๋ก์ง์คํฑ ํ๊ท, ๊ทธ๋ฆฌ๊ณ ๋ถ๋ฅ ์๊ณ ๋ฆฌ์ฆ๊น์ง ๋ฐฐ์ฐ๋ฉด์ ์ ์ ๋จธ์ ๋ฌ๋์ ์ค์ ์์ฉ์ ๊ฐ๊น์์ง๊ณ ์๋ค. ์ด๋ฒ์๋ "ํ ์ค์ฉ ์ดํดํ๋ฉฐ ์ฝ๋๋ก ๊ตฌํํ์"๋ ๋ง์๊ฐ์ง์ผ๋ก ์ํ๋ค.
4. ์ ํ ํ๊ท(Linear Regression): ๋ฐ์ดํฐ ์ ์ ํ ๊ด๊ณ ์ฐพ๊ธฐ
์ ํ ํ๊ท๋ ๋ ๋ฆฝ ๋ณ์์ ์ข ์ ๋ณ์ ๊ฐ์ ์ ํ ๊ด๊ณ๋ฅผ ๋ชจ๋ธ๋งํ๋ ๊ธฐ๋ฒ์ด๋ค. ์ง ๊ฐ๊ฒฉ ์์ธก, ํ๋งค๋ ์์ธก ๋ฑ ์ฐ์์ ์ธ ๊ฐ์ ์์ธกํ๋ ๋ฐ ๋๋ฆฌ ์ฌ์ฉ๋๋ค.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
# ์ํ ๋ฐ์ดํฐ ์์ฑ (์ง ํฌ๊ธฐ์ ๊ฐ๊ฒฉ)
np.random.seed(42)
house_size = np.random.normal(150, 40, 100) # ํ๊ท 150mยฒ, ํ์คํธ์ฐจ 40mยฒ์ ์ง ํฌ๊ธฐ 100๊ฐ
noise = np.random.normal(0, 50, 100)
house_price = 1500 * house_size + 10000 + noise # ๊ฐ๊ฒฉ = 1500 ร ํฌ๊ธฐ + 10000 + ๋
ธ์ด์ฆ
# ๋ฐ์ดํฐ ๋ถํ
X = house_size.reshape(-1, 1)
y = house_price
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# ์ ํ ํ๊ท ๋ชจ๋ธ ํ์ต
model = LinearRegression()
model.fit(X_train, y_train)
# ์์ธก ๋ฐ ํ๊ฐ
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"๊ณ์(๊ธฐ์ธ๊ธฐ): {model.coef_[0]:.2f}")
print(f"์ ํธ: {model.intercept_:.2f}")
print(f"ํ๊ท ์ ๊ณฑ ์ค์ฐจ(MSE): {mse:.2f}")
print(f"๊ฒฐ์ ๊ณ์(Rยฒ): {r2:.2f}")
# ๊ทธ๋ํ๋ก ์๊ฐํ
plt.scatter(X_test, y_test, color='black', label='์ค์ ๋ฐ์ดํฐ')
plt.plot(X_test, y_pred, color='blue', linewidth=3, label='์ ํ ํ๊ท์ ')
plt.xlabel('์ง ํฌ๊ธฐ (mยฒ)')
plt.ylabel('์ง ๊ฐ๊ฒฉ')
plt.title('์ ํ ํ๊ท: ์ง ํฌ๊ธฐ์ ๋ฐ๋ฅธ ๊ฐ๊ฒฉ ์์ธก')
plt.legend()
plt.show()
์ ํ ํ๊ท ๋ชจ๋ธ์ ๋จ์ํ์ง๋ง ๊ฐ๋ ฅํ๋ค. ํนํ ๊ฒฐ์ ๊ณ์(Rยฒ)๊ฐ ๋๊ฒ ๋์ค๋ฉด ๋ชจ๋ธ์ด ๋ฐ์ดํฐ๋ฅผ ์ ์ค๋ช ํ๋ค๋ ์๋ฏธ๋ค. ํ์ง๋ง ๋ฐ์ดํฐ์ ๋น์ ํ ๊ด๊ณ๊ฐ ์๊ฑฐ๋ ํน์ง ๊ฐ ์ํธ์์ฉ์ด ์๋ค๋ฉด ๋คํญ ํ๊ท๋ ๋ค๋ฅธ ๋น์ ํ ๋ชจ๋ธ์ ๊ณ ๋ คํด์ผ ํ๋ค.
5. ๋ก์ง์คํฑ ํ๊ท(Logistic Regression): ํ๋ฅ ๋ก ๋ถ๋ฅํ๊ธฐ
๋ก์ง์คํฑ ํ๊ท๋ ์ด๋ฆ์ 'ํ๊ท'๊ฐ ๋ค์ด๊ฐ์ง๋ง ์ฌ์ค์ ๋ถ๋ฅ ์๊ณ ๋ฆฌ์ฆ์ด๋ค. ํนํ ์ด์ง ๋ถ๋ฅ(๋ ํด๋์ค๋ก ๋๋๊ธฐ)์ ํ์ํ๋ฉฐ, ์ถ๋ ฅ๊ฐ์ 0๊ณผ 1 ์ฌ์ด์ ํ๋ฅ ๋ก ๋ณํํ๋ค.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_classification
# ์ด์ง ๋ถ๋ฅ์ฉ ์ํ ๋ฐ์ดํฐ ์์ฑ (์: ์ํ ํฉ๊ฒฉ/๋ถํฉ๊ฒฉ)
X, y = make_classification(n_samples=300, n_features=2, n_redundant=0,
n_clusters_per_class=1, random_state=42)
# ๋ฐ์ดํฐ ๋ถํ
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# ๋ฐ์ดํฐ ์ค์ผ์ผ๋ง (๋ก์ง์คํฑ ํ๊ท์์ ์ค์)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# ๋ก์ง์คํฑ ํ๊ท ๋ชจ๋ธ ํ์ต
model = LogisticRegression(random_state=42)
model.fit(X_train_scaled, y_train)
# ์์ธก ๋ฐ ํ๊ฐ
y_pred = model.predict(X_test_scaled)
y_prob = model.predict_proba(X_test_scaled)[:, 1] # ํด๋์ค 1์ ํ๋ฅ
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
print(f"์ ํ๋: {accuracy:.4f}")
print("ํผ๋ ํ๋ ฌ:")
print(conf_matrix)
print("\n๋ถ๋ฅ ๋ณด๊ณ ์:")
print(classification_report(y_test, y_pred))
# ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ
def plot_decision_boundary(X, y, model, scaler):
h = 0.02 # ๊ฒฉ์ ๊ฐ๊ฒฉ
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# ๊ฒฉ์์ ์ ๋ํ ์์ธก
Z = model.predict(scaler.transform(np.c_[xx.ravel(), yy.ravel()]))
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.3)
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', marker='o')
plt.xlabel('ํน์ฑ 1')
plt.ylabel('ํน์ฑ 2')
plt.title('๋ก์ง์คํฑ ํ๊ท์ ๊ฒฐ์ ๊ฒฝ๊ณ')
plt.show()
plot_decision_boundary(X_test, y_test, model, scaler)
๋ก์ง์คํฑ ํ๊ท๋ ํด์์ด ์ฝ๊ณ ํ๋ฅ ๊ฐ์ ์ป์ ์ ์์ด ์ํ ํ๊ฐ๋ ๊ณ ๊ฐ ์ดํ ์์ธก ๋ฑ์ ์ ์ฉํ๋ค. ๋ํ L1, L2 ์ ๊ทํ๋ฅผ ํตํด ๊ณผ์ ํฉ์ ๋ฐฉ์งํ ์ ์๋ค.
6. ๋ถ๋ฅ(Classification): ๋ค์ํ ์๊ณ ๋ฆฌ์ฆ ํ์
๋ถ๋ฅ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฏธ๋ฆฌ ์ ์๋ ์นดํ ๊ณ ๋ฆฌ๋ก ๊ตฌ๋ถํ๋ ๊ณผ์ ๋ค. ์ด์ง ๋ถ๋ฅ๋ฅผ ๋์ด ๋ค์ค ๋ถ๋ฅ ๋ฌธ์ ๋ ์์ฃผ ๋ง์ฃผํ๊ฒ ๋๋ค. ์ฌ๋ฌ ๋ถ๋ฅ ์๊ณ ๋ฆฌ์ฆ์ ๋น๊ตํด๋ณด์.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_wine
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report
import pandas as pd
import seaborn as sns
# ์์ธ ๋ฐ์ดํฐ์
๋ก๋ (๋ค์ค ๋ถ๋ฅ)
wine = load_wine()
X = wine.data
y = wine.target
# ๋ฐ์ดํฐ ๋ถํ
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# ์ค์ผ์ผ๋ง
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# ๋ค์ํ ๋ถ๋ฅ ์๊ณ ๋ฆฌ์ฆ
classifiers = {
'๋ก์ง์คํฑ ํ๊ท': LogisticRegression(max_iter=1000, random_state=42),
'๊ฒฐ์ ํธ๋ฆฌ': DecisionTreeClassifier(random_state=42),
'๋๋ค ํฌ๋ ์คํธ': RandomForestClassifier(n_estimators=100, random_state=42),
'SVM': SVC(random_state=42),
'K-์ต๊ทผ์ ์ด์': KNeighborsClassifier(n_neighbors=5)
}
# ๊ฐ ์๊ณ ๋ฆฌ์ฆ ํ๋ จ ๋ฐ ํ๊ฐ
results = {}
for name, clf in classifiers.items():
clf.fit(X_train_scaled, y_train)
y_pred = clf.predict(X_test_scaled)
accuracy = accuracy_score(y_test, y_pred)
cv_scores = cross_val_score(clf, X_train_scaled, y_train, cv=5)
results[name] = {
'accuracy': accuracy,
'cv_mean': cv_scores.mean(),
'cv_std': cv_scores.std()
}
print(f"\n{name}:")
print(f"ํ
์คํธ ์ ํ๋: {accuracy:.4f}")
print(f"๊ต์ฐจ ๊ฒ์ฆ ์ ํ๋: {cv_scores.mean():.4f} ยฑ {cv_scores.std():.4f}")
print(classification_report(y_test, y_pred, target_names=wine.target_names))
# ๊ฒฐ๊ณผ ๋น๊ต ์๊ฐํ
results_df = pd.DataFrame({
'Algorithm': list(results.keys()),
'Test Accuracy': [results[name]['accuracy'] for name in results],
'CV Accuracy': [results[name]['cv_mean'] for name in results]
})
plt.figure(figsize=(12, 6))
sns.barplot(x='Algorithm', y='value', hue='variable',
data=pd.melt(results_df, id_vars='Algorithm',
value_vars=['Test Accuracy', 'CV Accuracy']))
plt.title('๋ค์ํ ๋ถ๋ฅ ์๊ณ ๋ฆฌ์ฆ์ ์ฑ๋ฅ ๋น๊ต')
plt.ylim(0.7, 1.0)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
# ํน์ฑ ์ค์๋ (๋๋ค ํฌ๋ ์คํธ ๊ธฐ์ค)
rf = classifiers['๋๋ค ํฌ๋ ์คํธ']
feature_importance = pd.DataFrame({
'Feature': wine.feature_names,
'Importance': rf.feature_importances_
}).sort_values('Importance', ascending=False)
plt.figure(figsize=(10, 6))
sns.barplot(x='Importance', y='Feature', data=feature_importance)
plt.title('ํน์ฑ ์ค์๋ (๋๋ค ํฌ๋ ์คํธ)')
plt.tight_layout()
plt.show()
๋ค์ํ ๋ถ๋ฅ ์๊ณ ๋ฆฌ์ฆ์ ๋น๊ตํด๋ณด๋, ๋ฐ์ดํฐ์ ์ ๋ฐ๋ผ ์ฑ๋ฅ ์ฐจ์ด๊ฐ ์๊ธด๋ค. ๋ชจ๋ธ ์ ํ์ ์ ํ๋๋ฟ๋ง ์๋๋ผ ํด์ ๊ฐ๋ฅ์ฑ, ํ์ต/์์ธก ์๋, ๊ณผ์ ํฉ ๋ฐฉ์ง ๋ฅ๋ ฅ ๋ฑ์ ์ข ํฉ์ ์ผ๋ก ๊ณ ๋ คํด์ผ ํ๋ค.
ํ์ตํ๋ฉด์ ๋๋ ์
1. ์ ์ ํ ์๊ณ ๋ฆฌ์ฆ ์ ํ์ ์ค์์ฑ
๋ชจ๋ ๋ฐ์ดํฐ์ ๋ง๋ฅ์ธ ์๊ณ ๋ฆฌ์ฆ์ ์๋ค
๋ฐ์ดํฐ ํน์ฑ๊ณผ ๋ชฉ์ ์ ๋ง๋ ์๊ณ ๋ฆฌ์ฆ ์ ํ์ด ์ค์
์ฌ๋ฌ ์๊ณ ๋ฆฌ์ฆ์ ๋น๊ตํ๋ ์ต๊ด์ ๋ค์ด์
2. ์ ์ฒ๋ฆฌ์ ํผ์ฒ ์์ง๋์ด๋ง์ ํ
์ค์ผ์ผ๋ง์ด ๋ชจ๋ธ ์ฑ๋ฅ์ ํฌ๊ฒ ์ํฅ์ ๋ฏธ์น๋ค
ํผ์ฒ ์ ํ๊ณผ ๊ฐ๊ณต์ ๋ ๋ง์ ์๊ฐ์ ํฌ์ํ์
๋๋ฉ์ธ ์ง์์ ์ ๊ทน ํ์ฉํ์
3. ๋ชจ๋ธ ํ๊ฐ๋ ๋ค๊ฐ๋๋ก
์ ํ๋๋ง์ผ๋ก๋ ๋ถ์กฑํ๋ค
ํผ๋ ํ๋ ฌ, ์ ๋ฐ๋, ์ฌํ์จ, F1 ์ค์ฝ์ด ๋ฑ ๋ค์ํ ์งํ๋ฅผ ์ดํด๋ณด์
๊ต์ฐจ ๊ฒ์ฆ์ ํตํด ์ผ๋ฐํ ์ฑ๋ฅ์ ํ์ธํ์
๋ง์น๋ฉฐ
๋จธ์ ๋ฌ๋ ์๊ณ ๋ฆฌ์ฆ์ ๊ฐ์์ ํน์ฑ๊ณผ ์ฅ๋จ์ ์ด ์๋ค. ์ ํ ํ๊ท๋ถํฐ ๋ณต์กํ ๋ถ๋ฅ ์๊ณ ๋ฆฌ์ฆ๊น์ง, ๊ฐ ๋ชจ๋ธ์ ์๋ฆฌ๋ฅผ ์ดํดํ๊ณ ์ ์ฌ์ ์์ ํ์ฉํ๋ ๋ฅ๋ ฅ์ด ์ค์ํ๋ค. ์ด๋ก ๊ณผ ์ค์ต์ ๋ณํํ๋ฉด์ ์ง๊ด์ ํค์๋๊ฐ๋ ์ค์ด๋ค.
"๋ชจ๋ธ์ ๋๊ตฌ์ผ ๋ฟ, ๊ฒฐ๊ตญ ์ค์ํ ๊ฒ์ ๋ฌธ์ ์ ์์ ๋ฐ์ดํฐ๋ค" - ๋ฐ์ดํฐ ์ฌ์ด์ธํฐ์คํธ์ ํต์ฐฐ