(instruction=None,
                      dataset=None,
                      ca_threshold=None,
                      preprocess=True,
                      drop=None,
                      min_neighbors=3,
                      max_neighbors=10,
                      leaf_size=30,
                      p=2,
                      test_size=0.2,
                      random_state=49,
                      algorithm='auto',
                      text=[])
return {'id': generate_id(),
            "model": knn,
            'num_classes': num_classes,
            "accuracy": {'accuracy_score': scores[scores.index(max(scores))],
                         'cross_val_score': cross_val_score(knn,
                                                            X_train,
                                                            y_train,
                                                            cv=3)},
            "preprocessor": full_pipeline,
            "interpreter": label_mappings,
            'test_data': {'X': X_test,
                          'y': y_test},
            "target": remove}
function to train a nearest neighbor algorithm
    :param many params: used to hyperparametrize the function.
    :return a dictionary object with all of the information for the algorithm.
function to train a nearest neighbor algorithm
    :param many params: used to hyperparametrize the function.
    :return a dictionary object with all of the information for the algorithm.
function to train a nearest neighbor algorithm : param many params : used to hyperparametrize the function . : return a dictionary object with all of the information for the algorithm .
def nearest_neighbors(instruction=None,
                      dataset=None,
                      ca_threshold=None,
                      preprocess=True,
                      drop=None,
                      min_neighbors=3,
                      max_neighbors=10,
                      leaf_size=30,
                      p=2,
                      test_size=0.2,
                      random_state=49,
                      algorithm='auto',
                      text=[]):
    '''
    function to train a nearest neighbor algorithm
    :param many params: used to hyperparametrize the function.
    :return a dictionary object with all of the information for the algorithm.
    '''
    
    logger("Reading in dataset")
    # Reads in dataset
    # data = pd.read_csv(self.dataset)
    dataReader = DataReader(dataset)
    data = dataReader.data_generator()
    if drop is not None:
        data.drop(drop, axis=1, inplace=True)
    logger("Preprocessing data")
    data, y, remove, full_pipeline = initial_preprocessor(
        data, instruction, preprocess, ca_threshold, text, test_size=test_size, random_state=random_state)
    logger("->", "Target column found: {}".format(remove))
    X_train = data['train']
    y_train = y['train']
    X_test = data['test']
    y_test = y['test']
    # classification_column = get_similar_column(getLabelwithInstruction(instruction), data)
    num_classes = len(np.unique(y))
    # encodes the label dataset into 0's and 1's
    y_vals = np.unique(pd.concat([y['train'], y['test']], axis=0))
    label_mappings = sklearn.preprocessing.LabelEncoder()
    label_mappings.fit(y_vals)

    y_train = label_mappings.transform(y_train)
    y_test = label_mappings.transform(y_test)
    logger("Labels being mapped to appropriate classes")
    models = []
    scores = []
    logger("Fitting nearest neighbors model")
    logger("Identifying optimal number of neighbors")
    # Tries all neighbor possibilities, based on either defaults or user
    # specified values
    num_neighbors = []
    for x in range(min_neighbors, max_neighbors):
        knn = KNeighborsClassifier(
            n_neighbors=x,
            leaf_size=leaf_size,
            p=p,
            algorithm=algorithm)
        knn.fit(X_train, y_train)
        models.append(knn)
        scores.append(accuracy_score(knn.predict(X_test), y_test))
        num_neighbors.append(x)

    logger("->", "Optimal number of neighbors found: {}".format(
        num_neighbors[scores.index(max(scores))]))
    logger(
        "->", "Accuracy found on testing set: {}".format(scores[scores.index(max(scores))]))
    logger("Stored model under 'nearest_neighbors' key")
    knn = models[scores.index(min(scores))]
    clearLog()
    return {'id': generate_id(),
            "model": knn,
            'num_classes': num_classes,
            "accuracy": {'accuracy_score': scores[scores.index(max(scores))],
                         'cross_val_score': cross_val_score(knn,
                                                            X_train,
                                                            y_train,
                                                            cv=3)},
            "preprocessor": full_pipeline,
            "interpreter": label_mappings,
            'test_data': {'X': X_test,
                          'y': y_test},
            "target": remove}
    clearLog()
def nearest_neighbors ( instruction = None , dataset = None , ca_threshold = None , preprocess = True , drop = None , min_neighbors = 3 , max_neighbors = 10 , leaf_size = 30 , p = 2 , test_size = 0.2 , random_state = 49 , algorithm = 'auto' , text = [ ] ) : logger ( "Reading in dataset" ) # Reads in dataset # data = pd.read_csv(self.dataset) dataReader = DataReader ( dataset ) data = dataReader . data_generator ( ) if drop is not None : data . drop ( drop , axis = 1 , inplace = True ) logger ( "Preprocessing data" ) data , y , remove , full_pipeline = initial_preprocessor ( data , instruction , preprocess , ca_threshold , text , test_size = test_size , random_state = random_state ) logger ( "->" , "Target column found: {}" . format ( remove ) ) X_train = data [ 'train' ] y_train = y [ 'train' ] X_test = data [ 'test' ] y_test = y [ 'test' ] # classification_column = get_similar_column(getLabelwithInstruction(instruction), data) num_classes = len ( np . unique ( y ) ) # encodes the label dataset into 0's and 1's y_vals = np . unique ( pd . concat ( [ y [ 'train' ] , y [ 'test' ] ] , axis = 0 ) ) label_mappings = sklearn . preprocessing . LabelEncoder ( ) label_mappings . fit ( y_vals ) y_train = label_mappings . transform ( y_train ) y_test = label_mappings . transform ( y_test ) logger ( "Labels being mapped to appropriate classes" ) models = [ ] scores = [ ] logger ( "Fitting nearest neighbors model" ) logger ( "Identifying optimal number of neighbors" ) # Tries all neighbor possibilities, based on either defaults or user # specified values num_neighbors = [ ] for x in range ( min_neighbors , max_neighbors ) : knn = KNeighborsClassifier ( n_neighbors = x , leaf_size = leaf_size , p = p , algorithm = algorithm ) knn . fit ( X_train , y_train ) models . append ( knn ) scores . append ( accuracy_score ( knn . predict ( X_test ) , y_test ) ) num_neighbors . append ( x ) logger ( "->" , "Optimal number of neighbors found: {}" . format ( num_neighbors [ scores . index ( max ( scores ) ) ] ) ) logger ( "->" , "Accuracy found on testing set: {}" . format ( scores [ scores . index ( max ( scores ) ) ] ) ) logger ( "Stored model under 'nearest_neighbors' key" ) knn = models [ scores . index ( min ( scores ) ) ] clearLog ( ) return { 'id' : generate_id ( ) , "model" : knn , 'num_classes' : num_classes , "accuracy" : { 'accuracy_score' : scores [ scores . index ( max ( scores ) ) ] , 'cross_val_score' : cross_val_score ( knn , X_train , y_train , cv = 3 ) } , "preprocessor" : full_pipeline , "interpreter" : label_mappings , 'test_data' : { 'X' : X_test , 'y' : y_test } , "target" : remove } clearLog ( )