Пример #1
0
 def test__predict_with_tans_fun(self):
     X = array([[2, 4], [6, 8]])
     Y = array(['a', 'b'])
     knn = KNN(trans_fun=KNN.STD_TRANS)
     knn.fit(X, Y)
     self.assertEqual(knn.predict(array([4, 5]), 1), 'a')
     self.assertEqual(knn.predict(array([5, 6]), 1), 'b')
Пример #2
0
 def test__predict_with_tans_fun(self):
     X = array([[2, 4], [6, 8]])
     Y = array(['a','b'])
     knn = KNN(trans_fun = KNN.STD_TRANS)
     knn.fit(X,Y)
     self.assertEqual(knn.predict(array([4,5]), 1), 'a')
     self.assertEqual(knn.predict(array([5,6]), 1), 'b')
Пример #3
0
 def test_basic_predict(self):
     '''全部默认状态使用KNN'''
     X = array([[1,2],[1.5,3],[1.3,2.5],[4,7],[6,6],[5,8]])
     Y = array(['a','c','a','b','b','a'])
     knn = KNN().fit(X,Y)
     self.assertEqual(knn.predict(array([2,2]), 3),'a')
     self.assertEqual(knn.predict(array([5,5]), 3),'b')
     self.assertEqual(knn.predict(array([5,5]), 6),'a')
     self.assertEqual(knn.predict(array([1,5]), 10),'a')
Пример #4
0
 def test_basic_predict(self):
     '''全部默认状态使用KNN'''
     X = array([[1, 2], [1.5, 3], [1.3, 2.5], [4, 7], [6, 6], [5, 8]])
     Y = array(['a', 'c', 'a', 'b', 'b', 'a'])
     knn = KNN().fit(X, Y)
     self.assertEqual(knn.predict(array([2, 2]), 3), 'a')
     self.assertEqual(knn.predict(array([5, 5]), 3), 'b')
     self.assertEqual(knn.predict(array([5, 5]), 6), 'a')
     self.assertEqual(knn.predict(array([1, 5]), 10), 'a')
Пример #5
0
    def process(self, event):
        """
        event.event_type
            'modified' | 'created' | 'moved' | 'deleted'
        event.is_directory
            True | False
        event.src_path
            path/to/observed/file
        """
        # the file will be processed there
        print(event.src_path, event.event_type)  # print now only for degug
        if (event.event_type == "modified"):
            picture_path = "./knn/test/Unknown.jpeg"

            # full_file_path = os.path.join("knn/test", picture_path)
            # print(full_file_path)

            print("Looking for faces in {}".format(picture_path))

            # Find all people in the image using a trained classifier model
            # Note: You can pass in either a classifier file name or a classifier model instance
            predictions = knn.predict(picture_path, model_path="trained_knn_model.clf")

            if len(predictions) > 0:
                # Print results on the console
                for name, (top, right, bottom, left) in predictions:
                    print("- Found {} at ({}, {})".format(name, left, top))
                    r = requests.post('http://192.168.1.101:1880/find-face', data = {'_id':name})
                    print("call done")
            else:
                r = requests.post('http://192.168.1.101:1880/no-face')
def train_plot(features, labels):
    y0, y1 = features[:, 2].min() * .9, features[:, 2].max() * 1.1
    x0, x1 = features[:, 0].min() * .9, features[:, 0].max() * 1.1
    X = np.linspace(x0, x1, 100)
    Y = np.linspace(y0, y1, 100)
    X, Y = np.meshgrid(X, Y)

    model = fit_model(1, features[:, (0, 2)], np.array(labels))
    C = predict(
        np.vstack([X.ravel(), Y.ravel()]).T, model).reshape(X.shape)
    if COLOUR_FIGURE:
        cmap = ListedColormap([(1., .6, .6), (.6, 1., .6), (.6, .6, 1.)])
    else:
        cmap = ListedColormap([(1., 1., 1.), (.2, .2, .2), (.6, .6, .6)])
    plt.xlim(x0, x1)
    plt.ylim(y0, y1)
    plt.xlabel(feature_names[0])
    plt.ylabel(feature_names[2])
    plt.pcolormesh(X, Y, C, cmap=cmap)
    if COLOUR_FIGURE:
        cmap = ListedColormap([(1., .0, .0), (.0, 1., .0), (.0, .0, 1.)])
        plt.scatter(features[:, 0], features[:, 2], c=labels, cmap=cmap)
    else:
        for lab, ma in zip(range(3), "Do^"):
            plt.plot(features[labels == lab, 0], features[
                     labels == lab, 2], ma, c=(1., 1., 1.))
Пример #7
0
def main():
    # load and normalize the data
    data, labels = load_data(
        'veh-prime.arff',
        filterFn=lambda line: len(line.strip()) > 0 and line[0] != '@',
        labelFn=lambda label: {
            'car': 1.0,
            'noncar': 0.0
        }[label])
    data = zscore(data)
    n = data.shape[DATA_AXIS]
    d = data.shape[FEATURE_AXIS]

    # score each feature in the data
    scores = np.zeros(d)
    for j in range(d):
        scores[j] = abs(pcc(data[:, j], labels))
    feature_indices = np.flipud(scores.argsort())
    for index in feature_indices:
        print "f=%d,|r|=%f" % (index + 1, scores[index])
    print

    # try adding features in order of score
    data_indices = [x for x in range(data.shape[DATA_AXIS])]
    for m in range(1, d + 1):
        correct = 0.0
        subdata = data[:, feature_indices[:m]]
        for i in range(n):
            indices = data_indices[:i] + data_indices[i + 1:]
            predicted = predict(subdata[None, i], subdata[indices],
                                labels[indices], k)
            if predicted[0] == labels[i]:
                correct += 1
        print "m=%d,accuracy=%f" % (m, correct / n)
    print
def k_fold_cross_val(input_data, K, k):
    """Splits the input data set into a k number of sections/folds where each fold is used as a testing set 
    and the rest of input data are used as training set.
    Returns a mean accuracy of predicting model used.

    Prameters:
    
    k (int): Number of folds \n
    K (int): Number of nearest neighbors
    """
    k_fold_sets = k_fold_data_split(input_data, k)
    all_accuracy = []
    for k_set in k_fold_sets:
        test_set = k_set[0]
        train_set = k_set[1]
        all_predictions = []
        for x in range(len(test_set)):
            neighbors = k_neighbors(test_set[x][:-1], train_set, K)
            prediction = predict(neighbors)
            all_predictions.append(prediction)
        accuracy = accuracy_score(test_set, all_predictions)
        all_accuracy.append(accuracy)
    
    mean_accuracy = sum(all_accuracy) / len(all_accuracy)

    return mean_accuracy
Пример #9
0
def kNN_test(X_train, X_test, y_train, y_test, distance = "euclidean",k=3):
    output_classes = []
    for i in range(0, X_test.shape[0]):
        output = knn.train(X_train, X_test[i],distance,k)
        predictedClass = knn.predict(output, y_train)
        output_classes.append(predictedClass)
    return output_classes
Пример #10
0
def plot_decision(features, labels):
    '''KNN에 대한 결정선 그리기

    Parameters
    ----------
    features : ndarray
    labels : sequence

    Returns
    -------
    fig : Matplotlib Figure
    ax  : Matplotlib Axes
    '''
    y0, y1 = features[:, 2].min() * .9, features[:, 2].max() * 1.1
    x0, x1 = features[:, 0].min() * .9, features[:, 0].max() * 1.1
    X = np.linspace(x0, x1, 100)
    Y = np.linspace(y0, y1, 100)
    X, Y = np.meshgrid(X, Y)

    model = fit_model(1, features[:, (0, 2)], np.array(labels))
    ## 원본 소스 오류
    # C = predict(
    #     np.vstack([X.ravel(), Y.ravel()]).T, model).reshape(X.shape)
     C = predict(
        model, np.vstack([X.ravel(), Y.ravel()]).T).reshape(X.shape)
Пример #11
0
def run_with_sample() -> None:
    total_point = 0.0
    number_test = 100
    for i in range(0, number_test):
        data = get_train_data()
        samples: List[FrameOrSeries] = []
        data[0], sample = get_sample_data(data[0], 25)  # type: FrameOrSeries
        samples.append(sample)
        data[1], sample = get_sample_data(data[1], 25)  # type: FrameOrSeries
        samples.append(sample)

        knn.clear()
        knn.fit(data[0].values.tolist(), data[1].values.tolist())

        point = 0
        total = 0
        for k in range(0, len(samples)):
            for s in samples[k].values:
                if knn.predict(s.tolist(), k=3) == k:
                    point += 1
                total += 1

        print('point: ' + str(point))
        print('total: ' + str(total))
        total_point += float(point / total * 100)
        print(point / total * 100)

    print('total_point: ' + str(total_point / number_test))
