Logo Studenta

MCOM2_U2_contenido - Pilar Carrasco

Esta es una vista previa del archivo. Inicie sesión para ver el archivo original

Índice 
 
 
 
 
 
 
 
Presentación 
Desarrollo de temas 
𝒜
(𝑥𝑖 , 𝑦𝑖)
𝑓(𝑥𝑖) = 𝑦𝑖 𝑓
𝑓′
𝑓′
 
 
𝒜 𝐸 𝒜
𝐸
𝒜
𝒜 𝐸
K-Medios 
 
 𝑋 𝑛 𝑚 𝑋
𝑚
 𝐶
𝑐 ∈ 𝐶
𝐶 = {𝜇|𝜇 ∈ 𝑅𝑚} ; #𝐶 = 𝑘
 𝑘 
𝑆1, 𝑆2, … , 𝑆𝑘 𝑡. 𝑞. 𝑆𝑖 ← []
 𝑥𝑗 ∈ 𝑋
𝜇𝑖
𝑥𝑗
 
𝑆𝑖
𝑆𝑖
𝜇𝑖 =
1
|𝑆𝑖|
 ∑ 𝑥𝑗
𝑥𝑗∈𝑆𝑖
 
 
 
 
# -*- coding:utf8 -*- 
import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
 
 
from sklearn.cluster import KMeans 
from sklearn import datasets 
 
np.random.seed(5) 
 
centroides = [[1, 1], [-1, -1], [1, -1]] 
iris = datasets.load_iris() 
X = iris.data 
y = iris.target 
 
 
estimadores = {'k_means_iris_3': KMeans(n_clusters=3), 
 'k_means_iris_8': KMeans(n_clusters=8), 
 'k_means_iris_bad_init': KMeans(n_clusters=3, n_init=1, 
init='random')} 
 
 
num = 1 
for nombre_estimador, est in estimatores.items(): 
 fig = plt.figure(num, figsize=(4, 3)) 
 plt.clf() 
 ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134) 
 
 plt.cla() 
 est.fit(X) #Scikit entrena en este paso el algoritmo. 
 centroides=str(est.n_clusters) 
 labels = est.labels_ 
 
 ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=labels.astype(np.float)) 
 
 ax.w_xaxis.set_ticklabels([]) 
 ax.w_yaxis.set_ticklabels([]) 
 ax.w_zaxis.set_ticklabels([]) 
 ax.set_xlabel(u'Ancho de pétalo') 
 ax.set_ylabel(u'Ancho de sépalo') 
 ax.set_zlabel(u'Largo de pétalo') 
 ax.set_title('Centroides :'+centroides) 
 num = num + 1 
 
plt.show() 
 
𝑥
𝑥
 
𝑝𝑢𝑟𝑒𝑧𝑎(Ω, 𝐶) =
1
𝑁
∑ max
𝑗
|𝜔𝑘⋂𝑐𝑗|
𝑘
𝑁
𝑘 ≈ √
𝑁
2
PCA (Análisis por componentes principales) 
 
 𝐷𝑚×𝑛 𝑚
ℝ𝑛
 
𝐷′
 𝐷′
Σ = 𝑐𝑜𝑣(𝐷′)
 Σ 𝑒𝑖𝑔(Σ) = {Σ1, Σ2, … , Σ𝑛}
𝑈 = {𝑢1, 𝑢2, … , 𝑢𝑛}
𝑈𝑇𝑈 = 𝑈𝑈𝑇 = 𝐼
𝑥 ∈ 𝐷
𝑥 = 𝑈𝑥′
 
from numpy import dot, cov 
import numpy.linalg as la 
def pca(datos, dimens=2): 
 m, n = datos.shape 
 D = datos[:]; D -= datos.mean(axis=0) 
 R = cov(D, rowvar=False) 
 evals, evecs = la.eigh(R) 
 idx = argsort(evals)[::-1] 
 evecs = evecs[:,idx] 
 evals = evals[idx] 
 evecs = evecs[:, :dimens] 
 return dot(evecs.T, D.T).T, evals, evecs 
