求机器学习中预测结果准确值accuracy的python代码,要求不用sklearn库
时间: 2024-05-05 08:15:36 浏览: 68
基于python实现的机器学习预测系统-源码
以下是一个简单的Python代码,用于计算预测结果准确度(accuracy),不使用sklearn库:
```python
def accuracy_score(y_true, y_pred):
"""
计算预测结果准确度
:param y_true: 实际标签列表
:param y_pred: 预测标签列表
:return: 预测结果准确度
"""
correct = 0
for i in range(len(y_true)):
if y_true[i] == y_pred[i]:
correct += 1
accuracy = correct / len(y_true)
return accuracy
```
使用方法:
```python
y_true = [0, 1, 1, 0, 1, 0, 0, 1]
y_pred = [0, 1, 1, 1, 0, 1, 0, 1]
accuracy = accuracy_score(y_true, y_pred)
print("Accuracy:", accuracy)
```
输出结果:
```
Accuracy: 0.625
```
阅读全文