注:代码和运行结果都是从jupyter notebook中导出的
导入所需要的第三方库1
2
3
4
5import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
%matplotlib inline
导入数据(iris数据集是sklearn中自带的数据集)1
2
3
4
5
6# load data
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['label'] = iris.target
df.columns = ['sepal length', 'sepal width', 'petal length', 'petal width', 'label']
df.label.value_counts()
2 50
1 50
0 50
Name: label, dtype: int64
选取两个特征sepal length
和sepal width
,绘制散点图1
2
3
4
5plt.scatter(df[:50]['sepal length'], df[:50]['sepal width'], label='0')
plt.scatter(df[50:100]['sepal length'], df[50:100]['sepal width'], label='1')
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.legend()
<matplotlib.legend.Legend at 0x1b17e936160>
数据处理:将pandas类型数据转换为np.array
1
2
3data = np.array(df.iloc[:100, [0, 1, -1]])
X, y = data[:, :-1], data[:, -1]
y = np.array([1 if i == 1. else -1 for i in y])
定义模型1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19class Model():
def __init__(self):
self.w = np.ones(len(data[0])-1, dtype=np.float32)
self.b = 0
self.l_rate = 0.1
def linear_func(self, w, b, x):
return np.dot(x, w) + b
def fit(self, X_train, y_train):
finished = False
while not finished:
wrong_count = 0
for d in range(len(X_train)):
if y_train[d] * self.linear_func(self.w, self.b, X_train[d]) <= 0:
self.w = self.w + self.l_rate * y_train[d] * X_train[d]
self.b = self.b + self.l_rate * y_train[d]
wrong_count += 1
if wrong_count == 0:
finished = True
return "perceptron model"
1 | perceptron = Model() |
[ 7.8 -10. ]
-12.099999999999973
使用自己训练的模型做二分类1
2
3
4
5
6
7x_points = np.linspace(4, 7, 10)
y_ = -(perceptron.w[0] * x_points + perceptron.b) / perceptron.w[1]
plt.plot(x_points, y_)
plt.plot(data[:50, 0], data[:50, 1], 'bo', color='blue', label='0')
plt.plot(data[50:100, 0], data[50:100, 1], 'bo', color='orange', label='1')
plt.legend()
<matplotlib.legend.Legend at 0x1b17ecb4d68>
使用sklearn中自带的感知机模型1
2
3
4
5
6from sklearn.linear_model import Perceptron
clf = Perceptron(fit_intercept=False, max_iter=1000, shuffle=False)
clf.fit(X, y)
print(clf.coef_)
print(clf.intercept_)
[[ 74.6 -127.2]]
[0.]
c:\python36\lib\site-packages\sklearn\linear_model\stochastic_gradient.py:183: FutureWarning: max_iter and tol parameters have been added in Perceptron in 0.19. If max_iter is set but tol is left unset, the default value for tol in 0.19 and 0.20 will be None (which is equivalent to -infinity, so it has no effect) but will change in 0.21 to 1e-3. Specify tol to silence this warning.
FutureWarning)
绘制sklearn训练得到的感知机模型1
2
3
4
5
6
7x_points = np.arange(4, 8)
y_ = -(clf.coef_[0][0] * x_points + clf.intercept_) / clf.coef_[0][1]
plt.plot(x_points, y_)
plt.plot(data[:50, 0], data[:50, 1], 'bo', color='blue', label='0')
plt.plot(data[50:100, 0], data[50:100, 1], 'bo', color='orange', label='1')
plt.legend()
<matplotlib.legend.Legend at 0x1b17f046278>
C:\Python36\Lib\site-packages\sklearn\datasets\data