+ 8
Split to Achieve Gain project
I get a problem on 3,4,5ft case on Split to Achieve Gain project in Machine learning course(Sololearn)! Help me please, here my code S = [int(x) for x in input().split()] A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] SL = len(S) BL=len(B) AL = len(A) one = S.count(1) zero = S.count(0) giniP = one/(one+zero) giniInit = 2*giniP*(1-giniP) one = A.count(1) zero = A.count(0) giniP = one/(one+zero) giniLeft = 2*giniP*(1-giniP) one = B.count(1) zero = B.count(0) giniP = one/(one+zero) giniRight= 2*giniP*(1-giniP) IG= giniInit -(giniLeft*(AL/SL))-(giniRight*(BL/SL)) print(IG)
24 Answers
- 1
Bat-Ochir Artur , in the task it's mentioned that you should round the result to 5 decimal places. There is no rounding in your code.
+ 12
S = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
SL = len(S)
BL=len(B)
AL = len(A)
one = S.count(1)
zero = S.count(0)
giniP = one/(one+zero)
giniInit = 2*giniP*(1-giniP)
one = A.count(1)
zero = A.count(0)
giniP = one/(one+zero)
giniLeft = 2*giniP*(1-giniP)
one = B.count(1)
zero = B.count(0)
giniP = one/(one+zero)
giniRight= 2*giniP*(1-giniP)
IG= giniInit -(giniLeft*(AL/SL))-(giniRight*(BL/SL))
print (round(IG,5))
+ 5
Please expostion
+ 4
SOLVED
S = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
def G(data):
c = len(data)
l=data.count(1)
return (2 * l/c * (1-(l/c)))*len(data)/len(S)
k = G(S)-G(A)-G(B)
print(round(k,5))
+ 4
S = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
gini_s = 2*S.count(1)/len(S) * (1-S.count(1)/len(S))
gini_a = 2*A.count(1)/len(A) * (1-A.count(1)/len(A))
gini_b = 2*B.count(1)/len(B) * (1-B.count(1)/len(B))
information_gain = gini_s - gini_a*len(A)/len(S) - gini_b*len(B)/len(S)
print(round(information_gain, 5))
+ 3
i tried similar code case 3 fails rest of the cases are ok.
+ 3
S = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
def G(data):
c = len(data)
l=data.count(1)
return (2 * l/c * (1-(l/c)))*len(data)/len(S)
k = G(S)-G(A)-G(B)
print(round(k,5))
+ 2
S = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
inital_length = len(S)
def findGini(dataset):
length_of_data = len(dataset)
zero = dataset.count(0)
one = dataset.count(1)
return (2 * (one/length_of_data) * (zero/length_of_data)) * (length_of_data / inital_length)
info_gain = findGini(S) - findGini(A) - findGini(B)
print(round(info_gain, 5))
+ 2
# Made by Lord Priyansh
S = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
def ginx(s):
positive_target = []
for i in s:
if i > 0:
positive_target.append(i)
return (2*(len(positive_target)/len(s))*(1-(len(positive_target)/len(s))))*len(s)/len(S)
k = ginx(S)-ginx(A)-ginx(B)
print(round(k, 5))
+ 2
Use this code to get all cases right;
SOLUTION
import numpy as np
S = np.array([int(x) for x in input().split()])
A = np.array([int(x) for x in input().split()])
B = np.array([int(x) for x in input().split()])
mean_S = S.mean()
mean_A = A.mean()
mean_B = B.mean()
g1 = 2 * mean_S * (1-mean_S)
g2 = 2 * mean_A * (1-mean_A)
g3 = 2 * mean_B * (1-mean_B)
H_S = len(S)
H_A = len(A)
H_B = len(B)
infog = g1 - (H_A/H_S) * g2 - (H_B/H_S) * g3
print(round(infog , 5))
+ 1
print (round(infogain,5)) is what i used for printing
+ 1
Here is my solution, using a single function to calculate the Gini values:
def gini(x):
return 2*(x.count(1)/len(x))*(x.count(0)/len(x))
ig = gini(S) - (len(A)/len(S))*gini(A) - (len(B)/len(S))*gini(B)
print(round(ig,5))
+ 1
import numpy as np
S = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
p_S = np.sum(S) / len(S)
p_A = np.sum(A) / len(A)
p_B = np.sum(B) / len(B)
gini_S = 2*(p_S)*(1-p_S)
gini_A = 2*(p_A)*(1-p_A)
gini_B = 2*(p_B)*(1-p_B)
gain = gini_S - (gini_A * len(A)/len(S)) - (gini_B * len(B)/len(S))
print(round(gain, 5))
0
Oh my god! You are my hero!!! All cases passed! thank you!
0
TheWh¡teCat 🇧🇬 don't be lazy.
0
def getGini(s):
a = 0
for i in s:
if s[i]==1:
a=a+1
return 2*((a/len(s))*((len(s)-a)/len(s)))
#print(getGini(S))
print(round(getGini(S) - (len(A)/len(S))*getGini(A) - (len(B)/len(S))*(getGini(B)),5))
two hidden cases failing... please help
0
S = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
s_start_1 = S.count(1)
s_start_0 = S.count(0)
a_pv_1 = A.count(1)
a_pv_0 = A.count(0)
b_nv_1 = B.count(1)
b_nv_0 = B.count(0)
gini_impurity_start = 2 * (s_start_1/(s_start_1 + s_start_0)) * (1 - (s_start_1/(s_start_1 + s_start_0)))
gini_impurity_a = 2 * (a_pv_1 / (a_pv_1 + a_pv_0)) * (1 - (a_pv_1 / (a_pv_1 + a_pv_0)))
gini_impurity_b = 2 * (b_nv_1 / (b_nv_1 + b_nv_0)) * (1 - (b_nv_1 / (b_nv_1 + b_nv_0)))
information_gain = gini_impurity_start - ((a_pv_1 + a_pv_0) / (s_start_1 + s_start_0)) * gini_impurity_a - ((b_nv_1 + b_nv_0) / (s_start_1 + s_start_0)) * gini_impurity_b
print (round(information_gain, 5))
0
S = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
s_zero = S.count(0)
s_one = S.count(1)
S_final = 2 * (s_one/(s_one + s_zero) * (1 - s_one/(s_one + s_zero)))
a_zero = A.count(0)
a_one = A.count(1)
A_final = 2 * (a_one/(a_one + a_zero) * (1 - a_one/(a_one + a_zero)))
b_zero = B.count(0)
b_one = B.count(1)
B_final = 2 * (b_one/(b_one + b_zero) * (1 - b_one/(b_one + b_zero)))
gain = S_final - (A_final * len(A)/len(S)) - (B_final * len(B)/len(S))
print(round(gain, 5))
0
S = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
surv = S.count(1)/len(S)
Sgini = 2 * surv * (1-surv)
surv = A.count(1)/len(A)
Agini = 2 * surv * (1-surv)
surv = B.count(1)/len(B)
Bgini = 2 * surv * (1-surv)
info_gain = Sgini - (len(A)/len(S)) * Agini - (len(B)/len(S)) * Bgini
print(round(info_gain, 5))
0
S = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
def gini(a):
h=2*sum(a)/len(a)*(1-sum(a)/len(a))
return h
infogain=gini(S)-(len(A)/len(S))*gini(A)-(len(B)/len(S))*gini(B)
print(round(infogain,5))
WORKS!