cov, argsort dot
lingal numpy. 
M = loadtxt('iris.data',delimiter=',') 
N,_,_ = pca(M, 2) 
 
plt.scatter(N[:,0],N[:,1], c=M[:,4]) 
plt.title('PCA para Iris con dos componentes') 
plt.xlabel('Componente 1') 
plt.ylabel('Componente 2') 
plt.show() 
 
SVM (Maquinas de soporte vectorial) 
𝒳 ↦ 𝒴
𝑥𝑖 ∈ 𝒳 𝑦𝑖 ∈ 𝒴
𝛾𝑖 = 𝑦𝑖(𝑤
𝑇𝑥 + 𝑏)
𝑦𝑖 ∈ {−1,1}
𝑦𝑖(𝑤
𝑇𝑥 + 𝑏) > 0
𝑤 → 2𝑤 𝑏 → 2𝑏
𝛾 = min
𝑖=1…𝑚
𝛾𝑖
𝑥𝑖 − 𝛾𝑖 ∙
𝑤
‖𝑤‖
 
𝑤𝑇 (𝑥𝑖 − 𝑦𝑖 ∙
𝑤
‖𝑤‖
) + 𝑏 = 0
𝛾𝑖
𝛾𝑖 =
𝑤𝑇𝑥𝑖 + 𝑏
‖𝑤‖
= (
𝑤
‖𝑤‖
)
𝑇
𝑥𝑖 +
𝑏
‖𝑤‖
 𝛾𝑖 = 𝑦𝑖 ((
𝑤
‖𝑤‖
)
𝑇
𝑥𝑖 +
𝑏
‖𝑤‖
) 
 
# -*- coding: cp1252 -*- 
import numpy as np 
import matplotlib.pyplot as plt 
from sklearn import svm, datasets 
 
 
iris = datasets.load_iris() 
X = iris.data[:, :2] 
y = iris.target 
 
#parámetro de regularización 
C = 1.0 
 
svc = svm.SVC(kernel='linear', C=C).fit(X, y) 
rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C).fit(X, y) 
poly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X, y) 
lin_svc = svm.LinearSVC(C=C).fit(X, y) 
 
 
 
#graficado 
h = .02 
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), 
 np.arange(y_min, y_max, h)) 
 
titulos = ['SVC - kernel lineal', 
 'LinearSVC lineal', 
 'SVC kernel RBF', 
 u'SVC kernel cúbico'] 
 
for i, clf in enumerate((svc, lin_svc, rbf_svc, poly_svc)): 
 plt.subplot(2, 2, i + 1) 
 plt.subplots_adjust(wspace=0.4, hspace=0.4) 
 Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) 
 Z = Z.reshape(xx.shape) 
 
 plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8) 
 
 plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired) 
 plt.xlabel('Sepal length') 
 plt.ylabel('Sepal width') 
 plt.xlim(xx.min(), xx.max()) 
 plt.ylim(yy.min(), yy.max()) 
 plt.xticks(()) 
 plt.yticks(()) 
 plt.title(titulos[i]) 
 
plt.show() 
 
Cierre de la unidad 
Fuentes de consulta 
 
 
 
 
 
 
 
https://es.wikipedia.org/w/index.php?title=Iris_flor_conjunto_de_datos&oldid=87366278
https://es.wikipedia.org/w/index.php?title=Iris_flor_conjunto_de_datos&oldid=87366278
http://stanford.edu/~cpiech/cs221/handouts/kmeans.html
http://ufldl.stanford.edu/wiki/index.php/UFLDL_Tutorial
https://www.youtube.com/watch?v=_PwhiWxHK8o
http://cs229.stanford.edu/notes/cs229-notes3.pdf
http://www.kernel-machines.org/

Continuar navegando