首页 > 解决方案 > 如何用二进制数组训练多个矩阵

问题描述

我有七个矩阵和几个数组。每个数组都有二进制元素,每个矩阵都是心电图信号的一类。对于训练,我有 7 个矩阵,每个矩阵有 110 个数组,其中包含许多表示 ecg 信号的二进制元素(只有 0 和 1,其中 1 是空白,0 是代表信号的黑色部分)。对于测试,我有 7 个相同的矩阵,有 30 个数组。

应该如何制作模型来训练这些矩阵,我应该如何做这个机器学习算法?这就是火车矩阵的样子

[[1. 1. 1. ... 1. 1. 1.] ->this is array 1
 [1. 1. 1. ... 1. 1. 1.] ->this is array 2
 [1. 1. 1. ... 1. 1. 1.]
 ...//here are a lot of numbers, including 0
 [1. 1. 1. ... 1. 1. 1.]
 [1. 1. 1. ... 1. 1. 1.]
 [1. 1. 1. ... 1. 1. 1.]] -> this is array 110

这就是测试矩阵的样子:

[[1. 1. 1. ... 1. 1. 1.] ->this is array 1
 [1. 1. 1. ... 1. 1. 1.] ->this is array 2
 [1. 1. 1. ... 1. 1. 1.]
 ... //here are a lot of numbers, including 0
 [1. 1. 1. ... 1. 1. 1.]
 [1. 1. 1. ... 1. 1. 1.]
 [1. 1. 1. ... 1. 1. 1.]] -> this is array 30```

标签: pythonarraysmachine-learningmatrix

解决方案


将其转换为 numpy 数组并使用机器学习模型对其进行训练。这是一个示例

import numpy as np 
features=np.random.choice([0,1], (110,6), p=[0.5, 0.5]) # Generating random numpy array with values 0&1 for training
targets = np.random.choice([0,1], (110,), p=[0.5, 0.5])
test_features = np.random.choice([0,1], (50,6), p=[0.5, 0.5]) # Test set
test_targets = np.random.choice([0,1], (50,), p=[0.6, 0.4])

from sklearn.tree import DecisionTreeClassifier # Training algo of your choice
from sklearn.metrics import accuracy_score 
model = DecisionTreeClassifier() 
model.fit(features,targets)
pred = model.predict(test_features) # Inference of test set
accuracy_score(test_targets,pred) # Evaluation 


推荐阅读