기본 콘텐츠로 건너뛰기

Python Deep Learning 예제

예제
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=150, n_features=2, centers=3, random_state=0)

X.shape # (150, 2)
y.shape # (150,)

import pandas as pd
pd.Series(y).value_counts()
1    50
0    50
2    50
dtype: int64

from sklearn.cluster import KMeans
km = KMeans(n_clusters=3, random_state=0)

# 비지도 학습
pred = km.fit_predict(X)

pd.Series(pred).value_counts()
2    52
1    50
0    48
dtype: int64

# 한글 깨짐 없이 나오게 설정
from matplotlib import rcParams # 인코딩 폰트 설정
rcParams['font.family'] = 'New Gulim'
rcParams['font.size'] = 10

plt.figure(figsize=(10,4))
plt.subplot(1,2,1)
plt.scatter(X[:,0],X[:,1],c=pred,s=10)
plt.title('추정')
plt.subplot(1,2,2)
plt.scatter(X[:,0],X[:,1],c=y,s=10)
plt.title('정답')
plt.show()

# 학습된 클러스터들의 centroids를 화면에 표시해본다
centroids = km.cluster_centers_
centroids
array([[ 0.91563959,  4.44602508],
       [-1.73388472,  3.00577412],
       [ 2.07417555,  1.04097145]])

# 적색 별 모양의 표시를 각 centroid에 해본다
plt.scatter(X[:,0],X[:,1],c=pred,s=10)
plt.scatter(centroids[:,0],centroids[:,1],c='r',marker='*')
plt.title('centroid 표시')
plt.show()

# 앨보우 차트
# 클러스터의 갯수와 inertia의 값을 라인차트에 표시한 것수
# inertia : 센트로이드와 클러스터 소속 값들의 거리의 제곱의 합
inertia = []
for i in range(1,11):
    km = KMeans(n_clusters=i, random_state=0)
    km.fit(X)
    inertia.append(km.inertia_)

plt.plot(range(1,11), inertia, marker='o')
plt.xlabel("Number of Clusters")
plt.ylabel("Inertia")
plt.show()
# 최적의 클러스터 수는 꺾이는 곳으로 정하면 된다

이 블로그의 인기 게시물

Python Sklearn make_blobs

from sklearn.datasets import make_blobs 예제 X, y = make_blobs(n_samples=500, centers=3, n_features=2, random_state=0) # 500개의 점을 3개로 모이게 한다, 변수는 2개, 무작위 상태는 0 X.shape, y.shape # ((500, 2), (500,)) plt.scatter(X[:,0],X[:,1],c=y,s=5) plt.show() # 학습 데이터 나누기 from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=.25, random_state=0) x_train.shape, x_test.shape, y_train.shape, y_test.shape # ((375, 2), (125, 2), (375,), (125,)) # 지도 학습 하기 from sklearn.linear_model import LogisticRegression logisticReg = LogisticRegression(max_iter=5000) # 기본 반복 100 logisticReg.fit(x_train, y_train) # 추정하기 pred = logisticReg.predict(X) # 결정계수 logisticReg.score(x_test, y_test) # 0.92 # 한글 깨짐 없이 나오게 설정 from matplotlib import rcParams # 인코딩 폰트 설정 rcParams['font.family'] = 'New Gulim' rcParams['font.size'] = 10 # 산점도 plt.figure(figsize=(10,4)) plt.subplot(1,2, 1) plt.scatter(X[:,0],X[:,1],c=y) plt.title('정답') plt.su...

Grid 정렬

  .container { display : grid ; gap : 22px ; width : 1000px ; grid-template-columns : repeat ( auto-fit , 150px ); margin : auto ; justify-content : center ; } .container {      display : grid ; gap : 22px ; grid-template-columns : repeat ( auto-fit , minmax ( 250px , 1fr )); }

Python 변수

지역 변수 a 전역 변수 밖에서 선언한 변수를 사용할 때 앞에 global 을 붙여준다     global a