栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【李航统计学习方法】—— 感知机(Perceptron)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【李航统计学习方法】—— 感知机(Perceptron)

理论 实践
from sklearn.datasets import load_iris
import pandas as pd 
import numpy as np
from sklearn.linear_model import Perceptron
import matplotlib.pyplot as plt
# 加载数据
iris = load_iris()
df = pd.Dataframe(iris.data, columns = iris.feature_names)
df['label'] = pd.Dataframe(iris.target)
df.head()
sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)label
05.13.51.40.20
14.93.01.40.20
24.73.21.30.20
34.63.11.50.20
45.03.61.40.20
# 选择两个特征进行二分类 可视化数据
plt.scatter(df[:50]['sepal length (cm)'], df[:50]['sepal width (cm)'], label='0')
plt.scatter(df[50:100]['sepal length (cm)'], df[50:100]['sepal width (cm)'], label='1')
plt.xlabel('sepal length (cm)')
plt.ylabel('sepal width (cm)')
plt.legend()

# 训练感知机模型
model = Perceptron(fit_intercept=True, 
                 max_iter=1000,
                 tol=None,
                 shuffle=True)
model.fit(df.iloc[:100, [0,1]], df[0:100]['label'])
Perceptron(tol=None)
print(model.coef_)
print(model.intercept_)
[[ 70.7 -87.9]]
[-117.]
# 可视化
plt.scatter(df[:50]['sepal length (cm)'], df[:50]['sepal width (cm)'], label='0')
plt.scatter(df[50:100]['sepal length (cm)'], df[50:100]['sepal width (cm)'], label='1')
plt.xlabel('sepal length (cm)')
plt.ylabel('sepal width (cm)')
x_points = np.arange(4, 8)
# coef_ : w; intercept_: b
y_ = -(model.coef_[0][0]*x_points + model.intercept_)/model.coef_[0][1]
plt.plot(x_ponits, y_)
plt.legend()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/290699.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号