#!/usr/bin/python
# coding=utf-8
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt # 加载matplotlib用于数据的可视化
from sklearn.decomposition import PCA # 加载PCA算法包
from sklearn.datasets import load_iris
# 画图支持中文显示
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
# 负号
plt.rcParams['axes.unicode_minus'] = False
# 显示所有列
pd.set_option('display.max_columns', None)
# 显示所有行
pd.set_option('display.max_rows', None)
# 设置value的显示长度为10000,默认为50
pd.set_option('display.width',10000)
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
#
np.set_printoptions(linewidth=1000)
data = load_iris()
y = data.target
x = data.data
pca = PCA(n_components=2) # 加载PCA算法,设置降维后主成分数目为2
reduced_x = pca.fit_transform(x) # 对样本进行降维
print('-------------------------------')
print(type(reduced_x))
print(reduced_x[0:5])
print(x[0:3])
red_x, red_y = [], []
blue_x, blue_y = [], []
green_x, green_y = [], []
for i in range(len(reduced_x)):
if y[i] == 0:
red_x.append(reduced_x[i][0])
red_y.append(reduced_x[i][1])
elif y[i] == 1:
blue_x.append(reduced_x[i][0])
blue_y.append(reduced_x[i][1])
else:
green_x.append(reduced_x[i][0])
green_y.append(reduced_x[i][1])
# 可视化
plt.figure()
plt.title('鸢尾花')
plt.scatter(red_x, red_y, c='r', marker='x')
plt.scatter(blue_x, blue_y, c='b', marker='o')
plt.scatter(green_x, green_y, c='g', marker='^')
plt.savefig('G:\\rnn\鸢尾花(使用pca进行降维).png')
plt.show()
网友评论