%matplotlib inline
import matplotlib.pyplot as plt
import scipy.misc
w = scipy.misc.imread("HendrixCollegeMainEntrance.PNG")
w
array([[[255, 255, 151], [255, 255, 158], [212, 219, 123], ..., [ 53, 88, 45], [ 82, 119, 78], [123, 161, 124]], [[253, 255, 165], [224, 227, 140], [176, 181, 101], ..., [147, 180, 135], [137, 172, 130], [121, 157, 119]], [[108, 108, 46], [ 67, 67, 7], [ 46, 49, 0], ..., [193, 221, 172], [165, 197, 150], [128, 159, 117]], ..., [[ 51, 53, 6], [ 62, 61, 15], [ 65, 64, 16], ..., [ 96, 71, 51], [105, 79, 62], [126, 99, 82]], [[ 51, 53, 4], [ 62, 61, 13], [ 66, 66, 14], ..., [144, 114, 88], [149, 114, 92], [151, 115, 93]], [[ 51, 53, 3], [ 62, 62, 12], [ 66, 66, 14], ..., [216, 182, 155], [198, 161, 135], [165, 125, 100]]], dtype=uint8)
z = w.reshape((-1, 3))
z
array([[255, 255, 151], [255, 255, 158], [212, 219, 123], ..., [216, 182, 155], [198, 161, 135], [165, 125, 100]], dtype=uint8)
from scipy.cluster.vq import kmeans, vq
centroids, distortion = kmeans(z, 3)
centroids
array([[213, 216, 147], [114, 106, 62], [ 37, 40, 15]], dtype=uint8)
idx, _ = vq(z, centroids)
idx
array([0, 0, 0, ..., 0, 0, 1])
res = centroids[idx]
res
array([[213, 216, 147], [213, 216, 147], [213, 216, 147], ..., [213, 216, 147], [213, 216, 147], [114, 106, 62]], dtype=uint8)
res2 = res.reshape((w.shape))
res2
array([[[213, 216, 147], [213, 216, 147], [213, 216, 147], ..., [ 37, 40, 15], [114, 106, 62], [114, 106, 62]], [[213, 216, 147], [213, 216, 147], [213, 216, 147], ..., [213, 216, 147], [213, 216, 147], [114, 106, 62]], [[114, 106, 62], [ 37, 40, 15], [ 37, 40, 15], ..., [213, 216, 147], [213, 216, 147], [114, 106, 62]], ..., [[ 37, 40, 15], [ 37, 40, 15], [ 37, 40, 15], ..., [114, 106, 62], [114, 106, 62], [114, 106, 62]], [[ 37, 40, 15], [ 37, 40, 15], [ 37, 40, 15], ..., [114, 106, 62], [114, 106, 62], [114, 106, 62]], [[ 37, 40, 15], [ 37, 40, 15], [ 37, 40, 15], ..., [213, 216, 147], [213, 216, 147], [114, 106, 62]]], dtype=uint8)
scipy.misc.imsave("res2.png", res2)
import numpy as np
d = []
fin = open("FEV.csv")
fin.readline()
for line in fin.readlines():
d.append([float(x) for x in line.split(",")[1:4]])
fin.close()
data = np.array(d)
data
array([[ 9. , 1.708, 57. ], [ 8. , 1.724, 67.5 ], [ 7. , 1.72 , 54.5 ], ..., [ 18. , 2.853, 60. ], [ 16. , 2.795, 63. ], [ 15. , 3.211, 66.5 ]])
plt.plot(data[:,0], data[:,1], "o")
[<matplotlib.lines.Line2D at 0x1167df290>]
plt.plot(data[:,0], data[:,2], "o")
[<matplotlib.lines.Line2D at 0x116895b10>]
plt.plot(data[:,2], data[:,1], "o")
[<matplotlib.lines.Line2D at 0x1088304d0>]
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plt.plot(m.Y[:,0], m.Y[:,1], m.Y[:,2], "o")
[<mpl_toolkits.mplot3d.art3d.Line3D at 0x116859090>]
from matplotlib.mlab import PCA
m = PCA(data)
dir(m)
['Wt', 'Y', '__doc__', '__init__', '__module__', '_get_colinear', 'a', 'center', 'fracs', 'mu', 'numcols', 'numrows', 'project', 'sigma']
m.a
array([[-0.3154793 , -1.07200387, -0.72705184], [-0.65426988, -1.05353656, 1.11532795], [-0.99306045, -1.05815339, -1.1657137 ], ..., [ 2.7336359 , 0.24956278, -0.20065762], [ 2.05605474, 0.18261879, 0.32573661], [ 1.71726416, 0.66276877, 0.93986321]])
plt.plot(m.a[:,0], m.a[:,1], "o")
[<matplotlib.lines.Line2D at 0x117ab8e90>]
m.Wt
array([[ 0.56192915, 0.58087456, 0.58891458], [ 0.81597991, -0.50604714, -0.27945138], [-0.13569234, -0.63757434, 0.75834461]])
m.fracs
array([ 0.87064597, 0.08654881, 0.04280522])
m.Y
array([[-1.22814821, 0.48823537, 0.17493444], [-0.32279301, -0.31241185, 1.60629023], [-1.85918979, 0.05091842, -0.07461055], ..., [ 1.56290418, 2.16037549, -0.68221589], [ 1.45326675, 1.4942581 , -0.14840333], [ 1.90346546, 0.80321474, 0.05715624]])
plt.plot(m.Y[:,0], m.Y[:,1], "o")
[<matplotlib.lines.Line2D at 0x11d87cf50>]
m.project([8, 4, 60])
array([ 0.42814649, -1.27403075, -1.06657181])
from scipy.cluster.vq import kmeans, vq
centroids, distortion = kmeans(data, 3)
idx, _ = vq(data, centroids)
plt.plot(m.Y[idx==0,0], m.Y[idx==0,1], "o")
plt.plot(m.Y[idx==1,0], m.Y[idx==1,1], "o")
plt.plot(m.Y[idx==2,0], m.Y[idx==2,1], "o")
[<matplotlib.lines.Line2D at 0x11d9adad0>]