Пример #12
0
def train_plot(features, labels):
    y0, y1 = features[:, 2].min() * .9, features[:, 2].max() * 1.1
    x0, x1 = features[:, 0].min() * .9, features[:, 0].max() * 1.1
    X = np.linspace(x0, x1, 100)
    Y = np.linspace(y0, y1, 100)
    X, Y = np.meshgrid(X, Y)

    model = fit_model(1, features[:, (0, 2)], np.array(labels))
    C = predict(np.vstack([X.ravel(), Y.ravel()]).T, model).reshape(X.shape)
    if COLOUR_FIGURE:
        cmap = ListedColormap([(1., .6, .6), (.6, 1., .6), (.6, .6, 1.)])
    else:
        cmap = ListedColormap([(1., 1., 1.), (.2, .2, .2), (.6, .6, .6)])
    plt.xlim(x0, x1)
    plt.ylim(y0, y1)
    plt.xlabel(feature_names[0])
    plt.ylabel(feature_names[2])
    plt.pcolormesh(X, Y, C, cmap=cmap)
    if COLOUR_FIGURE:
        cmap = ListedColormap([(1., .0, .0), (.0, 1., .0), (.0, .0, 1.)])
        plt.scatter(features[:, 0], features[:, 2], c=labels, cmap=cmap)
    else:
        for lab, ma in zip(range(3), "Do^"):
            plt.plot(features[labels == lab, 0],
                     features[labels == lab, 2],
                     ma,
                     c=(1., 1., 1.))
Пример #13
0
def draw_box_on_image(num_hands_detect, score_thresh, scores, boxes, im_width, 
                      im_height, image_np):
    # print(num_hands_detect)

    hands = ['None', 'None']
    for i in range(num_hands_detect):
        if (scores[i] > score_thresh):
            (left, right, top, bottom) = (boxes[i][1] * im_width,
                                          boxes[i][3] * im_width,
                                          boxes[i][0] * im_height,
                                          boxes[i][2] * im_height)
            p1 = (int(left), int(top))
            p2 = (int(right), int(bottom))
            cv2.rectangle(image_np, p1, p2, (77, 255, 9), 2, 2)

            test = image_np[int(top): int(bottom), int(left):int(right)]
            test = cv2.resize(test, (128, 128))
            test = cv2.cvtColor(test, cv2.COLOR_RGB2GRAY)
            label = predict(test,lda)
            # font = cv2.FONT_HERSHEY_SIMPLEX
            if i == 0:
                # cv2.putText(image_np, str(label), (100, 100), font, 1,
                #             (255, 0, 0), 2, cv2.LINE_AA)
                hands[0] = str(label[0])
            if i == 1:
                # cv2.putText(image_np, str(label), (150, 100), font, 1,
                #             (255, 0, 0), 2, cv2.LINE_AA)
                hands[1] = str(label[0])
    return hands
Пример #14
0
def predict_thread(face_algorithm, model_name, image_file):
    # https://discuss.streamlit.io/t/attributeerror-thread-local-object-has-no-attribute-value/574/3
    import keras.backend.tensorflow_backend as tb
    tb._SYMBOLIC_SCOPE.value = True
    return knn.predict(image_file, 
        model_path=model_name, 
        distance_threshold=ALGORITHM[face_algorithm]['distance_threshold'],
        face_algorithm=face_algorithm)
Пример #15
0
def run() -> None:
    test_data = get_test_data()
    data = get_train_data()
    knn.clear()
    knn.fit(data[0].values.tolist(), data[1].values.tolist())
    test_list: list = test_data.values.tolist()
    results = []
    for i in test_list:  # type: list
        id = i[:1][0]
        del i[0]
        results.append([int(id), knn.predict(i)])
    write_to_csv(results)
