先进行均值归一化 ############################################################## 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()