Given a rating matrix in .dat:
rating = np.load(os.path.join(data_dir, "rating.dat"))
matrix([[ 5, 4, 0, 0],
[ 0, 0, 5, 0],
[ 0, 0, 0, 1],
[ 0, 0, 0, 1]])
And a df such as:
df=pd.read_csv('data_path')
df
user item
0 foo qw
1 foo rt
2 coo ty
3 doo yu
4 moo yu
The rating matrix row corresponds user
and column item
and values are ratings. I want to add this matrix to my df
as an additional column, in order to have a result like this:
user item rating
0 foo qw 5
1 foo rt 4
2 coo ty 5
3 doo yu 1
4 moo yu 1
Thank you in advance!
Given a rating matrix:
ratings = np.asarray([
[ 5, 4, 0, 0],
[ 0, 0, 5, 0],
[ 0, 0, 0, 1],
[ 0, 0, 0, 1]
])
ratings.flatten()[ratings.flatten().nonzero()]
Out[1]: array([5, 4, 5, 1, 1])
The trick is to flatten the matrix and remove the non-zero elements. Then simply df['ratings'] = ratings
and you will have your column filled in the proper order. Note that if some user makes several reviews, also has several rows in your df
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.