In [1]:
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import numpy as np
from sklearn.preprocessing import scale
from scipy.linalg import svd
In [2]:
# load data
data = load_iris()
x = data['data']
y = data['target']
In [4]:
# scale the x by mean of x
x_s =scale(x,with_mean=True,with_std=False,axis=0)
In [5]:
# svd技术分解
U,S,V = svd(x_s,full_matrices=False)
In [6]:
# top 2
x_t = U[:,:2]
In [7]:
# plot the figure
plt.close('all')
plt.figure(1)
plt.scatter(x_t[:,0],x_t[:,1],c=y)
plt.xlabel('componet 1')
plt.ylabel('componet 2')
plt.show()