preprocessing.py
def create_clusters(X_train_scaled, X_validate_scaled, X_test_scaled, features, n, cluster_name):
    
    '''
    Create clusters based on features specified. n is amount of groups within the cluster.
    Best when used on scaled dfs. Returns dfs with dummy variables of clusters appended.
    '''
    
    from sklearn.cluster import KMeans

    X = X_train_scaled[features]
    Y = X_validate_scaled[features]
    Z = X_test_scaled[features]
    
    # create object with clusters chosen by n parameter
    kmeans = KMeans(n_clusters=n, random_state = 666)

    # fit to train only and the features chosen
    kmeans.fit(X)
    
    # add a column to the dfs of the prediction of cluster group
    X_train_scaled[cluster_name] = kmeans.predict(X)
    X_validate_scaled[cluster_name] = kmeans.predict(Y)
    X_test_scaled[cluster_name] = kmeans.predict(Z)
    
    
    # naming the cluster groups by cluster name plus numbers 1 through n for each group
    columns = []
    for x in range(1, n+1):
        columns.append(f'{cluster_name}_{x}')
    
    
    # create dataframe of dummy variables of cluster group created for each train, validate, test
    # train cluster dummy variables
    dummies = pd.get_dummies(X_train_scaled[cluster_name])
    dummies.columns = columns

    # validate cluster dummy variables
    dummies2 = pd.get_dummies(X_validate_scaled[cluster_name])
    dummies2.columns = columns

    # test cluster dummy variables
    dummies3 = pd.get_dummies(X_test_scaled[cluster_name])
    dummies3.columns = columns
    
    # add cluster dummy variables to scaled df
    # adding train cluster dummies to train scaled df
    X_train_scaled = pd.concat([X_train_scaled,dummies], axis=1)
    
    # adding validate cluster dummies to validate scaled df
    X_validate_scaled = pd.concat([X_validate_scaled,dummies2], axis=1)
    
    # adding test cluster dummies to test scaled df
    X_test_scaled = pd.concat([X_test_scaled,dummies3], axis=1)

    return X_train_scaled, X_validate_scaled, X_test_scaled

    Create clusters based on features specified. n is amount of groups within the cluster.
    Best when used on scaled dfs. Returns dfs with dummy variables of clusters appended.
    
Create clusters based on features specified. n is amount of groups within the cluster.
Best when used on scaled dfs. Returns dfs with dummy variables of clusters appended.
Create clusters based on features specified . n is amount of groups within the cluster . Best when used on scaled dfs . Returns dfs with dummy variables of clusters appended .
def create_clusters(X_train_scaled, X_validate_scaled, X_test_scaled, features, n, cluster_name):
    from sklearn.cluster import KMeans
    X = X_train_scaled[features]
    Y = X_validate_scaled[features]
    Z = X_test_scaled[features]
    kmeans = KMeans(n_clusters=n, random_state = 666)
    kmeans.fit(X)
    X_train_scaled[cluster_name] = kmeans.predict(X)
    X_validate_scaled[cluster_name] = kmeans.predict(Y)
    X_test_scaled[cluster_name] = kmeans.predict(Z)
    columns = []
    for x in range(1, n+1):
        columns.append(f'{cluster_name}_{x}')
    dummies = pd.get_dummies(X_train_scaled[cluster_name])
    dummies.columns = columns
    dummies2 = pd.get_dummies(X_validate_scaled[cluster_name])
    dummies2.columns = columns
    dummies3 = pd.get_dummies(X_test_scaled[cluster_name])
    dummies3.columns = columns
    X_train_scaled = pd.concat([X_train_scaled,dummies], axis=1)
    X_validate_scaled = pd.concat([X_validate_scaled,dummies2], axis=1)
    X_test_scaled = pd.concat([X_test_scaled,dummies3], axis=1)
    return X_train_scaled, X_validate_scaled, X_test_scaled
def create_clusters ( X_train_scaled , X_validate_scaled , X_test_scaled , features , n , cluster_name ) : from sklearn . cluster import KMeans X = X_train_scaled [ features ] Y = X_validate_scaled [ features ] Z = X_test_scaled [ features ] kmeans = KMeans ( n_clusters = n , random_state = 666 ) kmeans . fit ( X ) X_train_scaled [ cluster_name ] = kmeans . predict ( X ) X_validate_scaled [ cluster_name ] = kmeans . predict ( Y ) X_test_scaled [ cluster_name ] = kmeans . predict ( Z ) columns = [ ] for x in range ( 1 , n + 1 ) : columns . append ( f'{cluster_name}_{x}' ) dummies = pd . get_dummies ( X_train_scaled [ cluster_name ] ) dummies . columns = columns dummies2 = pd . get_dummies ( X_validate_scaled [ cluster_name ] ) dummies2 . columns = columns dummies3 = pd . get_dummies ( X_test_scaled [ cluster_name ] ) dummies3 . columns = columns X_train_scaled = pd . concat ( [ X_train_scaled , dummies ] , axis = 1 ) X_validate_scaled = pd . concat ( [ X_validate_scaled , dummies2 ] , axis = 1 ) X_test_scaled = pd . concat ( [ X_test_scaled , dummies3 ] , axis = 1 ) return X_train_scaled , X_validate_scaled , X_test_scaled
Create clusters based on features specified.
Create clusters based on features specified .
'''
    Create clusters based on features specified. n is amount of groups within the cluster.
    Best when used on scaled dfs. Returns dfs with dummy variables of clusters appended.
    ''' # create object with clusters chosen by n parameter # fit to train only and the features chosen # add a column to the dfs of the prediction of cluster group # naming the cluster groups by cluster name plus numbers 1 through n for each group # create dataframe of dummy variables of cluster group created for each train, validate, test # train cluster dummy variables # validate cluster dummy variables # test cluster dummy variables # add cluster dummy variables to scaled df # adding train cluster dummies to train scaled df # adding validate cluster dummies to validate scaled df # adding test cluster dummies to test scaled df
X_train_scaled
X_validate_scaled
X_test_scaled
features
n
cluster_name