Skip to content

Instantly share code, notes, and snippets.

from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
steps = [
("vectorizer", TfidfVectorizer(stop_words=my_stopwords)),
("rf", RandomForestClassifier())
]
# instatiating the pipeline
import pandas as pd
from rake_nltk import Rake
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import CountVectorizer
df = pd.read_csv('https://query.data.world/s/uikepcpffyo2nhig52xxeevdialfl7')
df = df[['Title','Genre','Director','Actors','Plot']]
df.head()
# initializing the new column
df['Key_words'] = ""
for index, row in df.iterrows():
plot = row['Plot']
# instantiating Rake, by default it uses english stopwords from NLTK
# and discards all puntuation characters as well
r = Rake()
# instantiating and generating the count matrix
count = CountVectorizer()
count_matrix = count.fit_transform(df['bag_of_words'])
# generating the cosine similarity matrix
cosine_sim = cosine_similarity(count_matrix, count_matrix)
# creating a Series for the movie titles so they are associated to an ordered numerical
# list I will use in the function to match the indexes
indices = pd.Series(df.index)
# defining the function that takes in movie title
# as input and returns the top 10 recommended movies
def recommendations(title, cosine_sim = cosine_sim):
# initializing the empty list of recommended movies
recommended_movies = []
data = []
for event in event_types:
event_data = dict(
lat = df.loc[df['EVENT_TYPE'] == event,'BEGIN_LAT'],
lon = df.loc[df['EVENT_TYPE'] == event,'BEGIN_LON'],
name = event,
marker = dict(size = 8, opacity = 0.5),
type = 'scattermapbox'
)
data.append(event_data)
annotations = [dict(
# text I want to display. I used <br> to break it into two lines
text = 'All US storm events that caused more than $50k of economic damage,<br> from 2000 until today',
# font and border characteristics
font = dict(color = '#FFFFFF', size = 14), borderpad = 10,
# positional arguments
x = 0.05, y = 0.05, xref = 'paper', yref = 'paper', align = 'left',
layout = dict(
height = 800,
# top, bottom, left and right margins
margin = dict(t = 0, b = 0, l = 0, r = 0),
font = dict(color = '#FFFFFF', size = 11),
paper_bgcolor = '#000000',
mapbox = dict(
# here you need the token from Mapbox
accesstoken = mapbox_access_token,
bearing = 0,
updatemenus=list([
# drop-down 1: map styles menu
# buttons containes as many dictionaries as many alternative map styles I want to offer
dict(
buttons=list([
dict(
args=['mapbox.style', 'dark'],
label='Dark',
method='relayout'
),
{
"folder_title": "Cookiecutter Example",
"notebook_title": "Generate dataframe with random integer entries and calculate ",
"n_columns": 5,
"n_rows": 10,
"min_value_in_df": 0,
"max_value_in_df": 100,
"numpy_function_to_calculate": "mean",
"calculate_over_rows_or_columns": "columns",
"file_name": "results",