PCA降维

先进行均值归一化
##############################################################
df = pd.read_csv(r'C:\Users\zhoutao\Desktop\ok.csv')
df1 = df[df['a'].astype(int)+df['b'].astype(int)>10000]
df1.drop('target',axis=1, inplace=True)
df1 = df1.values
#axis = 0:压缩行,对各列求均值a
mean = np.mean(df1,axis=0)
max = np.max(df1,axis=0)
#均值归一化
df1 = (df1-mean)/max
#归一化后的正样本
pd.DataFrame(df1).to_csv(r'C:\Users\zhoutao\Desktop\ko.csv')
#######################################################
#coding:utf-8
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import numpy as np
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D  #三维绘图
#读取数据
#去掉列名和行索引读取
df1 = pd.read_csv(r'C:\Users\zhoutao\Desktop\11.csv',index_col=False,header=0)
df1=df1.fillna(0).values
df0 = pd.read_csv(r'C:\Users\zhoutao\Desktop\00.csv',index_col=False,header=0)
df0=df0.fillna(0).values
instances = np.array(df0)
pca = PCA(n_components=2).fit(instances)
pca_2d = pca.transform(instances)
# fig = plt.figure(figsize=(4,4))
plt.rcParams['font.sans-serif']=['SimHei']
# plt.axis([-0.2e+12,0.2e+12,-0.2e+11,0.2e+11])
fig=plt.figure()
axes = plt.subplot()
# ax = Axes3D(fig)
# ax.scatter(pca_2d[:,0],pca_2d[:,1],pca_2d[:,2],c='#00CED1',norm=0.5)
label1=plt.scatter(pca_2d[:,0],pca_2d[:,1],c='#00CED1',norm=0.8)
##########################
instances1 = np.array(df1)
pca1 = PCA(n_components=2).fit(instances1)

pca_2d1 = pca1.transform(instances1)

# ax.scatter(pca_2d1[:,0],pca_2d1[:,1],pca_2d1[:,2],c='#DC143C',norm=0.5)
label2=plt.scatter(pca_2d1[:,0],pca_2d1[:,1],c='#DC143C',norm=0.8)
axes.legend((label1, label2), ("负样本", "正样本"), loc=2)
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值