This file contains 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 = px.line(df_g7_melt, x="DATE", y="VALUE", color='COUNTRY', title='Real Residential Property Prices - All G7 Countries') | |
fig.update_layout(title=dict(x=0.5,y=0.95)) | |
fig.show() |
This file contains 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
countries = {'US':'United States', 'GB':'United Kingdom', 'FR':'France', 'CA':'Canada', 'DE':'Germany', 'JP':'Japan', 'IT':'Italy'} | |
def check_country(x): | |
for country in countries: | |
if country.lower() in x.lower(): | |
return countries[country] | |
return '' | |
df_g7_melt['COUNTRY'] = df_g7_melt['SERIES_ID'].map(lambda x: check_country(x)) |
This file contains 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
g7_list = df_g7['series_id'].tolist() | |
start_date = '1970-01-01' | |
end_date = '2021-10-01' | |
df_g7 = get_fred_data(series_list = g7_list, | |
start_date = start_date, | |
end_date = end_date) | |
df_g7_melt = pd.melt(df_g7, id_vars = ['DATE'], value_vars = g7_list, var_name = 'SERIES_ID', value_name = 'VALUE') |
This file contains 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
df_g7 = df_all_series_ids.loc[(df_all_series_ids['region'] == 'Canada') | (df_all_series_ids['region'] == 'France') | (df_all_series_ids['region'] == 'Germany') | (df_all_series_ids['region'] == 'Italy') | (df_all_series_ids['region'] == 'Japan') | (df_all_series_ids['region'] == 'United Kingdom') | (df_all_series_ids['region'] == 'United States')] | |
df_g7 |
This file contains 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
fred_api_key = 'your API key' | |
def get_fred_series_data(api_key, series): | |
url = "https://api.stlouisfed.org/geofred/series/data?series_id={0}&api_key={1}&file_type=json".format(series, api_key) | |
response = requests.request("GET", url) | |
return response | |
def transform_series_response(response): | |
latest_date = list(response.json()['meta']['data'].keys())[0] | |
return pd.DataFrame(response.json()['meta']['data'][latest_date]) | |
This file contains 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 = px.line(df_canada, x="DATE", y='QCAR628BIS', title='Real Residential Property Prices for Canada') | |
fig.update_layout(title=dict(x=0.5,y=0.95)) | |
fig.show(); |
This file contains 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_datareader as pdr | |
import pandas as pd | |
import requests | |
import plotly.express as px | |
from datetime import datetime | |
def get_fred_data(series_list, start_date, end_date): | |
df = pdr.DataReader(series_list, 'fred', start_date, end_date) | |
return df.reset_index() | |
This file contains 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
grouped_df.loc[grouped_df['customer_id'] == 2].sort_values('Quantity', ascending=False)[['customer_id', 'Description', 'Quantity']].head(20) |
This file contains 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 recommend(customer_id, sparse_customer_item, customer_vecs, item_vecs, num_items=10): | |
customer_interactions = sparse_customer_item[customer_id,:].toarray() | |
customer_interactions = customer_interactions.reshape(-1) + 1 | |
customer_interactions[customer_interactions > 1] = 0 | |
rec_vector = customer_vecs[customer_id,:].dot(item_vecs.T).toarray() | |
min_max = MinMaxScaler() | |
rec_vector_scaled = min_max.fit_transform(rec_vector.reshape(-1,1))[:,0] |
This file contains 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
item_id = 1319 | |
n_similar = 10 | |
item_vecs = model.item_factors | |
customer_vecs = model.user_factors | |
item_norms = np.sqrt((item_vecs * item_vecs).sum(axis=1)) | |
scores = item_vecs.dot(item_vecs[item_id]) / item_norms | |
top_idx = np.argpartition(scores, -n_similar)[-n_similar:] |
NewerOlder