In [46]:
# 导入头文件
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC

import matplotlib.pyplot as plt
In [47]:
data = pd.read_csv('data.csv')
np.array(data['y'])
Out[47]:
array([0., 1., 1., 1., 0., 0., 1., 0., 1., 1., 1., 1., 0., 1., 0., 0., 1.,
       0., 1., 1., 1., 0., 1., 0., 0., 0., 0., 0., 1., 0., 1., 1., 1., 1.,
       1., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1.,
       0., 0., 1., 1., 0., 0., 0., 1., 1., 1., 1., 1., 1., 0., 1., 0., 0.,
       0., 1., 0., 0., 1., 1., 1., 0., 0., 1., 0., 1., 1., 1., 0., 1., 1.,
       1., 1., 0., 0., 1., 0., 0., 0., 1., 1., 0., 0., 1., 1., 1.])
In [48]:
# 分解数据中的
X = np.array(data[['x1', 'x2']])
y = np.array(data['y'])
In [49]:
plt.scatter(X[:,0],X[:,1],c=y)
Out[49]:
<matplotlib.collections.PathCollection at 0x1a1814edd8>
In [50]:
# Logistic Regression Classifier
lg_classifier = LogisticRegression()
lg_classifier.fit(X,y)
/Users/weixu/AI/tool/anaconda3/envs/py3/lib/python3.6/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)
Out[50]:
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, max_iter=100, multi_class='warn',
          n_jobs=None, penalty='l2', random_state=None, solver='warn',
          tol=0.0001, verbose=0, warm_start=False)
In [59]:
y_p = classifier.predict(X)
In [60]:
plt.scatter(X[:,0],X[:,1],c=y_p)
Out[60]:
<matplotlib.collections.PathCollection at 0x1a152209e8>
In [61]:
Dt_classifier = DecisionTreeClassifier()
Dt_classifier.fit(X, y)
Out[61]:
DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
            max_features=None, max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, presort=False, random_state=None,
            splitter='best')
In [63]:
y_Dp = Dt_classifier.predict(X)
plt.scatter(X[:,0],X[:,1],c=y_Dp)
Out[63]:
<matplotlib.collections.PathCollection at 0x1a18ab8390>
In [57]:
svc_classifier = SVC()
svc_classifier.fit(X, y)
/Users/weixu/AI/tool/anaconda3/envs/py3/lib/python3.6/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)
Out[57]:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
  kernel='rbf', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False)
In [64]:
y_sp = svc_classifier.predict(X)
plt.scatter(X[:,0],X[:,1],c=y_sp)
Out[64]:
<matplotlib.collections.PathCollection at 0x1a1828b6d8>