<jupyter_start><jupyter_text>가우시안 혼합 모형(mixture)<jupyter_code>%matplotlib inline
from numpy.random import randn

n_samples = 500

mu1 = np.array([0, 0])
mu2 = np.array([-6, 3])
sigma1 = np.array([[0., -0.1], [1.7, .4]])
sigma2 = np.eye(2)

np.random.seed(0)
X = np.r_[1.0 * np.dot(randn(n_samples, 2), sigma1) + mu1,
          0.7 * np.dot(randn(n_samples, 2), sigma2) + mu2,
         ]
plt.scatter(X[:, 0], X[:, 1], s=5)
plt.show()
from sklearn.mixture import GaussianMixture
model = GaussianMixture(n_components=2, init_params='random', random_state=0, max_iter=1)
model.fit(X)
gamma = model.predict_proba(X)
gamma
plt.scatter(X[:, 0], X[:, 1], s=50, linewidth=1, edgecolors="b", cmap=plt.cm.binary, c=gamma[:, 0])
plt.show()
def plot_gaussianmixture(n, k=10):
    model = GaussianMixture(n_components=2, init_params='random', random_state=1, tol=1e-9, max_iter=n)
    model.fit(X)
    gamma = model.predict_proba(X)
    plt.scatter(X[:, 0], X[:, 1], s=50, linewidth=1, edgecolors="b", cmap=plt.cm.binary, c=gamma[:, 0])
    plt.show()
    return model
plot_gaussianmixture(5) # 5번 돌려본다.
plot_gaussianmixture(10)
plot_gaussianmixture(15)
plot_gaussianmixture(20)
model1 = GaussianMixture(n_components=2, init_params='random', random_state=0, max_iter=20)
model1.fit(X)
gamma1 = model1.predict_proba(X)
gamma1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1798507462686567, 'fraction_numerical': 0.05223880597014925, 'mean_word_length': 5.509708737864078, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '90360', 'license': 'MIT', 'n_tokens_mistral': 608, 'n_tokens_neox': 591, 'n_words': 105, 'path': 'machinelearning/clustering/gaussian_mixture.ipynb', 'repo_name': 'Junhojuno/TIL', 'size': '1340'}