Exemplo n.º 1
0
    def test_kNNClassifier(self):
        iris = datasets.load_iris()
        X = iris['data']
        y = iris['target']

        X_train, y_train, X_test, y_test = train_test_split(X,
                                                            y,
                                                            test_ratio=0.2)

        knn_clf = KNNClassifier(k_neighbours=3)
        fit_result = knn_clf.fit(X_train, y_train)
        print(fit_result)

        # x = np.array([8.093607318, 3.365731514])
        # X_predict = np.array(x).reshape(1, -1)
        y_predict = knn_clf.predict(X_test)
        print(y_predict)
        print(y_test)

        score = accuracy_rate(y_test, y_predict)
        print(score)
        print(knn_clf.score(X_test, y_test))

        plt.scatter(X_train[y_train == 0, 0],
                    X_train[y_train == 0, 1],
                    color='g')
        plt.scatter(X_train[y_train == 1, 0],
                    X_train[y_train == 1, 1],
                    color='b')
        plt.scatter(X_train[y_train == 2, 0],
                    X_train[y_train == 2, 1],
                    color='r')
        plt.show()
Exemplo n.º 2
0
    def test_LinearRegerssion(self):
        boston = datasets.load_boston()
        X = boston['data']
        y = boston['target']

        X = X[y < 50.0]
        y = y[y < 50.0]

        X_train, y_train, X_test, y_test = train_test_split(X, y)

        regression = LinearRegression()
        print(regression.fit_normal(X_train, y_train))
        print('score={}'.format(regression.score(X_test, y_test)))
Exemplo n.º 3
0
    def test_StandardScaler(self):
        iris = datasets.load_iris()
        X = iris['data']
        y = iris['target']

        X_train, y_train, X_test, y_test = train_test_split(X, y)

        # print(X_train)
        scaler = StandardScaler()
        fit = scaler.fit(X_train)
        print(fit)
        X_train = scaler.transform(X_train)
        print(X_train)

        # print(X_test)
        X_test = scaler.transform(X_test)
        print(X_test)
Exemplo n.º 4
0
    def test_stochastic_gradient_descent(self):
        boston = datasets.load_boston()
        X = boston['data']
        y = boston['target']

        X = X[y < 50.0]
        y = y[y < 50.0]

        X_train, y_train, X_test, y_test = train_test_split(X, y)

        scaler = StandardScaler()
        scaler.fit(X_train)
        X_train_standard = scaler.transform(X_train)
        X_test_standard = scaler.transform(X_test)

        lin_reg = LinearRegression()
        lin_reg.fit_stochastic_gradient_descent(X_train_standard, y_train)
        print(lin_reg.score(X_test_standard, y_test))
Exemplo n.º 5
0
    def test_find_the_best_k(self):
        iris = datasets.load_iris()
        X = iris['data']
        y = iris['target']

        X_train, y_train, X_test, y_test = train_test_split(X, y)

        best_score = 0.0
        best_k = -1

        for k in range(1, 11):
            knn_clf = KNNClassifier(k_neighbours=k)
            knn_clf.fit(X_train, y_train)
            score = knn_clf.score(X_test, y_test)
            if score > best_score:
                best_score = score
                best_k = k

        print('The best K is {} and the best score is {}'.format(
            best_k, best_score))
Exemplo n.º 6
0
    def test_MSE_RMSE_MAE_R2(self):
        boston = datasets.load_boston()
        print(boston['feature_names'])
        X = boston['data'][:, 5]  # Get the `RM`(room) data
        y = boston['target']

        # clean data
        X = X[y < 50.0]
        y = y[y < 50.0]

        x_train, y_train, x_test, y_test = train_test_split(X, y)

        regression = SimpleLinearRegression()
        print(regression.fit(x_train, y_train))
        y_predict = regression.predict(x_test)

        print(mean_squared_error(y_test, y_predict))
        print(root_meas_squared_error(y_test, y_predict))
        print(mean_absolute_error(y_test, y_predict))
        print(regression.score(x_test, y_test))

        plt.scatter(x_train, y_train)
        plt.plot(x_train, regression.predict(x_train), color='r')
        plt.show()