import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
x, y = datasets.make_moons(n_samples=500, noise=.3, random_state=42) #生成数据集
print(x)
[[ 8.31039149e-01 -2.58748754e-01] [ 1.18506381e+00 9.20387143e-01] [ 1.16402213e+00 -4.55525583e-01] [-2.36556013e-02 1.08628844e+00] [ 4.80502733e-01 1.50942444e+00] [ 1.31164912e+00 -5.51176060e-01]
[ 1.30173265e-01 1.09442697e+00] [ 2.82035071e-01 1.04835431e+00] [ 2.74530128e-01 -1.42660544e-01] [ 1.82398104e-01 2.96612334e-02] [ 1.26017313e+00 -5.89217041e-01] [-2.66492596e-02 1.58396005e+00]]
#数据集可视化
plt.scatter(x[y==0,0], x[y==0,1])
plt.scatter(x[y==1,0], x[y==1,1])
plt.show()
#用逻辑回归算法进行训练
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.2, random_state=27)
from sklearn.linear_model import LogisticRegression
log = LogisticRegression()
log.fit(x_train, y_train)
log.score(x_test, y_test)
F:\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning. FutureWarning)
0.87
#用支持向量机算法进行训练
from sklearn.svm import SVC
svm = SVC()
svm.fit(x_train, y_train)
svm.score(x_test, y_test)
F:\Anaconda3\lib\site-packages\sklearn\svm\base.py:196: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning. "avoid this warning.", FutureWarning)
0.93
#用决策树算法进行训练
from sklearn.tree i