-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyscikit-learn.py
69 lines (50 loc) · 1.85 KB
/
pyscikit-learn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# !! -- pip install -U scikit-learn numpy matplotlib -- !!
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, accuracy_score
from sklearn.datasets import load_iris, make_blobs
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.cluster import KMeans
# 예제 1: 회귀 분석
def linear_regression_example():
print("Example 1: Linear Regression")
x_train = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y_train = np.array([2, 4, 6, 8, 10])
model = LinearRegression()
model.fit(x_train, y_train)
x_test = np.array([6, 7, 8, 9, 10]).reshape(-1, 1)
y_test = np.array([12, 14, 16, 18, 20])
y_pred = model.predict(x_test)
print("Predictions:", y_pred)
mse = mean_squared_error(y_test, y_pred)
print("Mean Squared Error:", mse)
print("")
# 예제 2: 분류
def classification_example():
print("Example 2: Classification")
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
print("")
# 예제 3: 클러스터링
def clustering_example():
print("Example 3: Clustering")
X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.6, random_state=0)
kmeans = KMeans(n_clusters=4)
pred_y = kmeans.fit_predict(X)
plt.scatter(X[:, 0], X[:, 1], c=pred_y)
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=200, c='red')
plt.show()
print("")
# 모든 예제 실행
linear_regression_example()
classification_example()
clustering_example()