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
| data = pd.read_csv("./data.csv") #importing files using pandas | |
| data.head(10) #shows top 10 dataset items |
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
| #importing required libraries & dataset for the project | |
| import pandas as pd | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from sklearn import datasets | |
| from sklearn import preprocessing | |
| from sklearn.preprocessing import StandardScaler | |
| from sklearn.preprocessing import MinMaxScaler |
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
| text = ["The quick quick quick fox jumped over a big dog"] | |
| # encode document | |
| vector = vectorizer.transform(text) | |
| # summarize encoded vector | |
| print(vector.shape) | |
| print(vector.toarray()) | |
| OUTPUT: |
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 | |
| # list of text documents | |
| text = ["The quick brown fox jumped over the lazy dog.", | |
| "The dog.", | |
| "The fox"] | |
| # create the transform | |
| vectorizer = TfidfVectorizer() | |
| # tokenize and build vocab |
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 CountVectorizer | |
| # list of text documents | |
| text = ["The quick brown fox jumped over the lazy dog."] | |
| # create the transform | |
| vectorizer = CountVectorizer() | |
| # tokenize and build vocab | |
| vectorizer.fit(text) |
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
| #BOW ENCODING | |
| one_hot_encoding = [] | |
| for row in document_corpus: | |
| row_encoding = [] | |
| split = row.split(" ") | |
| for word in data_corpus: | |
| count = split.count(word) | |
| if word in split: | |
| row_encoding.append(count) | |
| else: |
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
| #BINARY BOW | |
| one_hot_encoding = [] | |
| for row in document_corpus: | |
| row_encoding = [] | |
| split = row.split(" ") | |
| for word in data_corpus: | |
| if word in split: | |
| row_encoding.append(1) | |
| else: | |
| row_encoding.append(0) |
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
| res = len(max(document_corpus, key = len).split(" ")) | |
| index_based_encoding=[] | |
| for row in document_corpus: | |
| row_encoding = [] | |
| split = row.split(" ") | |
| for i in range(res): | |
| if i <= len(split)-1: | |
| row_encoding.append(data_corpus.index(split[i])+1) | |
| else: | |
| row_encoding.append(0) |
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
| document_corpus = ["this is good phone phone" , | |
| "this is bad mobile mobile" , | |
| "she is good good cat" , | |
| "he has bad temper temper" , | |
| "this mobile phone phone is not good good"] | |
| data_corpus = set() | |
| for row in document_corpus: | |
| for word in row.split(" "): | |
| if word not in data_corpus: | |
| data_corpus.add(word) |
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 nltk.tokenize import word_tokenize | |
| from nltk.stem.lancaster import LancasterStemmer | |
| # stemming of words | |
| lancaster = LancasterStemmer() | |
| stemmed = [lancaster.stem(word) for word in words] | |
| print(stemmed[:100]) | |
| Output: | |
| ['albert', 'einstein', 'wid', 'celebr', 'on', 'bril', 'sci', 'ev', 'liv'] |
NewerOlder