Python数据分析与机器学习-PCA主成分分析
2018-01-10 10:34
477 查看
源码下载:
http://download.csdn.net/download/adam_zs/10197871
import numpy as np
import pandas as pd
df = pd.read_csv("iris.data", header=None)
df.columns = ['sepal_len', 'sepal_wid', 'petal_len', 'petal_wid', 'class']
X = df.ix[:, :4].values
y = df.ix[:, 4].values
import matplotlib.pyplot as plt
import math
label_dict = {1: 'Iris-Setosa',
2: 'Iris-Versicolor',
3: 'Iris-Virgnica'}
feature_dict = {0: 'sepal length [cm]',
1: 'sepal width [cm]',
2: 'petal length [cm]',
3: 'petal width [cm]'}
for cnt in range(4):
plt.subplot(2, 2, cnt + 1)
for lab in ('Iris-setosa', 'Iris-versicolor', 'Iris-virginica'):
plt.hist(X[y == lab, cnt], label=lab, bins=10, alpha=0.3)
plt.xlabel(feature_dict[cnt])
plt.legend(fontsize=8)
plt.tight_layout()
# plt.show()
from sklearn.preprocessing import StandardScaler
X_std = StandardScaler().fit_transform(X)
mean_vec = np.mean(X_std, axis=0)
cov_mat = (X_std - mean_vec).T.dot((X_std - mean_vec)) / (X_std.shape[0] - 1)
print('Covariance matrix \n%s' % cov_mat) # Covariance matrix 协方差矩阵
print('NumPy covariance matrix: \n%s' % np.cov(X_std.T))
cov_mat = np.cov(X_std.T)
eig_vals, eig_vecs = np.linalg.eig(cov_mat)
print('Eigenvectors \n%s' % eig_vecs) # 协方差特征向量
print('\nEigenvalues \n%s' % eig_vals) # 协方差特征值
# Make a list of (eigenvalue, eigenvector) tuples
eig_pairs = [(np.abs(eig_vals[i]), eig_vecs[:, i]) for i in range(len(eig_vals))]
print(eig_pairs)
print('----------')
# Sort the (eigenvalue, eigenvector) tuples from high to low
eig_pairs.sort(key=lambda x: x[0], reverse=True)
# Visually confirm that the list is correctly sorted by decreasing eigenvalues
print('Eigenvalues in descending order:')
for i in eig_pairs:
print(i[0])
tot = sum(eig_vals)
var_exp = [(i / tot) * 100 for i in sorted(eig_vals, reverse=True)]
print(var_exp)
cum_var_exp = np.cumsum(var_exp) # 每个值和前面的值加在一起
cum_var_exp
a = np.array([1, 2, 3, 4])
print(a)
print('-----------')
print(np.cumsum(a))
plt.figure(figsize=(6, 4))
plt.bar(range(4), var_exp, alpha=0.5, align='center',
label='individual explained variance')
plt.step(range(4), cum_var_exp, where='mid',
label='cumulative explained variance')
plt.ylabel('Explained variance ratio')
plt.xlabel('Principal components')
plt.legend(loc='best')
plt.tight_layout()
plt.show()
matrix_w = np.hstack((eig_pairs[0][1].reshape(4, 1),
eig_pairs[1][1].reshape(4, 1)))
print('Matrix W:\n', matrix_w)
Y = X_std.dot(matrix_w)
plt.figure(figsize=(6, 4))
for lab, col in zip(('Iris-setosa', 'Iris-versicolor', 'Iris-virginica'),
('blue', 'red', 'green')):
plt.scatter(X[y == lab, 0],
X[y == lab, 1],
label=lab,
c=col)
plt.xlabel('sepal_len')
plt.ylabel('sepal_wid')
plt.legend(loc='best')
plt.tight_layout()
plt.show()
plt.figure(figsize=(6, 4))
for lab, col in zip(('Iris-setosa', 'Iris-versicolor', 'Iris-virginica'),
('blue', 'red', 'green')):
plt.scatter(Y[y == lab, 0],
Y[y == lab, 1],
label=lab,
c=col)
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.legend(loc='lower center')
plt.tight_layout()
plt.show()
http://download.csdn.net/download/adam_zs/10197871
import numpy as np
import pandas as pd
df = pd.read_csv("iris.data", header=None)
df.columns = ['sepal_len', 'sepal_wid', 'petal_len', 'petal_wid', 'class']
X = df.ix[:, :4].values
y = df.ix[:, 4].values
import matplotlib.pyplot as plt
import math
label_dict = {1: 'Iris-Setosa',
2: 'Iris-Versicolor',
3: 'Iris-Virgnica'}
feature_dict = {0: 'sepal length [cm]',
1: 'sepal width [cm]',
2: 'petal length [cm]',
3: 'petal width [cm]'}
for cnt in range(4):
plt.subplot(2, 2, cnt + 1)
for lab in ('Iris-setosa', 'Iris-versicolor', 'Iris-virginica'):
plt.hist(X[y == lab, cnt], label=lab, bins=10, alpha=0.3)
plt.xlabel(feature_dict[cnt])
plt.legend(fontsize=8)
plt.tight_layout()
# plt.show()
from sklearn.preprocessing import StandardScaler
X_std = StandardScaler().fit_transform(X)
mean_vec = np.mean(X_std, axis=0)
cov_mat = (X_std - mean_vec).T.dot((X_std - mean_vec)) / (X_std.shape[0] - 1)
print('Covariance matrix \n%s' % cov_mat) # Covariance matrix 协方差矩阵
print('NumPy covariance matrix: \n%s' % np.cov(X_std.T))
cov_mat = np.cov(X_std.T)
eig_vals, eig_vecs = np.linalg.eig(cov_mat)
print('Eigenvectors \n%s' % eig_vecs) # 协方差特征向量
print('\nEigenvalues \n%s' % eig_vals) # 协方差特征值
# Make a list of (eigenvalue, eigenvector) tuples
eig_pairs = [(np.abs(eig_vals[i]), eig_vecs[:, i]) for i in range(len(eig_vals))]
print(eig_pairs)
print('----------')
# Sort the (eigenvalue, eigenvector) tuples from high to low
eig_pairs.sort(key=lambda x: x[0], reverse=True)
# Visually confirm that the list is correctly sorted by decreasing eigenvalues
print('Eigenvalues in descending order:')
for i in eig_pairs:
print(i[0])
tot = sum(eig_vals)
var_exp = [(i / tot) * 100 for i in sorted(eig_vals, reverse=True)]
print(var_exp)
cum_var_exp = np.cumsum(var_exp) # 每个值和前面的值加在一起
cum_var_exp
a = np.array([1, 2, 3, 4])
print(a)
print('-----------')
print(np.cumsum(a))
plt.figure(figsize=(6, 4))
plt.bar(range(4), var_exp, alpha=0.5, align='center',
label='individual explained variance')
plt.step(range(4), cum_var_exp, where='mid',
label='cumulative explained variance')
plt.ylabel('Explained variance ratio')
plt.xlabel('Principal components')
plt.legend(loc='best')
plt.tight_layout()
plt.show()
matrix_w = np.hstack((eig_pairs[0][1].reshape(4, 1),
eig_pairs[1][1].reshape(4, 1)))
print('Matrix W:\n', matrix_w)
Y = X_std.dot(matrix_w)
plt.figure(figsize=(6, 4))
for lab, col in zip(('Iris-setosa', 'Iris-versicolor', 'Iris-virginica'),
('blue', 'red', 'green')):
plt.scatter(X[y == lab, 0],
X[y == lab, 1],
label=lab,
c=col)
plt.xlabel('sepal_len')
plt.ylabel('sepal_wid')
plt.legend(loc='best')
plt.tight_layout()
plt.show()
plt.figure(figsize=(6, 4))
for lab, col in zip(('Iris-setosa', 'Iris-versicolor', 'Iris-virginica'),
('blue', 'red', 'green')):
plt.scatter(Y[y == lab, 0],
Y[y == lab, 1],
label=lab,
c=col)
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.legend(loc='lower center')
plt.tight_layout()
plt.show()
相关文章推荐
- Python数据分析与机器学习-神经网络
- Python数据分析与机器学习-线性回归算法原理推导
- Python数据分析与机器学习-交易数据异常检测
- Python数据分析与机器学习-scikit-learn模型建立与评估
- Python数据分析与机器学习-使用sklearn构造决策树模型
- Python数据分析与机器学习-Pandas
- Python数据分析与机器学习-新闻分类任务
- python数据分析(预测性分析与机器学习)
- Python数据分析与机器学习-聚类实践
- Python数据分析与机器学习-Seaborn
- Python数据分析与机器学习-使用Kmeans进行图像压缩
- 关于 Python 数据抓取 & 分析 & 机器学习 & 挖掘 & 神经网络 内容的分享。
- Python数据分析与机器学习-逻辑回归算法
- Python数据分析与机器学习-Python时间序列分析
- Python数据分析与机器学习-Numpy
- 500G python web、爬虫、数据分析、机器学习、大数据、前端实战项目视频代码免费分享
- python数据分析与机器学习-用户流失预警
- 关于 Python 数据抓取、分析、挖掘、机器学习和Python 分布式计算内容分享
- Python数据分析与机器学习-足球赛事数据集
- Python数据分析与机器学习-贝叶斯实现拼写检查器