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
| naive_bayes.fit(training_data,y_train) | |
| predictions = naive_bayes.predict(testing_data) | |
| # accuracy: 0.7391304347826086 | |
| print('accuracy: {}'.format(accuracy_score(y_test, predictions))) | |
| #using cross-validation | |
| X_whole = count_vector.fit_transform(covid_data['prep_arg']) | |
| y_whole = covid_data['concern'] |
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
| count_vectorizer = CountVectorizer(binary=True) | |
| #fit training data | |
| training_data = count_vectorizer.fit_transform(X_train) | |
| #transform test data | |
| testing_data = count_vectorizer.transform(X_test) |
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
| X_train, X_test, y_train, y_test = train_test_split( | |
| covid_data['prep_arg'], | |
| covid_data['concern'], | |
| test_size=0.2, | |
| random_state=50 | |
| ) |
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
| covid_data = pd.read_csv('covid_vacc_concerns.csv') | |
| covid_data['prep_arg'] = covid_data['arg'].apply(preprocess) |
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
| vocabulary | apples | are | great | but | so | pears | however | sometimes | I | feel | like | oranges | and | on | other | days | bananas | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| index | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | |
| Sentence 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | |
| Sentence 2 | 0 | 2 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | |
| Sentence 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.stem import WordNetLemmatizer, PorterStemmer, SnowballStemmer | |
| stemmer = PorterStemmer() | |
| lemmatizer = WordNetLemmatizer() | |
| word = "considering" | |
| stemmed_word = stemmer.stem(word) | |
| lemmatised_word = lemmatizer.lemmatize(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
| import re | |
| #letters only | |
| raw_text = "this is a test. To demonstrate 2 regex expressions!!" | |
| letters_only_text = re.sub("[^a-zA-Z]", " ", raw_text) | |
| #keep numbers | |
| letnum_text = re.sub("[^a-zA-Z0-9\s]+", " ",raw_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
| from nltk.util import ngrams, word_tokenize, bigrams, trigrams | |
| sen = "Dummy sentence to demonstrate bigrams" | |
| nltk_tokens = word_tokenize(sen) #using tokenize from NLKT and not split() because split() does not take into account punctuation | |
| #splitting sentence into bigrams and trigrams | |
| print(list(bigrams(nltk_tokens))) | |
| print(list(trigrams(nltk_tokens))) | |
| #creating a dictionary that shows occurances of n-grams in 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
| from nltk.corpus import stopwords | |
| from nltk.tokenize import word_tokenize | |
| example_sent = "This is a sample sentence, showing off the stop words filtration." | |
| stop_words = set(stopwords.words('english')) | |
| word_tokens = word_tokenize(example_sent) | |
| filtered_sentence = [w for w in word_tokens if not w in stop_words] | |
| print(filtered_sentence) |
NewerOlder