首页 > 解决方案 > 如何删除新矩阵中的某些矩阵的旧值

问题描述

我想将旧矩阵中的填充值删除到我的新矩阵中。python中有什么语法或代码来做到这一点?

这是从旧矩阵到我想要的最终矩阵的矩阵。如何在python中做到这一点?是否有任何语法或一些代码可以删除新最终矩阵中的旧值?

这是我想要的最终结果

import numpy as np
import pandas as pd
from pandas import read_csv

read = pd.read_csv('rating_trainingreg.csv')

ratings_df = pd.pivot_table(raw_dataset_df, index='User_ID',
                        columns='Training_ID',
                        aggfunc=np.max)

predicted_ratings_df = pd.DataFrame(index=ratings_df.index,
                                columns=ratings_df.columns,
                                data=predicted_ratings)

reviewed_training_df = reviewed_training_df.join(training_df, 
on='Training_ID')
already_reviewed = reviewed_training_df['Training_ID']
newMatrix = predicted_ratings_df.index,isin(already_reviewed) == False

newMatrix



OLD matirx:

                               Rating   
TrainingID|   1001  |  1002  |  1003  | 1004 |
UserID    |         |        |        |   2  |
1         |    2    |        |        |   3  |
2         |         |    3   |        |      |
3         |    2    |        |        |      |
4         |         |        |        |      |

After some calculation:
                               Rating   
TrainingID|   1001   |  1002   |  1003  | 1004 |
UserID    |     1    |    2    |   2    |   2  |
1         |     2    |    3    |   3    |   3  |
2         |     3    |    3    |   4    |   2  |
3         |     2    |    1    |   4    |   2  |
4         |     4    |    1    |   5    |   2  |

Final matrix that I want:

                               Rating   
TrainingID|   1001  |  1002   |  1003  | 1004 |
UserID    |     1   |    2    |   2    |      |
1         |         |    3    |   3    |      |
2         |     3   |         |   4    |   2  |
3         |         |    1    |   4    |   2  |
4         |     4   |    1    |   5    |   2  |

标签: pythonmatrix

解决方案


也许这有帮助:

# manual way
newMatrix[2005][1001] = None
newMatrix[2002][1002] = None
newMatrix[2003][1004] = None
newMatrix[2005][1005] = None
newMatrix[2007][1007] = None

现在,正确的方法是,如果您有一个很长的矩阵:

# iterate the old matrix to set None in the new matrix where appropriate
for i, j in oldMatrix.iterrows():
    if oldMatrix[i][j]:
        newMatrix[i][j] = None

推荐阅读