《机器学习与数据挖掘》-实验四 鸢尾花决策边界问题

利用sklearn,导入鸢尾花数据,对鸢尾花数据进行决策边界问题。

目录

 1.导入包

2.进行数据导入与处理 

3. 进行决策边界的处理

4.根据数据,画出决策边界

 


 

 1.导入包

import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt
import matplotlib as mpl
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_iris

2.进行数据导入与处理 

def loaddata():
    data = np.loadtxt('这里写自己数据的路径', delimiter=',')
    n = data.shape[1] - 1
    X = data[:, 0:n]
    y = data[:, -1].reshape(-1, 1)
    return X, y

X, y = loaddata()
model = linear_model.LogisticRegression(C=50, max_iter=2000)
model.fit(X, y)
# print(model.coef_)
# print(model.intercept_)
y_hat = model.predict(X)


# print("准确度:",accuracy_score(y,y_hat))

3. 进行决策边界的处理

def plotDescisionBoundary(X, y, theta):
    cm_dark = mpl.colors.ListedColormap(['g', 'r'])
    plt.xlabel('Exam 1 score')
    plt.ylabel('Exam 2 score')
    plt.scatter(X[:, 0], X[:, 1], c=np.array(y).squeeze(), cmap=cm_dark, s=30)
    # 补充画决策边界代码;
    x1 = np.arange(min(X[:, 0]), max(X[:, 0]), 0.1)
    x2 = -(theta[1] * x1 + theta[0] / theta[2])
    plt.plot(x1, x2)
    plt.show()


theta = np.append(model.intercept_, model.coef_)
# plotDescisionBoundary(X,y,theta)

iris = load_iris()
# print(iris.DESCR)
X = iris.data
y = iris.target
# print(X[0:50,0])

feature = 1
feature_other = 2

plt.scatter(X[0:50, feature], X[0:50, feature_other], color="blue", marker='o')
plt.scatter(X[50:100, feature], X[50:100, feature_other], color="red", marker='x')
plt.scatter(X[100:, feature], X[100:, feature_other], color="green", marker='s')
plt.show()

X_2 = X[:, [feature, feature_other]]
model_2 = linear_model.LogisticRegression(C=100.0)
model_2.fit(X_2, y)

h = 0.2
x_min, x_max = X[:, feature].min() - .5, X[:, feature].max() + .5
y_min, y_max = X[:, feature_other].min() - .5, X[:, feature_other].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min,y_max,h))

np.c_[xx.ravel(), yy.ravel()]

z = model_2.predict(np.c_[xx.ravel(), yy.ravel()])
z = z.reshape(xx.shape)

4.根据数据,画出决策边界

plt.pcolormesh(xx, yy, z, cmap=plt.cm.Paired)
plt.scatter(X[0:50, feature], X[0:50, feature_other], color="blue", marker='o')
plt.scatter(X[50:100, feature], X[50:100, feature_other], color="red", marker='x')
plt.scatter(X[100:, feature], X[100:, feature_other], color="green", marker='s')
plt.xlabel('Sepal length', color='black', fontsize=15)
plt.ylabel('Sepal width', color='black', fontsize=15)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.legend(loc=2)
plt.show()
plt.title('决策分界', color='red', fontsize=20)

 PS:实验数据的不同,画出的决策边界也不同。 

总结 :

在机器学习中,鸢尾花模型是一个非常经典的模型,大家可以自行搜索其他的案例,对学习机器学习也有很大帮助哦!!!

 

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值