Skip to content

Instantly share code, notes, and snippets.

View dradecic's full-sized avatar

Dario Radečić dradecic

View GitHub Profile
@dradecic
dradecic / linreg_gradient_descent.py
Created October 13, 2019 07:58
linreg_gradient_descent
b0, b1 = 0.0, 1.0
lr = 0.001
epochs = 10000
error = []
# run 10000 times
for epoch in range(epochs):
# initialize to 0 -> cost of epoch, Jb_0, Jb_1
epoch_cost, cost_b0, cost_b1 = 0, 0, 0
@dradecic
dradecic / recommender1_8_scatter.py
Created September 29, 2019 10:34
recommender1_8_scatter
ratings_df = pd.DataFrame()
ratings_df['Mean_Rating'] = data.groupby('title')['rating'].mean().values
ratings_df['Num_Ratings'] = data.groupby('title')['rating'].count().values
fig, ax = plt.subplots(figsize=(14, 7))
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.set_title('Rating vs. Number of Ratings', fontsize=24, pad=20)
ax.set_xlabel('Rating', fontsize=16, labelpad=20)
@dradecic
dradecic / recommender1_7_top10.py
Created September 29, 2019 10:29
recommender1_7_top10
data.sort_values(by='numRatings', ascending=False).drop_duplicates('movieId')[:10]
@dradecic
dradecic / recommender1_6_num_ratings.py
Created September 29, 2019 10:28
recommender1_6_num_ratings
num_ratings = pd.DataFrame(data.groupby('movieId').count()['rating']).reset_index()
data = pd.merge(left=data, right=num_ratings, on='movieId')
data.rename(columns={'rating_x': 'rating', 'rating_y': 'numRatings'}, inplace=True)
@dradecic
dradecic / recommender1_5_rating_by_genre.py
Created September 29, 2019 10:27
recommender1_5_rating_by_genre
values = defaultdict(list)
for ind, row in data.iterrows():
for genre in row['genres'].split('|'):
values[genre].append(row['rating'])
genre_lst, rating_lst = [], []
for key, item in values.items():
if key not in [0, 1]:
genre_lst.append(key)
@dradecic
dradecic / recommender1_bar_chart.py
Created September 29, 2019 10:26
recommender1_bar_chart
def make_bar_chart(dataset, attribute, bar_color='#3498db', edge_color='#2980b9', title='Title', xlab='X', ylab='Y', sort_index=False):
if sort_index == False:
xs = dataset[attribute].value_counts().index
ys = dataset[attribute].value_counts().values
else:
xs = dataset[attribute].value_counts().sort_index().index
ys = dataset[attribute].value_counts().sort_index().values
fig, ax = plt.subplots(figsize=(14, 7))
@dradecic
dradecic / recommender1_4_genres.py
Created September 29, 2019 10:25
recommender1_4_genres
genre_df = pd.DataFrame(data['genres'].str.split('|').tolist(), index=data['movieId']).stack()
genre_df = genre_df.reset_index([0, 'movieId'])
genre_df.columns = ['movieId', 'Genre']
@dradecic
dradecic / recommender1_3_histogram.py
Created September 29, 2019 10:22
recommender1_3_histogram
def make_histogram(dataset, attribute, bins=25, bar_color='#3498db', edge_color='#2980b9', title='Title', xlab='X', ylab='Y', sort_index=False):
if attribute == 'moviePubYear':
dataset = dataset[dataset['moviePubYear'] != 9999]
fig, ax = plt.subplots(figsize=(14, 7))
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.set_title(title, fontsize=24, pad=20)
ax.set_xlabel(xlab, fontsize=16, labelpad=20)
ax.set_ylabel(ylab, fontsize=16, labelpad=20)
@dradecic
dradecic / recommender_1_obtaining_years.py
Created September 29, 2019 10:21
recommender_1_obtaining_years
years = []
for title in data['title']:
year_subset = title[-5:-1]
try: years.append(int(year_subset))
except: years.append(9999)
data['moviePubYear'] = years
print(len(data[data['moviePubYear'] == 9999]))
@dradecic
dradecic / recommender_1_imports.py
Created September 29, 2019 10:20
recommender_1_imports
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from collections import defaultdict
# read CSVs
movies = pd.read_csv('data/movies.csv')
ratings = pd.read_csv('data/ratings.csv')