+ 6
Question regarding Python Machine Learning Course Code Challenge: Welcome to the Matrix
As I am not allowed to share the task (pro user challenge), I am writing to those of you that have taken the course as well. My 5th Test case keeps failing. I’ve tried many different solutions (and my code got messier as I tried...). Does anyone have a suggestion why it could fail? MY code: https://code.sololearn.com/cK9QT91H1NxP/?ref=app
15 Respuestas
+ 8
Lars Masanneck , Zhenis Otarbay , I think the reason is lost of a precision when you round each variable and use the rounded variables to calculate new result. According to your code I can suggest you the following solution.
https://code.sololearn.com/c8Yd5TEo7Q7a/?ref=app
Hope it helps you.
+ 6
tp, fp, fn, tn = [int(x) for x in input().split()]
ac = (tp + tn) / (tp+fp+fn+tn)
pr = tp / (tp + fp)
re = tp / (tp + fn)
f1 = (2 * pr * re) / (pr + re)
output = [ac, pr, re, f1]
for i in output:
print(round(i, 4))
+ 4
I encountered same problem.
you really worked hard to come up with test cases including zero value. i never would have thouht of it of course it would be unfair on part of sololearn to provide zero values for any parameter.
debugging a code with unknown input can be quite frustrating but finally once you succeed the satisfaction is great.
the precision value when rounded prior to calculating f1 causes the case 5 to fail .
rounding only while printing worked for me.
here is the simple version of code
+ 2
Douglas in which line?
+ 1
AMOGHA. A. K. Which values are error prone?
+ 1
Lars Masanneck could you determine?
+ 1
You could try to round to 5 decimals before round to 4.
It seems we have a float issue.
+ 1
Zhenis Otarbay, I think TheWhiteCat is right.
Here is my code
https://code.sololearn.com/cjopV0EM1vE2/?ref=app
+ 1
I wrote f1 expression directly through tp, fp and fn and simplified the result. Then it worked.
0
AMOGHA. A. K. What do you mean by value error?
0
I dont understand why my code with the # print statement gives me wrong but with multiple print statements gives me:
tp, fp, fn, tn = [int(x) for x in input().split()]
tot = tp +fp +fn +tn
acc = (tp + tn)/tot
acc_1 = round(acc, 4)
pre = tp/(tp + fp)
pre_1 = round(pre, 4)
recall = tp /(tp + fn)
recall_1 = round(recall, 4)
f1_score = 2 * pre * recall / (pre + recall)
f1_score_1 = round(f1_score, 4)
#print(str(acc_1),'\n'+ str(pre_1),'\n'+ str(recall_1), '\n'+ str(f1_score_1))
print(str(acc_1))
print(str(pre_1))
print(str(recall_1))
print(str(f1_score_1))
0
import numpy as np
tp, fp, fn, tn = [int(x) for x in input().split()]
"""
@ required
1. float - accuracy
2. float - precision
3. float - recall
4. float - f1 score
"""
def accuracy_precision_recall_f1score(tp,fp,fn,tb):
"""
1. precision is a ratio can be found here -> ``` tp / (tp+fp) ```
2. recall -> ``` tp / (tp + fn) ```
3. f1 score -> ``` 2 * (precision*recall) / (precision + recall)
4. accuracy -> ``` (tp + tn) / (tn+tp+fn+fp) ```
"""
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1_score = 2 * (precision*recall) / (precision + recall)
accuracy = (tp + tn) / (tn+tp+fn+fp)
return np.array([accuracy, precision, recall, f1_score]).round(4)
result = accuracy_precision_recall_f1score(tp,fp,fn,tn)
for item in result:
print(item)
- 1
tp, fp, fn, tn = [int(x) for x in input().split()]
all= (tp+fp+fn+tn)
pos= tp+fp
tru= tp+fn
try:
acc = ((tp+tn)/all)
prec= (tp/(tp+fp))
rec= (tp/(tp+fn))
f1s= round((2*prec*rec/(prec+rec)), 4)
except:
if all==0:
acc = 0
prec= 0
rec= 0
f1s= 0
if pos==0:
acc = ((tp+tn)/all)
prec= 0
rec= (tp/(tp+fn))
f1s= round((2*prec*rec/(prec+rec)), 4)
if tru == 0:
acc = ((tp+tn)/all)
prec= (tp/(tp+fp))
rec= 0
f1s= round((2*prec*rec/(prec+rec)), 4)
print(round(acc, 4))
print(round(prec, 4))
print(round(rec, 4))
print(f1s)