Thursday, January 16, 2020

Binary Classifiation

Binary Classification

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import tensorflow as tf
import keras
import numpy as np

from keras.datasets import imdb

(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)

#print("train_data[0] : ")
#print(train_data[0])

print(max([max(sequence) for sequence in train_data]))

word_index = imdb.get_word_index()
reverse_word_index = dict([value, key] for(key, value) in word_index.items())
decoded_review = ''.join([reverse_word_index.get(i-3, '?') for i in train_data[0]])
print(decoded_review)

def vectorize_sequences(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1.
    return results

x_train = vectorize_sequences(train_data)
x_test = vectorize_sequences(test_data)

print(x_train)

Thursday, November 10, 2016

Lucky Box


public class LuckyBox {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int N = 5;
        int M = 5;
        int[] values = {1, 2, 3, 1, 1};
        
        
        
    }

}

Eight Queens


public class EightQueens {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n = 8;
        enumerate(n);
    }
    
    public static void enumerate(int n){
        int[] a = new int[n];
        enumerate(a, 0);
    }
    
    public static void enumerate(int[] q, int k){
        int n = q.length;
        if(k == n)
            printQueens(q);
        else{
            for(int i=0; i                q[k] = i;
                if(isConsistent(q, k))
                    enumerate(q, k+1);
            }
        }
    }
    
    public static boolean isConsistent(int[] q, int n){
        for(int i=0; i            if(q[i] == q[n]) //same column
                return false;
            if((q[i] - q[n]) == (n-i)) //same major diagonal (upper left entry to lower right entry)
                return false;
            if((q[n] - q[i]) == (n-i)) //same minor diagonal (upper right entry to lower left entry)
                return false;
        }
        return true;
    }
    
    public static void printQueens(int[] q){
        int n = q.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (q[i] == j) 
                    System.out.print("Q ");
                else          
                    System.out.print("* ");
            }
            System.out.println();
        }  
        System.out.println();
    }

}

Valuntary


public class Valuntary {

    public static int maxCost = 0;
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n = 4;
        int input[][] = {{4,1,2,3},{3,1,2,4},{2,1,4,3},{3,1,2,4}};
        enumerate(n, input);
        System.out.println("Max Cost : "+maxCost);
    }
    
    public static void enumerate(int n, int[][] input){
        int[] a = new int[n];
        enumerate(a, 0, input);
    }
    
    public static int enumerate(int[] a, int k, int[][] input){
        int cost = 0;
        int n = a.length;
        if(k == n){
             cost = computeCost(a, input);
             if(maxCost < cost){
                 maxCost = cost;
             }
        }
        else{
            for(int i=0; i                a[k] = i;
                if(isConsistent(a, k)){
                    enumerate(a, k+1, input);
                }
            }
        }
        return cost;
        
    }
    
    public static boolean isConsistent(int[] a, int n){
        for(int i=0; i            if(a[i] == a[n])
                return false;
        }
        
        return true;
    }
    
    public static int computeCost(int[] a, int[][] input){
        int n = a.length;
        for(int i=0; i            for (int j = 0; j < n; j++) {
                if (a[i] == j) 
                    System.out.print("O ");
                else          
                    System.out.print("* ");
            }
            System.out.println();
        }
        int cost = 0;
        for(int i=0; i            cost += input[i][a[i]];
        }
        System.out.println("Cost : "+cost);
        System.out.println();
        return cost;
    }

}

BFS

public class NovFifth {

    public static void main(String args[]){
        int a = 10;
        int b = 100;
        
        String inputOperation1 = "*2";
        String inputOperation2 = "*3";
        String inputOperation3 = "+1";
        
        char[] operations1 = inputOperation1.toCharArray();
        char[] operations2 = inputOperation2.toCharArray();
        char[] operations3 = inputOperation3.toCharArray();
        
        char operator1 = operations1[0];
        int operand1 = Character.getNumericValue(operations1[1]);
        char operator2 = operations2[0];
        int operand2 = Character.getNumericValue(operations2[1]);
        char operator3 = operations3[0];
        int operand3 = Character.getNumericValue(operations3[1]);
        
        Operation[] operationArray = new Operation[3];
        Operation Operation1 = new Operation(operator1, operand1);
        Operation Operation2 = new Operation(operator2, operand2);
        Operation Operation3 = new Operation(operator3, operand3);
        operationArray[0] = Operation1;
        operationArray[1] = Operation2;
        operationArray[2] = Operation3;
        
        
        
        Node startPoint = new Node();
        Node start = new Node();
        start.level = 0;
        start.number = a;
        startPoint = start;
        
        Node endPoint = new Node();
        endPoint = start;
        
        Node currentNode = start;
        
        int minimunLength = 0;
        while(true){
            boolean answerFlag = false;
            int currentLevel = currentNode.level;
            
            for(int i=0; i<3; i++){
                Operation op = operationArray[i];
                int answer = calc(currentNode.number, op.operator, op.operand);
                if(answer == b){
                    answerFlag = true;
                    minimunLength = currentLevel + 1;
                }else if(answer < b){
                    //Push Queue
                    Node node  = new Node();
                    node.level = currentLevel  + 1;
                    node.number = answer;
                    endPoint.next = node;
                    endPoint = endPoint.next;
                }
            }
            
            if(answerFlag){
                break;
            }
            
            //Pop Queue
            currentNode = startPoint;
            startPoint = startPoint.next;
            
        }
        printQueue(startPoint);
        System.out.println("Answer : "+minimunLength);
        
    }
    
    public static int calc(int current, char operator, int operand){
        int returnNum = 0;
        if(operator == '*'){
            returnNum = current * operand;
        }else{
            returnNum = current + operand;
        }
        
        return returnNum;
    }
    
    public static void printQueue(Node node){
        int count = 0;
        while(true){
            //System.out.print(node.number+" ");
            if(node.next == null){
                break;
            }else{
                node = node.next;
                count++;
            }
        }
    }    
}

class Operation{
    public char operator;
    public int operand;
    public Operation(char operator, int operand){
        this.operator = operator;
        this.operand = operand;
    }
    
}

class Node{
    int number;
    int level;
    Node next;
}

Thursday, October 27, 2016

청첩장 무료 샘플 제공 사이트 소개

카드마켓 (http://www.card-market.co.kr/) 


- 다른 청첩장 제공 사이트가 배송비를 요구하는데 비해 무료로 샘플을 제공한다는 점에저 장점 (From&To, http://www.wfromnto.co.kr/ 의 경우 샘플 신청을 하였으나 2,500원 배송비 지불)

- 카드마켓이 From&To에비해 |1~20,000원 정도 비싸기는 하지만 청첩장 퀄리티나 완성도,추가 서비스(액자, 할인 쿠폰 제공, 추가 샘플 제공, 모바일 무료 청첩장 등) 등 다른 모든 면에서 월등히 앞선다고 판단됨.




카드마켓에서 무료로 제공된 샘플 청첩장

Thursday, October 20, 2016

DFS Example


public class DFSExample {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[][] conn = { { 0, 1, 0, 1, 0, 0, 0, 0, 1 }, // 0
                        { 0, 0, 0, 0, 0, 0, 0, 1, 0 }, // 1
                        { 0, 0, 0, 0, 0, 1, 0, 0, 0 }, // 2
                        { 0, 0, 1, 0, 1, 0, 0, 0, 0 }, // 3
                        { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 4
                        { 0, 0, 0, 0, 0, 0, 1, 0, 0 }, // 5
                        { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 6
                        { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 7
                        { 0, 0, 0, 0, 0, 0, 0, 0, 0 } };// 8
        
        
        boolean[] visited = new boolean[conn.length];
        int v = 0;
        
        DFS(conn, visited, v);
    }
    
    public static void DFS(int[][] conn, boolean[] visited, int v){
        visited[v] = true;
        System.out.println("nextNode : "+v);
        
        for(int i = 0; i < visited.length; i++){
            if(conn[v][i] > 0 && !visited[i]){
                DFS(conn, visited, i);
            }
        }
    }

}