Пример #16
0
def main():
    # load and normalize the data
    data, labels = load_data(
        'veh-prime.arff',
        filterFn = lambda line: len(line.strip()) > 0 and line[0] != '@',
        labelFn = lambda label: {'car': 1.0, 'noncar': 0.0}[label]
    )
    data = zscore(data)
    n = data.shape[DATA_AXIS]
    d = data.shape[FEATURE_AXIS]

    # initialize set of unused features
    data_indices = [x for x in range(data.shape[DATA_AXIS])]
    candidates = set([j for j in range(d)])
    features = []
    best_accr = 0.0

    # search for the best feature to add next
    while len(candidates) > 0:
        best_candidate = None
        for candidate in candidates:
            test_features = features + [candidate]
            subdata = data[:,test_features]

            # run LOOCV and find average accuracy
            correct = 0.0
            for i in range(n):
                indices = data_indices[:i] + data_indices[i+1:]
                predicted = predict(
                    subdata[None,i],
                    subdata[indices],
                    labels[indices], k
                )
                if predicted[0] == labels[i]:
                    correct += 1
            accr = correct / n

            # update best accuracy if better
            if (accr > best_accr):
                best_accr = accr
                best_candidate = candidate

        # update set of features or break if none found    
        if best_candidate is None:
            break
        else:
            candidates.remove(best_candidate)
            features.append(best_candidate)
            pretty_features = [index + 1 for index in features]
            msg = "features=%s,accuracy=%f"
            print msg % (pretty_features, best_accr)
    print
Пример #17
0
def gridsearchKNN2(parametros, xTrain, yTrain, Xval, yVal):
    f1Metric = []
    par = []
    for params in ParameterGrid(parametros):
        knn = KNeighborsClassifier(n_neighbors=params['n_neighbors'],
                                   metric=params['metric'])
        knn.fit(xTrain, yTrain)
        pred = knn.predict(Xval)
        f1 = metrics.f1_score(yVal, pred, average='weighted')
        print(f1)
        f1Metric.append(f1)
        par.append(params)
    return par[f1Metric.index(max(f1Metric))]
Пример #18
0
def bruteForceBestHyperParams(diagnosis):
    hyperParams = []
    for sizes in [s * 0.1 for s in range(4, 9)]:
        for k in [k for k in range(1, 15)]:
            trainingData, testingData = crossValidate(dataset, trainSize=sizes)
            correct = 0
            total = len(testingData[diagnosis])

            for i in range(total):
                example = testingData[diagnosis][i]
                if knn.predict(trainingData, example, k=k) == diagnosis:
                    correct += 1
            hyperParams.append((100 * correct / total, sizes, k))

    hyperParams.sort(key=lambda t: t[0])
    return hyperParams
def plot_decision(features, labels):
    '''Plots decision boundary for KNN

    Parameters
    ----------
    features : ndarray
    labels : sequence

    Returns
    -------
    fig : Matplotlib Figure
    ax  : Matplotlib Axes
    '''
    y0, y1 = features[:, 2].min() * .9, features[:, 2].max() * 1.1
    x0, x1 = features[:, 0].min() * .9, features[:, 0].max() * 1.1
    X = np.linspace(x0, x1, 100)
    Y = np.linspace(y0, y1, 100)
    X, Y = np.meshgrid(X, Y)

    model = fit_model(1, features[:, (0, 2)], np.array(labels))
    C = predict(model, np.vstack([X.ravel(), Y.ravel()]).T).reshape(X.shape)
    if COLOUR_FIGURE:
        cmap = ListedColormap([(1., .6, .6), (.6, 1., .6), (.6, .6, 1.)])
    else:
        cmap = ListedColormap([(1., 1., 1.), (.2, .2, .2), (.6, .6, .6)])
    fig, ax = plt.subplots()
    ax.set_xlim(x0, x1)
    ax.set_ylim(y0, y1)
    ax.set_xlabel(feature_names[0])
    ax.set_ylabel(feature_names[2])
    ax.pcolormesh(X, Y, C, cmap=cmap)
    if COLOUR_FIGURE:
        cmap = ListedColormap([(1., .0, .0), (.0, 1., .0), (.0, .0, 1.)])
        ax.scatter(features[:, 0], features[:, 2], c=labels, cmap=cmap)
    else:
        for lab, ma in zip(range(3), "Do^"):
            ax.plot(features[labels == lab, 0],
                    features[labels == lab, 2],
                    ma,
                    c=(1., 1., 1.))
    return fig, ax
