Skip to main content

Low Rank Matrix Learning Library. A python library for low rank matrix methods used in machine learning, like Matrix Completion, Robust PCA, and CUR factorizations

Project description

LRMatLearnLib

Using Matrix Completion for Movie Recomendations

Start by loading the MovieLens1M dataset

data = np.loadtxt( 'ml-1m/ratings.dat',delimiter='::' )
X=data[:, [0,1]].astype(int)-1
y=data[:,2]

n_users=max(X[:,0])+1
n_movies=max(X[:,1])+1

print((n_users,n_movies))
(6040, 3952)

So, we have 6040 users and 3952 movies. That's a total of about 23 million potential ratings, of which we know 1 million. We're going to reserve 200,000 of the ratings to test our results.

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Import MC.py, fit the data and make predictions

from MC import *
from statistics import mean

mc_model=MC(n_users,n_movies,5)
mc_model.fit(np.array(X_train).transpose(), y_train)
y_predict=mc_model.predict((np.array(X_test).transpose()))

print("MAE:",mean(abs(y_test-y_predict)))

print("Percent of predictions off my less than 1: ",np.sum(abs(y_test-y_predict)<1)/len(y_test))
MAE: 0.6910439339771605
Percent of predictions off my less than 1:  0.7603603243318903

Using Robust Principle Component Analysis for Background Forground Seperation

Import packages, open the video file, and flatten the frames into vectors.

import sys
sys.path.append('../')
from LRMatLearnLib.datasets.data_loader import *
from LRMatLearnLib.RPCA.algorithms import *
import matplotlib.pyplot as plt
import matplotlib.animation as animation

v=load_video("../datasets/videos/escalator.avi")
(n_frames,d1,d2)=v.shape
v=v.reshape(n_frames, d1*d2)

run altProjNiave, a basic RPCA algorithm. The first arguement is the matrix to be decomposed, the second is the rank of the low rank matrix, and the third is the number of entries in the sparse matrix.

(L,S)=altProjNiave(v, 2,100*n_frames)

Reshape the frames back into images and plot them.

L=L.reshape(n_frames,d1,d2)
S=S.reshape(n_frames,d1,d2)
v=v.reshape(n_frames,d1,d2)
all=np.concatenate((v,L,S), axis=2)

plt.imshow(all[1,:,:])
plt.show()

png

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

LRMatLearnLib-0.1-py3-none-any.whl (2.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page