This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import mysql.connector | |
| from mysql.connector import Error | |
| import tweepy | |
| import json | |
| from dateutil import parser | |
| import time | |
| import os | |
| import subprocess | |
| #importing file which sets env variable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Kmeans: | |
| """ K Means Clustering | |
| Parameters | |
| ----------- | |
| k: int , number of clusters | |
| seed: int, will be randomly set if None | |
| max_iter: int, number of iterations to run algorithm, default: 200 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Kmeans: | |
| """ K Means Clustering | |
| Parameters | |
| ----------- | |
| k: int , number of clusters | |
| seed: int, will be randomly set if None | |
| max_iter: int, number of iterations to run algorithm, default: 200 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def assign_clusters(self, data): | |
| """Compute distance of data from clusters and assign data point | |
| to closest cluster. | |
| Parameters | |
| ---------- | |
| data: array or matrix, number_rows, number_features | |
| Returns | |
| -------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def predict(self, data): | |
| """Predict which cluster data point belongs to | |
| Parameters | |
| ---------- | |
| data: array or matrix, number_rows, number_features | |
| Returns | |
| -------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| data = df['body_new'] | |
| tf_idf_vectorizor = TfidfVectorizer(stop_words = 'english',#tokenizer = tokenize_and_stem, | |
| max_features = 20000) | |
| tf_idf = tf_idf_vectorizor.fit_transform(data) | |
| tf_idf_norm = normalize(tf_idf) | |
| tf_idf_array = tf_idf_norm.toarray() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sklearn_pca = PCA(n_components = 2) | |
| Y_sklearn = sklearn_pca.fit_transform(tf_idf_array) | |
| test_e = Kmeans(3, 1, 600) | |
| fitted = test_e.fit_kmeans(Y_sklearn) | |
| predicted_values = test_e.predict(Y_sklearn) | |
| plt.scatter(Y_sklearn[:, 0], Y_sklearn[:, 1], c=predicted_values, s=50, cmap='viridis') | |
| centers = fitted.centroids | |
| plt.scatter(centers[:, 0], centers[:, 1],c='black', s=300, alpha=0.6); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.cluster import KMeans | |
| sklearn_pca = PCA(n_components = 2) | |
| Y_sklearn = sklearn_pca.fit_transform(tf_idf_array) | |
| kmeans = KMeans(n_clusters=3, max_iter=600, algorithm = 'auto') | |
| fitted = kmeans.fit(Y_sklearn) | |
| prediction = kmeans.predict(Y_sklearn) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np # linear algebra | |
| import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv) | |
| from sklearn.cluster import KMeans | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| from sklearn.decomposition import PCA | |
| from sklearn.preprocessing import normalize | |
| from sklearn.metrics import pairwise_distances | |
| import nltk | |
| import string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| number_clusters = range(1, 7) | |
| kmeans = [KMeans(n_clusters=i, max_iter = 600) for i in number_clusters] | |
| kmeans | |
| score = [kmeans[i].fit(Y_sklearn).score(Y_sklearn) for i in range(len(kmeans))] | |
| score | |
| plt.plot(number_clusters, score) | |
| plt.xlabel('Number of Clusters') |