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.metrics.pairwise import cosine_similarity | |
| max_sim = [0, 0] | |
| for i, sim in enumerate(cosine_similarity(tfs, search_tf)): | |
| if sim[0] > max_sim[1]: | |
| max_sim = [i, sim[0]] | |
| print(max_sim[1]) | |
| list_form[max_sim[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
| def prepare_search(text): | |
| return text.rstrip().lower().translate(str.maketrans('','',string.punctuation)) | |
| search = prepare_search("What’s the episode where Cartman turns someones parents into chili") | |
| search_tf = tfidf.transform([search]) |
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 | |
| def manual_cosine_similarity(a, b): | |
| prod = np.dot(a, b) | |
| mag = np.linalg.norm(a) * np.linalg.norm(b) | |
| return prod / mag |
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
| # Episode (1,1) | |
| [['moo', 0.53256875573025619], | |
| ['visitors', 0.37152571127407436], | |
| ['brother', 0.19616083922151151], | |
| ['dildo', 0.19585243229585761], | |
| ['cows', 0.19443034618806601]] | |
| # Episode (5, 4) | |
| [['scott', 0.56930533201219424], | |
| ['pubes', 0.41616028565538887], |
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 random | |
| i = random.randint(0, len(corpus)) | |
| print(list_form[i][0]) | |
| feature_names = tfidf.get_feature_names() | |
| response = tfidf.transform([corpus[i]]) | |
| keywords = [] | |
| for col in response.nonzero()[1]: | |
| keywords.append([feature_names[col], response[0, col]]) | |
| sorted(keywords, key= lambda x: -x[1])[:5] |
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 | |
| import nltk | |
| tfidf = TfidfVectorizer(tokenizer=nltk.word_tokenize, stop_words='english', min_df=2, max_df=0.5) | |
| tfs = tfidf.fit_transform(corpus) |
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 string | |
| for text in list_form: | |
| corpus.append(' '.join([l.rstrip().lower().translate(str.maketrans('','',string.punctuation)) for l in text[1]])) |
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 pandas as pd | |
| df = pd.read_csv('south_park.csv') | |
| by_episode = {} | |
| for i, row in df.iterrows(): | |
| key = (row.Season, row.Episode) | |
| try: |
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
| fig, ax = plt.subplots(1, 1) | |
| ser = pd.Series(e_samples/c_samples) | |
| # Make the CDF | |
| ser = ser.sort_values() | |
| ser[len(ser)] = ser.iloc[-1] | |
| cum_dist = np.linspace(0., 1., len(ser)) | |
| ser_cdf = pd.Series(cum_dist, index=ser) |
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
| sample_size = 100000 | |
| c_samples = pd.Series([c_distribution.rvs() for _ in range(sample_size)]) | |
| e_samples = pd.Series([e_distribution.rvs() for _ in range(sample_size)]) | |
| p_ish_value = 1.0 - sum(e_samples > c_samples)/sample_size | |
| # 0.046830000000000038 |
NewerOlder