예제 #1
0
    dataset = dataset.copy()
    # [0=sur, 1=class, 2=sex, 3=age, 4=sibsp, 5=parch, 6=fare, 7=embarked]
    J = [0,3,2,3,3,3,1,3] # number of discrete feature values for each index, assuming age is binned into
    # (0-10), (11-19), 20+ and sibsp
    J1 = 1
    if laplacesmooth == "off":
        J = [0,0,0,0,0,0,0,0]
        J1 = 0*J1
    if df([[y,0],[value,index]],dataset) == []:
        nxy = 0
    else:
        nxy = showstats(df([[y,0],[value,index]],dataset))[1]
    ny = showstats(df([[y,0]],dataset))[1]
    return float(J1 + nxy)/(J[index] + ny)

db = databin(data)

def bayespreddepend(x, dataset = db):
    db = dataset.copy()
    x0 = list(x) #list of features [[value1, index1],[value2,index2],...]
    x1 = list(x) #list of features [[value1, index1],[value2,index2],...]
    n = np.size(x,0)
    x0.append([0,0]) #all of the features x, along with die
    x1.append([1,0]) #all of the features x, along with live
    if (df(x0,dataset) == []) or (df(x1,dataset) == []):
        return 100
    nxand0 = showstats(df(x0,dataset))[1]
    nxand1 = showstats(df(x1,dataset))[1]
    return (1/ (( float(nxand0)/float(nxand1) ) + 1))

def bayespred(x, dataset = db):
예제 #2
0
####################################################################################################
# DATA
####################################################################################################
data = titandata("train")  # (891,8) array
test8 = titandata("test8")  # (418,8) array
# [0=sur, 1=class, 2=sex, 3=age, 4=sibsp, 5=parch, 6=fare, 7=embarked]
# The survival values are 0=die, 1=live, 2=don't know (for test8)
totdata = vstack((data, test8))  # stacks data on top of test8 to create a (1309,8) array

# convert unknown placeholder ages to reasonable averages
data = convertages(data, 3)
test8 = convertages(test8, 3)
totdata = convertages(totdata, 3)

db = databin(data)
dba = databin(data, agebin="off")
tba = databin(test8, agebin="off")
####################################################################################################


def logreg_prepdata(dataset, interactionlist="none"):
    """
    This function takes in data in the shape (m,8) and outputs a longer array of shape
    (m, 13 + N_ints) where N_ints = number of interaction terms.
    This form is suitable for logistic regression, as the continuous vars age and fare have been
    feature scaled, and the discrete features are converted into binary vars.
    Note the ordering of index labels is changed.
    With no interaction terms we have each row of data transformed as:
    [sur, cl, sex, age, sibsp, parch, fare, city] ==>
    [sur, 1, <age>, <fare>, sex, cl1, cl2, sibsp0, sibsp1, parch0, parch1, city0, city1 ]