Q:

Trying to create a 3D matrix in Python by choosing specific data

Hi so I'm trying to make a 3D matrix here.. It's the MovieLens data (https://grouplens.org/datasets/movielens/100k/)  from where I'm taking a u1.base and u1.test pair as training and test sets (respectively). Below is an image of the format of the data of variable training_set you'll discover in the code.

The 3D matrix I'm trying to create is of the format (User, Movie, Timestamp) and the data in each of those cells is the ratings given by, for example, user 1 to movie 1 at time 1. 
If it's any help, below is the code where a 2D matrix is created with users in the rows and all the movies as the columns.
import numpy as np
import pandas as pd

training_set = pd.read_csv('ml-100k/u1.base', delimiter = '\t')
training_set = np.array(training_set, dtype='int')
test_set = pd.read_csv('ml-100k/u1.test', delimiter = '\t')
test_set = np.array(test_set, dtype = 'int64')

nb_users = int(max(max(training_set[:, 0]), max(test_set[:, 0])))
nb_movies = int(max(max(training_set[:, 1]), max(test_set[:, 1]))) 

def convert(data):
    new_data = [] #final list that we will return
    for id_users in range(1, nb_users+1):
        id_movies = data[:, 1][data[:, 0] == id_users] #contains the IDs of the movies rated by the id_user
        id_ratings = data[:, 2][data[:, 0] == id_users] #all movie ratings given by specific user
        ratings = np.zeros(nb_movies)
        ratings[id_movies-1] = id_ratings #these two lines are just so that the movies that are not rated by user have null (0) values
        new_data.append(list(ratings))
    return (new_data)
training_set = convert(training_set)
test_set = convert(test_set)

Below is a code that I tried which gave a number of errors, so many that I couldn't scroll up to the first one it threw.
import numpy as np
import pandas as pd
training_set = pd.read_csv('ml-100k/u1.base', delimiter = '\t')
training_set = np.array(training_set, dtype='int')
test_set = pd.read_csv('ml-100k/u1.test', delimiter = '\t')
test_set = np.array(test_set, dtype = 'int64')

nb_users = int(max(max(training_set[:, 0]), max(test_set[:, 0])))
nb_movies = int(max(max(training_set[:, 1]), max(test_set[:, 1])))

#The changes I made start here --

nb_timestamps = int(max(len(training_set[:, 3]), len(test_set[:, 3])))

ts_min = int(min(min(training_set[:, 3]), min(test_set[:, 3])))
ts_max = int(max(max(training_set[:, 3]), max(test_set[:, 3])))

def convert(data):
    new_data = [] #final list that we will return
    for timestamp in range(ts_min, ts_max+1):
        for id_users in range(1, nb_users+1):
            id_movies = data[:, 1][data[:, 0] == id_users][data[:, 3] == timestamp]
            #contains the IDs of the movies rated by the id_user
            id_ratings = data[:, 2][data[:, 0] == id_users][data[:, 3] == timestamp]
            ratings = np.zeros(nb_movies)
            ratings[id_movies-1] = id_ratings
        new_data.append(list(ratings))
    return (new_data)
training_set = convert(training_set)
test_set = convert(test_set)

A:

Remark: Please don't take this as an answer (yet).
There are few things to improve in your code:

When you read the csv you're taking the first row as header which means you are not considering all the data
If in this case (asn it should be so) there is just one user can rate a movie only one time you can use pd.pivot_table in order to get your 2D matrix.

import pandas as pd
import numpy as np
training_set = pd.read_csv('ml-100k/u1.base',
                           delimiter='\t',
                           header=None, # First row is not header
                           names=["user", "movie",
                                  "rating", "timestamp"]) # rename headers

# with pd.pivot_table you get a df where user are in rows 
# and movies in columns. The value is the rating for movie (i,j)
ratings = pd.pivot_table(training_set,
                         index=["user"],
                         columns=["movie"],
                         values="rating")

In case you want 0s instead of NaN you can use ratings.fillna(0). But I wouldn't do so. You should be care cos this will mess up the eventual statistics you want to extract.
In case you need the 2D matrix you can just use ratings.values.
UPDATE
In order to get your 3D matrix we can do the same pivoting with timestamps
timestamps = pd.pivot_table(training_set,
                         index=["user"],
                         columns=["movie"],
                         values="timestamp")

# get matrix
mat_ratings = ratings.values
mat_timestamps = timestamps.values

# stack matrix
mat3d = np.dstack((mat_ratings, mat_timestamps))

You can now check that from 2 matrix with shape (943, 1650) we get one of shape (943, 1650, 2). Note to get the shape of matrix mat just run mat.shape.

