# -*- coding: utf-8 -*-
"""
Created on Thu Feb 20 15:49:38 2020
@author: 29033
"""
#参考代码https://blog.csdn.net/A993852/article/details/80099258
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_excel('E:/01machineLearning/周志华/西瓜数据集3a.xlsx')
X0 = df.iloc[:8,1:3]
X1 = df.iloc[8:,1:3]
X0 = X0.values
X1 = X1.values
#求正反例均值
miu0 = np.mean(X0, axis=0).reshape((-1, 1))
miu1 = np.mean(X1, axis=0).reshape((-1, 1))
#求协方差
cov0 = np.cov(X0, rowvar=False)
cov1 = np.cov(X1, rowvar=False)
#求出w
S_w = np.mat(cov0 + cov1)
Omiga = S_w.I * (miu0 - miu1)
#画出点、直线
plt.scatter(X0[:, 0], X0[:, 1], c='b', label='+', marker = '+')
plt.scatter(X1[:, 0], X1[:, 1], c='r', label='-', marker = '_')
plt.plot([0, 1], [0, -Omiga[0] / Omiga[1]], label='y')
plt.xlabel('密度', fontproperties='SimHei', fontsize=15, color='green');
plt.ylabel('含糖率', fontproperties='SimHei', fontsize=15, color='green');
plt.title(r'3.5 线性判别分析', fontproperties='SimHei', fontsize=25);
plt.legend()
plt.show()
DATAFRAME真的是一个很有意思的type,总结下用法就是
提取行列可以用.iloc[xxx];操作数据用.value(๑•̀ㅂ•́)و✧
来源:CSDN
作者:shianlin2084
链接:https://blog.csdn.net/weixin_43759518/article/details/104413817