当未使用R中的插入符号包预测所有类别时的混淆矩阵

时间:2019-03-10 15:18:55

标签: r r-caret confusion-matrix

我有一个分类模型 public class Player : MonoBehaviour { private Rigidbody2D rigid; [SerializeField] private float jumpForce = 5.0f; private bool resetJump; [SerializeField] private float speed = 5.0f; private PlayerAnimation playerAnim; private SpriteRenderer playerSprite; // Start is called before the first frame update void Start() { ... } // Update is called once per frame void Update() { Movement(); } void Movement() { float move = Input.GetAxisRaw("Horizontal"); Flip(move); if (IsGrounded()) { playerAnim.Jump(false); } if (Input.GetKeyDown(KeyCode.Space) && IsGrounded() == true) { rigid.velocity = new Vector2(rigid.velocity.x, jumpForce); StartCoroutine(ResetJumpNeededRoutine()); playerAnim.Jump(true); } rigid.velocity = new Vector2(move * speed, rigid.velocity.y); playerAnim.Move(move); } void Flip(float move) { ... } bool IsGrounded() { RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector2.down, 0.3f, 1 << 8); if (hitInfo.collider != null) { if (resetJump == false) { return true; } } return false; } IEnumerator ResetJumpNeededRoutine() { yield return new WaitForSeconds(0.1f); resetJump = false; } } ,用于根据11个类别的数据评估模型,该模型的预测结果为:

predict(model, test.x)

我的测试标签(真相)是:

table(predicted_class)
0   1   2   3   5   6   8  10 
7   6  232  11  74  58   1   1 

当我想使用插入符号包获取混淆矩阵时,出现此错误消息,因为我的模型未预测类7和9:

table(test.y)
  0   1   2   3   4   5   6   7   8   9  10 
105  16  78  25  14  74  12   9  23  15  19 

当预测中缺少某些因子水平时,如何获得混淆矩阵:对于缺失的类(在本例中为4、7和9),如何为预测类自动添加0

1 个答案:

答案 0 :(得分:2)

通过将因子与union结合在一起,使水平相同

all_class <- union(predicted_class, test.y)
newtable <- table(factor(predicted_class, all_class), factor(test.y, all_class))
caret::confusionMatrix(newtable)