Пример #20
0
def evaluate(evaluateDir, modelPath):
    right = 0
    wrong = 0
    # Loop through each person in the training set
    for classDir in os.listdir(evaluateDir):
        if not os.path.isdir(os.path.join(evaluateDir, classDir)):
            continue
        # Loop through each training image for the current person
        for imgPath in image_files_in_folder(
            os.path.join(evaluateDir, classDir)
        ):
            faces = predict(imgPath, model_path=modelPath)
            if faces:
                # There is only 1 face
                face = faces[0]
                face_name = face[0]
                if face_name == classDir:
                    right = right + 1
                else:
                    wrong = wrong + 1
    return right / (right + wrong) * 100
def plot_decision(features, labels):
    '''Plots decision boundary for KNN

    Parameters
    ----------
    features : ndarray
    labels : sequence

    Returns
    -------
    fig : Matplotlib Figure
    ax  : Matplotlib Axes
    '''
    y0, y1 = features[:, 2].min() * .9, features[:, 2].max() * 1.1
    x0, x1 = features[:, 0].min() * .9, features[:, 0].max() * 1.1
    X = np.linspace(x0, x1, 100)
    Y = np.linspace(y0, y1, 100)
    X, Y = np.meshgrid(X, Y)

    model = fit_model(1, features[:, (0, 2)], np.array(labels))
    C = predict(
        model, np.vstack([X.ravel(), Y.ravel()]).T).reshape(X.shape)
    if COLOUR_FIGURE:
        cmap = ListedColormap([(1., .6, .6), (.6, 1., .6), (.6, .6, 1.)])
    else:
        cmap = ListedColormap([(1., 1., 1.), (.2, .2, .2), (.6, .6, .6)])
    fig,ax = plt.subplots()
    ax.set_xlim(x0, x1)
    ax.set_ylim(y0, y1)
    ax.set_xlabel(feature_names[0])
    ax.set_ylabel(feature_names[2])
    ax.pcolormesh(X, Y, C, cmap=cmap)
    if COLOUR_FIGURE:
        cmap = ListedColormap([(1., .0, .0), (.0, 1., .0), (.0, .0, 1.)])
        ax.scatter(features[:, 0], features[:, 2], c=labels, cmap=cmap)
    else:
        for lab, ma in zip(range(3), "Do^"):
            ax.plot(features[labels == lab, 0], features[
                     labels == lab, 2], ma, c=(1., 1., 1.))
    return fig,ax
Пример #22
0
def result(id):
    print("id:", id)
    predict_value = knn.predict(db_func.read_one_data(id))
    db_func.update_predict_value(id, predict_value)

    return Response(str(predict_value))
Пример #23
0
test_data_set = []
train_data_set = []
n = int((len(titanik_train_set) - 1) * 0.3)
idx_test = random.sample(range(1, len(titanik_train_set)-1), n)
for idx in range(1, len(titanik_train_set)-1):
    if idx in idx_test:
        test_data_set.append(titanik_train_set[idx])
    else:
        train_data_set.append(titanik_train_set[idx])

for K in range(1, 31):
    all_predictions = []
    k_accuracy = []
    for x in range(len(test_data_set)):
        neighbors = k_neighbors(test_data_set[x][:-1], train_data_set, K)
        prediction = predict(neighbors)
        all_predictions.append(prediction)
    accuracy = accuracy_score(test_data_set, all_predictions)
    k_accuracy.append(accuracy)
    print(f"K = {K:<8} accuracy: {accuracy} % ")
print(sum(k_accuracy)/len(k_accuracy))
print()

k_accuracy = []
for K in range(1, 31):
    accuracy = round(k_fold_cross_val(titanik_train_set, K, 10), 2)
    k_accuracy.append(accuracy)
    print(f"K = {K:<8} Mean accuracy: {accuracy} % ")
print(sum(k_accuracy)/len(k_accuracy))
print()
Пример #24
0
print(feat_vec.shape)

feat_vec = np.concatenate((feat_vec[:, :1], add / np.mean(add)), axis=1)

accuracy = 0
for i in range(0, 100):

    np.random.shuffle(feat_vec)

    sli = 0.8 * len(feat_vec)

    X = np.split(feat_vec, [int(sli)], axis=0)

    y_train = np.transpose(X[0][:, :1])[0]
    X_train = X[0][:, 1:]

    y_test = np.transpose(X[1][:, :1])[0]
    X_test = X[1][:, 1:]

    knn = neighbors.KNeighborsClassifier(n_neighbors=4,
                                         weights='distance',
                                         metric='manhattan')
    knn.fit(X_train, y_train)

    test = knn.predict(X_test)

    acc = test == y_test
    accuracy += acc.sum() / len(acc)

print(accuracy / 100)
Пример #25
0
	print("Enter the movieids:")
	for j in range(0, no_movies):
		movieid=int(raw_input())
		movie_label[movieid]=label
'''
f = open('input_task5.txt', 'rb')
labels = []
for line in f:
    mov = int(line.split(" ")[0])
    lab = line.split(" ")[1].rstrip('\n')
    labels.append(lab)
    movie_label[mov] = lab

print "Training set read from file: "
print movie_label
no_labels = len(set(labels))
tot_movies = len(movie_label)

if mode == 1:
    print "Enter r for R-Nearest Neighbours: "
    k = int(raw_input())
    knn.predict(movie_label, k)

if mode == 2:
    print "Running SVM:"
    svm.multiclassSVM(movie_label, no_labels, tot_movies)

if mode == 3:
    print "Running Decision trees:"
    dtree.multiclassTree(movie_label, no_labels, tot_movies)
Пример #26
0
        model_name += ALGORITHM[face_algorithm]['ext']

    if os.path.isdir(test_thing):
        images = os.listdir(test_thing)
        images = [os.path.join(test_thing, i) for i in images]
    else:
        images = [ test_thing ]

    # Using the trained classifier, make predictions for unknown images
    for image_file in images:
        print("Looking for faces in {}".format(image_file))

        # Find all people in the image using a trained classifier model
        # Note: You can pass in either a classifier file name or a classifier model instance
        start_time = datetime.now()
        predictions = knn.predict(image_file, 
            model_path=model_name, 
            distance_threshold=ALGORITHM[face_algorithm]['distance_threshold'],
            face_algorithm=face_algorithm)
        print('[Time taken: {!s}]'.format(datetime.now() - start_time))

        # Print results on the console
        for name, (top, right, bottom, left), distance, count in predictions:
            print("- Found {} at ({}, {}), distance={}, count={}".format(name, left, top, distance, count))

        if len(predictions)==0:
            print('Face not found!')
            
        # Display results overlaid on an image
        #knn.show_prediction_labels_on_image(image_file, predictions)
Пример #27
0
    actual_value_2 = 2
    new_id_2 = db_func.insert_data(test_data_2)
    assert (new_id_2 == 2)
    assert (db_func.update_actual_value(new_id_2, actual_value_2))

    data_set = knn.construct_data_set()
    assert (len(data_set) == 2)
    v = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0])
    M = knn.generate_M(data_set)
    assert (np.array_equal(
        M, np.array([[1, 1, 0, 0, 1, 0, 1, 1, 1], [1, 1, 1, 0, 1, 0, 1, 1,
                                                   1]])))

    real_mse = np.array([2 / 3, 7 / 9])
    assert (np.array_equal(knn.calculate_mse(v, M), real_mse))

    print("generate_M and calculate_mse test PASS")

    # Insert again test_data and test_data_2 to check robustness of predict
    new_id_3 = db_func.insert_data(test_data)
    db_func.update_actual_value(new_id_3, actual_value)

    new_id_4 = db_func.insert_data(test_data_2)
    db_func.update_actual_value(new_id_4, actual_value_2)

    predict_1 = knn.predict(test_data, test=True)
    assert (predict_1 == (1, 2))
    predict_2 = knn.predict(test_data_2, test=True)
    assert (predict_2 == (2, 2))

    print("predict test PASS")
Пример #28
0
import EEG_load

# main - train&test
#dataset = EEG_load.load_data("s16",20)

dataset = EEG_feature_extraction.generate_feature_data("s16", 20)

X = dataset['X_train']
y = dataset['Y_train']
Xtest = dataset['X_test']
ytest = dataset['Y_test']

k = [1, 3, 10]
for i in range(3):
    model = knn.fit(X, y, k[i])
    y_pred = knn.predict(model, X)
    train_error = np.mean(y_pred.flatten() != y)
    print("The current training error is: %r" % train_error)

    y_pred = knn.predict(model, Xtest)
    test_error = np.mean(y_pred.flatten() != ytest)
    print("The current test error is: %r" % test_error)

# part 3: plot classification boundaries for k=1 (use utils.plot_2dclassifier)
model1 = knn.fit(X, y, k[2])
utils.plot_2dclassifier(model1, X, y)
#plt.show()

# save figure
fname = "../s16-c20-mean.png"
plt.savefig(fname)
Пример #29
0
                        ])

    io_args = parser.parse_args()
    question = io_args.question

    if question == '1.1':
        dataset = utils.load_dataset('citiesSmall')
        X = dataset['X']
        y = dataset['y']
        Xtest = dataset['Xtest']
        ytest = dataset['ytest']
        #model = knn.fit(X,y,3)
        #model = knn.fit(X,y,1)
        model = knn.fit(X, y, 10)

        y_pred_tr = knn.predict(model, X)
        y_pred_te = knn.predict(model, Xtest)
        trerror = utils.classification_error(y_pred_tr, y)
        teerror = utils.classification_error(y_pred_te, ytest)

        print(trerror)
        print(teerror)

        utils.plot_2dclassifier(model, Xtest, ytest)

        # part 1: implement knn.predict
        # part 2: print training and test errors for k=1,3,10 (use utils.classification_error)
        # part 3: plot classification boundaries for k=1 (use utils.plot_2dclassifier)

    if question == '1.2':
        dataset = utils.load_dataset('citiesBig1')
Пример #30
0
                    axis=0,
                    ignore_index=True)
    y_1 = pd.concat([y_train[:start], y_train[end:]],
                    axis=0,
                    ignore_index=True)
    X_2 = X_train[start:end]
    #y_2 = y_train[start:end]

    t1 = xgb.predict(X_1, y_1, X_2)
    t2 = gradientBoosting.predict(X_1, y_1, X_2)
    t3 = lightgbm2.predict(X_1, y_1, X_2)
    t4 = randomForest.predict(X_1, y_1, X_2)
    t5 = ridge.predict(X_1, y_1, X_2)
    t6 = lasso.predict(X_1, y_1, X_2)
    t7 = elastic.predict(X_1, y_1, X_2)
    t8 = knn.predict(X_1, y_1, X_2)
    t9 = MLPRegressor.predict(X_1, y_1, X_2)
    #t10 = kernelridge.predict(X_1, y_1, X_2)
    #t11 = kernelridge2.predict(X_1, y_1, X_2)

    if i == 0:
        X_transfer = np.concatenate((t1, t2, t3, t4, t5, t6, t7, t8, t9),
                                    axis=1)
    else:
        X_temp = np.concatenate((t1, t2, t3, t4, t5, t6, t7, t8, t9), axis=1)
        X_transfer = np.concatenate((X_transfer, X_temp), axis=0)
    i += 1

t1 = xgb.predict(X_train, y_train, X_test)
t2 = gradientBoosting.predict(X_train, y_train, X_test)
t3 = lightgbm2.predict(X_train, y_train, X_test)
Пример #31
0
# 201420907_homework1 main.py
import knn
import numpy as np


train_data = np.loadtxt(fname='./digits_data/digits_train.csv', delimiter=',', dtype='float64')   # Training Set load
X_train, Y_train = knn.train(train_data)  # KNN don't need training step. So, knn.train(data) just load train_data.

test_data = np.loadtxt(fname='./digits_data/digits_test.csv', delimiter=',', dtype='float64')     # Test Set load
X_test = np.array(test_data[:, 1:], dtype='float64')  # Features of Test set data
Y_test = np.array(test_data[:, 0], dtype='int64')  # Labels of Test set data

# knn.predict(train_x, train_y, test_x, k)
knn_Y_pred = knn.predict(X_train, Y_train, X_test, 1)  # k means n of neighbors

# Calculation of TP, TN, FP, FN
TP = np.zeros(10)  # True Positive (Correct Predicted target class)
TN = np.zeros(10)  # True Negative (Correct Predicted not-target class)
FP = np.zeros(10)  # False Positive (Incorrect Predicted target class)
FN = np.zeros(10)  # False Negative (Incorrect Predicted  non-target class)

Confusion_matrix = np.zeros((10, 10), dtype='int64')

for idx in range(200):
    Confusion_matrix[Y_test[idx]][knn_Y_pred[idx]] += 1

for i in range(10):
    TP[i] = Confusion_matrix[i][i]

    for j in range(10):
        if i != j: