Created
September 13, 2018 08:28
-
-
Save elnikkis/5e31e15f096bcb54ec9ae07c7901cceb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
''' | |
Label Propagationを実装した | |
''' | |
import numpy as np | |
import scipy as sp | |
import sklearn.datasets | |
import sklearn.metrics as metrics | |
from sklearn.model_selection import train_test_split | |
from sklearn.decomposition import PCA | |
from sklearn.svm import SVC | |
from sklearn.preprocessing import StandardScaler, MinMaxScaler | |
class LabelPropagation(): | |
''' | |
Reference: | |
Zhu, Xiaojin, Ghahramani, Zoubin. Learning from Labeled and Unlabeled Data with Label Propagation. 2002. | |
''' | |
def __init__(self, sigma=0.22): | |
self.sigma = sigma | |
def fit(self, X, y): | |
self.X = X | |
self.y = y | |
def predict(self, X): | |
data = np.concatenate((self.X, X), axis=0) | |
dist_vec = sp.spatial.distance.pdist(data) | |
dist_mat = sp.spatial.distance.squareform(dist_vec) | |
weight = np.exp(- (np.multiply(dist_mat, dist_mat) / (self.sigma**2))) | |
row_sum = np.sum(weight, axis=1) | |
T = weight / row_sum | |
print(T) | |
self.dist = dist_mat | |
self.weight = weight | |
self.transition = T | |
C = len(np.unique(self.y)) | |
print(C) | |
Y_L = np.eye(C)[self.y] | |
Y_U = np.zeros((X.shape[0], C)) | |
Y = np.concatenate((Y_L, Y_U), axis=0) | |
Y_hat = Y | |
for i in range(10): | |
Y_hat = np.dot(T, Y_hat) | |
u = np.argmax(Y_hat, axis=1) | |
return u[self.y.shape[0]:] | |
def score(self, X, y): | |
pass | |
if __name__ == '__main__': | |
data = sklearn.datasets.load_iris() | |
#data = sklearn.datasets.load_digits() | |
print(data.data.shape) | |
X = data.data | |
y = data.target | |
X_train, X_test, y_train, y_test = train_test_split( | |
X, y, test_size=0.25, random_state=52) | |
# preprocess | |
#transformer = StandardScaler() | |
transformer = MinMaxScaler() | |
X_train = transformer.fit_transform(X_train) | |
X_test = transformer.transform(X_test) | |
print('SVM') | |
clf = SVC() | |
clf.fit(X_train, y_train) | |
predicted = clf.predict(X_test) | |
print(metrics.accuracy_score(y_test, predicted)) | |
print('LP') | |
clf = LabelPropagation() | |
clf.fit(X_train, y_train) | |
predicted = clf.predict(X_test) | |
np.set_printoptions(threshold=np.inf) | |
print(predicted) | |
print(predicted.shape) | |
print(metrics.accuracy_score(y_test, predicted)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment