+ 3

Binary disorder challenge

https://code.sololearn.com/c81vUnzCoAP6/?ref=app Can I get a better, more elegant solution than what I do here i.e any way except instantiating my x as [[0.,0.][0.,0.]]? I hate that dot(float?) instantiation. Felt like cheating.

1st Mar 2021, 12:46 PM
Satrio Bayu Pradhipta
Satrio Bayu Pradhipta - avatar
21 ответ
+ 8
To make it work i needed to change the input arrays to type character. So the total code goes like: y_true = [int(x) for x in input().split()] y_pred = [int(x) for x in input().split()] import numpy as np y_true=np.array(y_true ) y_true= y_true.astype(str) y_pred=np.array(y_pred ) y_pred=y_pred.astype(str) #y_true = y_true.reshape(-1, 1) #y_pred = y_pred.reshape(-1, 1) #print(y_true.shape) from sklearn.metrics import confusion_matrix print((confusion_matrix(y_pred, y_true, labels=['1', '0']))/1)
13th Mar 2021, 6:02 PM
Frank Kober
Frank Kober - avatar
+ 5
————————————————————— you can replace everything after your second line of code with these 2 lines. from sklearn.metrics import confusion_matrix print((confusion_matrix(y_pred, y_true, labels=['1', '0']))/1) the divided by one was just a lazy way to convert my answer to a float ————————————————————— PS you can convert numpy arrays to float arrays with the dtype property. x = np.array([0, 0], [0, 0], dtype=‘f’) if you have an array that you want to turn into a float array use the astype() method. arr = arr.astype(‘f’) For reference here are the numpy dtypes: int ———————- i bool ——————- b unsigned int ——- u float ——————— f complex number - c object —————— O string ——————- S unicode string —— U void ———————- V ————————————————————— as for the long list of if statements you could us a ternary operator in a comprehension if you want to make it a one liner but it will still have the same functionality as your code so I wouldn’t say it was more elegant. —————————————————————
4th Mar 2021, 10:33 PM
Ethan
Ethan - avatar
+ 4
Frank Kober the input type is character automatically in python. your list comprehension has x being casted to an int. just remove that and it will be a character already for you change it to: _____________________________ y_true = [x for x in input().split()] y_pred = [x for x in input().split()] from sklearn.metrics import confusion_matrix print((confusion_matrix(y_pred, y_true, labels=['1', '0']))/1) ______________________________
13th Mar 2021, 6:07 PM
Ethan
Ethan - avatar
+ 4
y_true = [int(x) for x in input().split()] y_pred = [int(x) for x in input().split()] from sklearn.metrics import confusion_matrix print((confusion_matrix(y_pred,y_true, labels=[1,0]))/1) It just that simple only 2 lines of code needed
12th Apr 2021, 10:36 AM
Pranav Hirani
Pranav Hirani - avatar
+ 2
y_true = [(x) for x in input().split()] y_pred = [(x) for x in input().split()] import numpy as np y_true=np.array(y_true ) y_true= y_true.astype(str) y_pred=np.array(y_pred ) y_pred=y_pred.astype(str) #y_true = y_true.reshape(-1, 1) #y_pred = y_pred.reshape(-1, 1) #print(y_true.shape) from sklearn.metrics import confusion_matrix print((confusion_matrix(y_pred, y_true, labels=['1', '0']))/1) This is the solution
8th Apr 2021, 7:04 AM
Kanaad
Kanaad - avatar
+ 2
Norika Gilbert it changes position and convert it into float
10th Jan 2022, 7:53 AM
Pranav Hirani
Pranav Hirani - avatar
+ 1
y_true = [int(x) for x in input().split()] y_pred = [int(x) for x in input().split()] from sklearn.metrics import confusion_matrix as cm print(cm(y_true,y_pred)[::-1,::-1].transpose().astype(float))
6th Mar 2022, 9:21 AM
Seyyed Soroush Mirzaei
+ 1
# not using sklearn import numpy as np y_true = [int(x) for x in input().split()] y_pred = [int(x) for x in input().split()] tp, tn, fp, fn = 0, 0, 0, 0 for i in range(len(y_true)): if y_true[i] == y_pred[i] & y_pred[i] == 1: tp += 1 if y_true[i] != y_pred[i] & y_pred[i] == 0: fn += 1 if y_true[i] == y_pred[i] & y_pred[i] == 0: tn += 1 if y_true[i] != y_pred[i] & y_pred[i] == 1: fp += 1 out = np.zeros((2,2)) out[0,0] = tp out[0,1] = fp out[1,0] = fn out[1,1] = tn print(out)
18th Mar 2022, 9:34 AM
Behrooz Ostadaghaee
Behrooz Ostadaghaee - avatar
+ 1
y_true = [int(x) for x in input().split()] y_pred = [int(x) for x in input().split()] from sklearn.metrics import confusion_matrix print(confusion_matrix(y_true, y_pred, labels=[1,0]).transpose().astype('float'))
18th Mar 2022, 9:40 AM
Behrooz Ostadaghaee
Behrooz Ostadaghaee - avatar
+ 1
import numpy as np y_true = [int(x) for x in input().split()] y_pred = [int(x) for x in input().split()] conf = list(zip(y_true, y_pred)) def confTuple(tupl, tr,pr): res = len([t[0] for t in tupl if t[0] == tr and t[1]==pr]) return res tp = confTuple(conf, 1,1) fp = confTuple(conf, 0,1) fn = confTuple(conf, 1,0) tn = confTuple(conf, 0,0) print(np.array([[tp,fp],[fn, tn]])/1)
16th Nov 2022, 9:46 AM
Andreas Ströhlein
Andreas Ströhlein - avatar
0
I have not encountered this quiz yet, but I am wondering why you only convert the thing to an array at the very end.
1st Mar 2021, 1:46 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
Wilbur Jaywright No reason. It might work without array, but I side with err and just convert them into array because that's what the example show
1st Mar 2021, 1:48 PM
Satrio Bayu Pradhipta
Satrio Bayu Pradhipta - avatar
0
what I mean is, why don’t you start with an arrary right at the beginning? They have lots of good methods for mathematical operations like you’re doing, and are easy to initialize with the right shape and preset values.
1st Mar 2021, 2:04 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
Wilbur Jaywright I'm not that comfortable with Numpy yet, so following how the data in the basic list flows is easier for me. With numpy I had to check web/tutorial just to ensure that I'm putting the right method/function. This one takes like 2-3 mins of mostly typing from phone keyboard without looking for outside sources
1st Mar 2021, 2:10 PM
Satrio Bayu Pradhipta
Satrio Bayu Pradhipta - avatar
13th Mar 2021, 9:32 PM
Frank Kober
Frank Kober - avatar
0
y_true = [int(x) for x in input().split()] y_pred = [int(x) for x in input().split()] import numpy as np import pandas as pd Matrix=pd.DataFrame(data=np.zeros((2,2)),columns=[1,0],index=[1,0]) for i in range(len(y_true)): Matrix.loc[y_pred[i],y_true[i]]+=1 print(np.array(Matrix)) Without using confusion matrix
21st Nov 2021, 6:28 PM
ChrisL
0
What does the labels [1,0]/1 do? When I just did confusion_matrix( y_pred, y_true) i get (2,1,0,1)
9th Jan 2022, 7:11 PM
Norika Gilbert
0
y_true = [int(x) for x in input().split()] y_pred = [int(x) for x in input().split()] import numpy as np y_true=np.array(y_true ) y_true= y_true.astype(str) y_pred=np.array(y_pred ) y_pred=y_pred.astype(str) #y_true = y_true.reshape(-1, 1) #y_pred = y_pred.reshape(-1, 1) #print(y_true.shape) from sklearn.metrics import confusion_matrix print((confusion_matrix(y_pred, y_true, labels=['1', '0']))/1)
17th Jun 2022, 5:39 PM
Sri varshini R
Sri varshini R - avatar
0
import numpy as np import pandas as pd y_true = [int(x) for x in input().split()] y_pred = [int(x) for x in input().split()] #Variables x = 0 y = 0 True_Positives = [] True_Negatives = [] Pred_Positives = [] Pred_Negatives = [] #TP & TN for i in y_true: x += 1 if i == 1: True_Positives.append(x) else: True_Negatives.append(x) #FP & FN for i in y_pred: y += 1 if i == 1: Pred_Positives.append(y) else: Pred_Negatives.append(y) #Number of TP FP FN TN TP = len(list(set(True_Positives) & set(Pred_Positives))) FP = len(list(set(True_Negatives) & set(Pred_Positives))) FN = len(list(set(True_Positives) & set(Pred_Negatives))) TN = len(list(set(True_Negatives) & set(Pred_Negatives))) """ print("True_Positives: ", True_Positives) print("True_Negatives: ", True_Negatives) print("Pred_Positives: ", Pred_Positives) print("Pred_Negatives: ", Pred_Negatives) """ #Matrix lst = [TP, FP, FN, TN] arr = np.array(lst) arr_2d = arr.reshape(2, 2) print(arr_2d/1)
18th Jul 2022, 10:19 PM
Kerem DÜZENLİ