sklearn降维1: 线性判别分析LDA原理python过程

import pandas as pd

#df = pd.read_csv('iris.data', header=None, sep=',')
df = pd.io.parsers.read_csv(filepath_or_buffer='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',header=None,sep=',')

feature_dict = {
   i:label for i,label in zip(range(4), ('sepal length', 'sepal width','petal lenth', 'petal width'))}
df.columns = [l for i,l in sorted(feature_dict.items())] + ['class label']
df.dropna(how = 'all', inplace = True)
df.head()

在这里插入图片描述

一. 求三种鸢尾花数据在不同特征维度上的均值向量

在这里插入图片描述
在这里插入图片描述

X = df[['sepal length','sepal width', 'petal lenth', 'petal width']].values
y = df['class label'].values

# 将标签编码
from sklearn.preprocessing import LabelEncoder
enc = LabelEncoder()
label_encoder = enc.fit(y)
y = label_encoder.transform(y) + 1
label_dict = {
   index+1:label for index,label in enumerate(enc.classes_)}
# label_dict = {1: 'Iris-setosa', 2: 'Iris-versicolor', 3: 'Iris-virginica'}

import numpy as np
np.set_printoptions(precision = 4)

# 分别求三种鸢尾花数据在不同特征维度上的均值向量
mean_vectors = []
for cl in range(1,4):
    mean_vectors.append(np.mean(X[y==cl], axis=0))
    print(<
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值