Skip to content

Instantly share code, notes, and snippets.

@ajitesh123
Created May 27, 2019 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajitesh123/c3c5ec91aabb285b2d2776cf51778630 to your computer and use it in GitHub Desktop.
Save ajitesh123/c3c5ec91aabb285b2d2776cf51778630 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 351,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3100, 468)\n"
]
}
],
"source": [
"#Implementing LDA \n",
"\n",
"from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer\n",
"from sklearn.decomposition import LatentDirichletAllocation\n",
"stop_words.update([\"shri\",\"also\", \"towards\"])\n",
"\n",
"tf_vectorizer = CountVectorizer(strip_accents = 'unicode',\n",
" stop_words = 'english',\n",
" lowercase = True,\n",
" token_pattern = r'\\b[a-zA-Z]{3,}\\b',\n",
" max_df = 0.5, \n",
" min_df = 10)\n",
"dtm_tf = tf_vectorizer.fit_transform(data['Tweets_Cln'])\n",
"print(dtm_tf.shape)"
]
},
{
"cell_type": "code",
"execution_count": 352,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3100, 468)\n"
]
}
],
"source": [
"tfidf_vectorizer = TfidfVectorizer(**tf_vectorizer.get_params())\n",
"dtm_tfidf = tfidf_vectorizer.fit_transform(data['Tweets_Cln'])\n",
"print(dtm_tfidf.shape)"
]
},
{
"cell_type": "code",
"execution_count": 353,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"LatentDirichletAllocation(batch_size=128, doc_topic_prior=None,\n",
" evaluate_every=-1, learning_decay=0.7, learning_method=None,\n",
" learning_offset=10.0, max_doc_update_iter=100, max_iter=10,\n",
" mean_change_tol=0.001, n_components=10, n_jobs=1, n_topics=5,\n",
" perp_tol=0.1, random_state=0, topic_word_prior=None,\n",
" total_samples=1000000.0, verbose=0)"
]
},
"execution_count": 353,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# for TF DTM\n",
"lda_tf = LatentDirichletAllocation(n_topics=5, random_state=0)\n",
"lda_tf.fit(dtm_tf)\n",
"# for TFIDF DTM\n",
"lda_tfidf = LatentDirichletAllocation(n_topics=5, random_state=0)\n",
"lda_tfidf.fit(dtm_tfidf)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment