<jupyter_start><jupyter_text>2D Gaussian<jupyter_code>mu_1 = np.array([1, 0])
mu_2 = np.array([0, 1.5])
sigma_1 = np.array([[0.9, 0.4], [0.4, 0.9]])
sigma_2 = np.array([[0.9, 0.4], [0.4, 0.9]])
gaussian_1 = np.random.multivariate_normal(mu_1, sigma_1, 500)
gaussian_2 = np.random.multivariate_normal(mu_2, sigma_2, 500)
plt.scatter(gaussian_1[:, 0], gaussian_1[:, 1], marker='.')
plt.scatter(gaussian_2[:, 0], gaussian_2[:, 1], marker='.')
x = np.concatenate([gaussian_1, gaussian_2])
def plot_result(x, labels, k):
    for i in range(k):
        plt.scatter(x[labels == i][:, 0], x[labels == i][:, 1], marker='.')
    for center in model.centers:
        plt.scatter(center[0], center[1], c='k', marker='x')<jupyter_output><empty_output><jupyter_text>KMeans: 2 clusters w/o given centers<jupyter_code>k = 2

model = KMeans(k)
centers, labels, n_iter = model.fit(x)

plot_result(x, labels, k)<jupyter_output><empty_output><jupyter_text>KMeans: 2 clusters w/ given centers<jupyter_code>k = 2
c = ((10, 10), (-10, -10))

model = KMeans(k, c)
centers, labels, n_iter = model.fit(x)

plot_result(x, labels, k)<jupyter_output><empty_output><jupyter_text>KMeans: 4 clusters w/ given centers<jupyter_code>k = 4
c = ((10, 10), (-10, -10), (10, -10), (-10, 10))

model = KMeans(k, c)
centers, labels, n_iter = model.fit(x)

plot_result(x, labels, k)<jupyter_output><empty_output><jupyter_text>TD-IDF<jupyter_code>model = TFIDF()
tfidf = model.fit('p2/Amazon_Reviews.csv')

img = (tfidf / tfidf.max()) * 255
plt.imshow(img)<jupyter_output><empty_output><jupyter_text>5 positive and 5 negative words<jupyter_code>postive_words = ["amazing", "favourite", "best", "good", "great"]
negative_words = ["bad", "fake", "poor", "disappointed", "negative"]
selected = postive_words + negative_words
tfidf = model.fit('Amazon_Reviews.csv', selected)

img = (tfidf / tfidf.max()) * 255
plt.imshow(img)<jupyter_output><empty_output><jupyter_text>TF-IDF with KMeans<jupyter_code>postive_vector = np.sum(tfidf[:, :5], axis=1, keepdims=True)
negative_vector = np.sum(tfidf[:, 5:], axis=1, keepdims=True)

docs_vector = np.concatenate((postive_vector, negative_vector), axis=1)
plt.scatter(docs_vector[:, 0], docs_vector[:, 1], marker='.')
k = 2

model = KMeans(k)
centers, labels, n_iter = model.fit(docs_vector)

plot_result(docs_vector, labels, k)
k = 3

model = KMeans(k)
centers, labels, n_iter = model.fit(docs_vector)

plot_result(docs_vector, labels, k)
k = 4

model = KMeans(k)
centers, labels, n_iter = model.fit(docs_vector)

plot_result(docs_vector, labels, k)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1825242718446602, 'fraction_numerical': 0.03922330097087379, 'mean_word_length': 6.2974504249291785, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '632774', 'license': 'MIT', 'n_tokens_mistral': 1152, 'n_tokens_neox': 1087, 'n_words': 210, 'path': 'Assignments/Homework 1/test.ipynb', 'repo_name': 'linbo0518/CSE-5334-Data-Mining', 'size': '2575'}