一、模型评估指标编程实现
#1.生成真实的值和标签
from sklearn.metrics import confusion_matrix
y_true = [1, 1, 0, 1, 0, 1]
y_pred = [1, 0, 0, 1, 1, 1]
TP, TN,FP ,FN = confusion_matrix(y_true, y_pred).ravel()
print('输出TP值',TP) # 输出TP值
print('输出FP值',FP)
print('输出FN值',FN)
print('输出TN值',TN)
#2.显示出混淆矩阵
cm = confusion_matrix(y_pred,y_true)
print("混淆矩阵:")
print(cm
#3.计算各个指标
error_rate = (FP + FN) / (TP + TN + FP + FN)
print(error_rate) # 输出错误率
right_rate=1-error_rate
right_rate #1-错误率=准确率
from sklearn.metrics import precision_score
precision = precision_score(y_true, y_pred)
print(precision) # 输出精确率,也叫做查准率.注意精确率不是准确率
from sklearn.metrics import recall_score
recall = recall_score(y_true, y_pred)
print(recall) # 输出查全率也叫做召回率
from sklearn.metrics import f1_score
f1 = f1_score(y_true, y_pred)
print(f1) # 输出F1值
二、计算实现
三、编程实现one-hot编码、k-mer编码
# 导入必要的库
import numpy as np
# One-hot编码实现
def one_hot_encoding2(sequence):
bases = 'ACGT' # DNA碱基
encoding = np.zeros((len(sequence), len(bases)))
for i, base in enumerate(sequence):
if base in bases:
encoding[i, bases.index(base)] = 1
return encoding
# K-mer编码实现
def kmer_encoding2(sequence, k):
bases = 'ACGT' # DNA碱基
encoding = []
for i in range(len(sequence) - k + 1):
kmer = sequence[i:i+k]
if 'N' not in kmer: # 如果k-mer中没有未知碱基
encoding.append(kmer)
return encoding
# 示例序列
sequence = "ACGTACGTACGT"
# 进行One-hot编码
one_hot_encoded = one_hot_encoding(sequence)
print("One-hot编码结果:")
print(one_hot_encoded)
# 进行K-mer编码
kmer_encoded = kmer_encoding(sequence, k=3)
print("\nK-mer编码结果:")
print(kmer_encoded)
结果如下:
四、与ilearn比较
我先学习一下ilearnPlus平台是如何使用的。下次见