-
-
Save uberwach/94be79de61b9950e36d6 to your computer and use it in GitHub Desktop.
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
# Solution Ex. 1.1 | |
import re | |
def extract_year(moviename): | |
m = re.search(r'\((\d*)\)', moviename) | |
if m == None: | |
return None | |
return int(m.group(1)) | |
# First step: extract release dates years from column moviename | |
df_mvl['release_year'] = df_mvl['moviename'].apply(extract_year) | |
df_mvl.hist(column='release_year') | |
df_mvl['release_year'].describe() | |
# Solution Ex. 1.2 | |
df_mvl.groupby('userid')['rating'].mean().hist() |
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
# Solution Ex. 2.2 | |
def incremental_als(B, W, k, lambd=0.1, iter_per_row=50): | |
n, m = B.shape | |
Psi = np.zeros((n, k)) | |
Phi = np.zeros((k, m)) | |
for i in range(k): | |
Psi[:, i] = np.random.rand(n) | |
Phi[i, :] = np.random.rand(m) | |
for j in range(iter_per_row): | |
# Alternate optimization | |
Psi[:, i] = fmin_l_bfgs_b(lambda psi_i: loss(np.hstack((Psi[:, :i], psi_i.reshape((n, 1)), Psi[:, i+1:])), Phi, B, W, lambd), | |
Psi[:, i], | |
approx_grad=1)[0] | |
Phi[i, :] = fmin_l_bfgs_b(lambda phi_i: loss(Psi, np.vstack((Phi[:i, :], phi_i.reshape((1, m)), Phi[i+1:, :])), B, W, lambd), | |
Phi[i, :], | |
approx_grad=1)[0] | |
return Psi, Phi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment