逻辑回归测试
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
data = np.loadtxt(r'D:\data\machine-learning-ex2\machine-learning-ex2\ex2\ex2data1.txt', delimiter=',')
data_x = data[:, [0, 1]]
data_y = data[:, [2]]
x_train, x_test, y_train, y_test = train_test_split(data_x, data_y, test_size=0.3, random_state=3)
y_train = np.ravel(y_train)
model = KNeighborsClassifier(n_neighbors=3)
model.fit(x_train, y=y_train)
print(model.predict(x_test))
print(y_test.T)
print(model.score(x_test, y_test))
两个邻居时。
C:\Users\G3\Anaconda3\python.exe D:/test/tool/tool.py
[1. 0. 1. 0. 1. 0. 0. 1. 1. 1. 1. 0. 1. 1. 0. 1. 1. 0. 1. 0. 1. 1. 1. 1.
0. 0. 0. 0. 1. 1.]
[[1. 0. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1. 0. 1. 0. 1. 1. 1. 1.
0. 0. 0. 0. 1. 1.]]
0.9
三个邻居时。
C:\Users\G3\Anaconda3\python.exe D:/test/tool/tool.py
[1. 0. 1. 0. 1. 0. 0. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1. 0. 1. 0. 1. 1. 1. 1.
1. 0. 1. 0. 1. 1.]
[[1. 0. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1. 0. 1. 0. 1. 1. 1. 1.
0. 0. 0. 0. 1. 1.]]
0.8666666666666667
邻居为三,正则化之后。
C:\Users\G3\Anaconda3\python.exe D:/test/tool/tool.py
[1. 0. 1. 0. 1. 0. 0. 1. 1. 1. 1. 0. 1. 1. 0. 1. 1. 0. 1. 0. 1. 1. 1. 1.
0. 0. 0. 0. 1. 1.]
[[1. 0. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1. 0. 1. 0. 1. 1. 1. 1.
0. 0. 0. 0. 1. 1.]]
0.9
Process finished with exit code 0
测试用例和之前一样,预测结果比较理想。
可通过正则化以及随机多次训练来提高拟合效果。
还没试过K折交叉验证。