Question regarding Python Machine Learning Course Code Challenge: Welcome to the Matrix | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

4th Jun 2020, 7:56 AM
Lars Masanneck
Lars Masanneck - avatar
14 Answers
+ 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.
23rd Dec 2020, 9:34 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 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))
27th Feb 2021, 3:22 PM
Abdullah Abdelhakeem
Abdullah Abdelhakeem - avatar
28th Dec 2020, 9:34 PM
LKScoder
LKScoder - avatar
+ 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
28th Dec 2020, 9:32 PM
LKScoder
LKScoder - avatar
+ 2
Douglas in which line?
23rd Dec 2020, 5:43 AM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 1
AMOGHA. A. K. Which values are error prone?
22nd Dec 2020, 2:48 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 1
Lars Masanneck could you determine?
22nd Dec 2020, 2:49 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 1
You could try to round to 5 decimals before round to 4. It seems we have a float issue.
23rd Dec 2020, 5:42 AM
Douglas
+ 1
Zhenis Otarbay, I think TheWhiteCat is right. Here is my code https://code.sololearn.com/cjopV0EM1vE2/?ref=app
23rd Dec 2020, 12:51 PM
Douglas
+ 1
I wrote f1 expression directly through tp, fp and fn and simplified the result. Then it worked.
30th Dec 2020, 6:30 PM
Andrey Kurdyubov
Andrey Kurdyubov - avatar
0
AMOGHA. A. K. What do you mean by value error?
22nd Dec 2020, 2:48 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
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))
3rd Aug 2022, 8:26 AM
maria damanaki
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)
11th Aug 2022, 12:16 PM
Mohammed Khalid Hamadameen
Mohammed Khalid Hamadameen - avatar
- 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)
2nd Mar 2021, 7:04 AM
Namrata Dattani
Namrata Dattani - avatar