<file_sep>import numpy as np

x = np.array([(1,2.,'Hello'), (2,3.,"world")],dtype = [('foo', 'i4'),('bar', 'f4'), ('baz', 'S10')])
print(x[1])
y = x['bar']
print(y)
y[:] = 2*y
print(y) #4,6
print(x) #(1,  4., b'Hello') (2,  6., b'world')
x[1] = (-1,-1., "Master")
print(x)

x = np.zeros(3, dtype='3int8, float32, (2,3)float64')
print(x)


<file_sep>import torch
import torchvision
import torchvision.datasets as dset
import torchvision.transforms as transforms
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

class LeNet(nn.Module):
    def __init__(self):
        super(LeNet, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.pool  = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1   = nn.Linear(4*4*16, 120)
        self.fc2   = nn.Linear(120, 84)
        self.fc3   = nn.Linear(84, 10)
        self.criterion = nn.CrossEntropyLoss()
    def forward(self, x,target):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        #print(x.size())
        x = x.view(-1, 4*4*16)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        loss = self.criterion(x,target)
        return x,loss
    def name(self):
        return 'LeNet-5'

transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.1307,), (0.3081,))])

batch_size = 128
kwargs = {'num_workers': 1, 'pin_memory': True}
trainset = dset.MNIST(root='MNIST', train=True, transform=transform, download=True)
train_loader = torch.utils.data.DataLoader(trainset, batch_size = batch_size, shuffle=True, **kwargs)

testset = dset.MNIST(root='MNIST', train=False, transform=transform)
test_loader = torch.utils.data.DataLoader(testset, batch_size=batch_size,shuffle=False, **kwargs)

print ('==>>> total training batch number: {}'.format(len(train_loader)))
print ('==>>> total testing batch number: {}'.format(len(test_loader)))
model = LeNet()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)

for epoch in range(10):  # loop over the dataset multiple times
    #training
    for batch_idx, (data, target) in enumerate(train_loader):
        optimizer.zero_grad()
        data, target = Variable(data), Variable(target)
        _,loss = model(data, target)
        loss.backward()
        optimizer.step()
        if batch_idx % 100 == 0:
                print ('==>>> epoch: {}, batch index: {}, train loss: {:.6f}'.format(epoch, batch_idx, loss.data[0]))
    #testing
    correct_cnt, ave_loss = 0,0
    for batch_idx, (data,target) in enumerate(test_loader):
        x,target = Variable(data),Variable(target)
        score,loss = model(x,target)
        _,pred_label = torch.max(score.data,1)
        correct_cnt += (pred_label == target.data).sum()
        ave_loss += loss.data[0]
    accuracy = correct_cnt*1.0/len(test_loader)/batch_size
    ave_loss /= len(test_loader)
    print ('==>>> epoch: {}, test loss: {:.6f}, accuracy: {:.4f}'.format(epoch, ave_loss, accuracy))

torch.save(model.state_dict(), model.name())
<file_sep>import os
os.system("./svm_learn BNP_test.txt model.txt ")<file_sep>import numpy as np
#Printing Arrays
a = np.arange(6) #일차원 배열
b = np.arange(12).reshape(4,3) #2차원배열
c = np.arange(24).reshape(2,3,4) #3차원배열
A = np.array( [[1,1], [0,1]] )
B = np.array( [[2,0], [3,4]] )
print(A*B) #elementwise product
A.dot(B) #matrix product
np.dot(A, B) #another matrix product
a = np.random.random((2,3))
print(a)
print(a.min())
print(a.max())
b = np.arange(12).reshape(3,4)
print(b.sum(axis=0)) #sum of each column
b.min(axis=1) #min of each row
b.cumsum(axis=1)  #cumulative sum along each row

#Universal Function
B = np.arange(3)
np.exp(B)
np.sqrt(B)
C = np.array([2., -1., 4.])
D = np.add(B, C)

#Indexing, Slicing and Iterating
a = np.arange(10)**3 #원소들값 3제곱

#Multidimensional
def f(x,y):
    return 10*x+y

b = np.fromfunction(f,(5,4),dtype=int)
b[0:5, 1] # each row in the second column of
b[ : ,1]
b[1:3, : ] # each column in the second and third row of b

for row in b:
     print(row)

for element in b.flat: #한원소씩 접근
    print(element)

#Stacking together different arrays
a = np.floor(10*np.random.random((2,2)))
print(a)
b = np.floor(10*np.random.random((2,2)))
np.vstack((a,b))
np.hstack((a,b))
np.column_stack((a,b))

#Splitting one array into several smaller ones
a = np.floor(10*np.random.random((2,12)))
np.hsplit(a,3) #split a into 3
np.hsplit(a,(3,4))   # Split a after the third and the fourth column

#Copies and Views
a = np.arange(12)
b = a
b.shape = 3,4
a.shape

#View or Shallow Copy
c = a.view()
c is a #true
c.base is a
#deep copy
d = a.copy()
d is a #false

#Linear Algebra
#Simple Array Operations
a = np.array([[1.0, 2.0], [3.0, 4.0]])
a.transpose()
u = np.eye(2) # unit 2x2 단위 matrix 생성
np.dot (j, j) # matrix product

<file_sep>#############Sequence#################
#1)make whole word list from train.txt, text.txt
#2)make word_cnt_dic based on acq or not AND make tf*idf dictionary => {word1 : 0.xxxx, word2 : 0.xxx, ....}
#3)make input_train, input_test files for svm
#4)run

from nltk.stem.snowball import SnowballStemmer
import re
import math
import copy

def make_stopword_list(f_name):
    f = open(f_name)
    for stopword in f:
        stopword_list = stopword.split(' ')
    return stopword_list

#delete stopword(s) in word list
def delete_stopword(word_list, stopword_list):
    for i in range(len(stopword_list)):
        if stopword_list[i] in word_list:
            word_list.remove(stopword_list[i])
    return


def UseStemmer(word_list,stemmer_obj):
    word_cnt = len(word_list)-2
    for i in range(1,word_cnt+1):
        word_list[i] = stemmer_obj.stem(word_list[i])
    return

def delete_word(complete_dic, word_dic):
    acq_word_list = word_dic['acq']
    etc_word_list = word_dic['etc']
    delete_acq_word = []
    delete_etc_word = []

    acq_word_cnt_dic = complete_dic['acq']
    etc_word_cnt_dic = complete_dic['etc']

    #delete from cnt_dic
    for word in acq_word_list:
        if acq_word_cnt_dic[word] < 5:
            delete_acq_word.append(word)
            acq_word_cnt_dic.pop(word)

    for word in etc_word_list:
        if etc_word_cnt_dic[word] < 5:
            delete_etc_word.append(word)
            etc_word_cnt_dic.pop(word)

    #delete form word_list
    for word in acq_word_list:
        if word in delete_acq_word:
            acq_word_list.pop(word)

    for word in etc_word_list:
        if word in delete_etc_word:
            etc_word_list.pop(word)
    return

def make_word_dic(file_name, stemmer, stopword_list):
    word_dic = {}

    f = open(file_name)

    for line in f:
        line_list = re.split('\t| ', line)
        delete_stopword(line_list, stopword_list)
        UseStemmer(line_list, stemmer)
        if line_list[0] == 'acq':
            input_class = line_list[0]
        else:
            input_class = 'etc'
        word_cnt = len(line_list) - 2
        # Check input_class is in input_dic
        if input_class not in word_dic:
            word_dic[input_class] = []

        word_list = word_dic[input_class]
        for i in range(1, word_cnt + 1):
            input_word = line_list[i]
            if input_word in word_list:
                continue
            else:
                word_list.append(input_word)
    return word_dic

def combine_two_word_list(word_list1, word_list2):
    new_word_list = copy.deepcopy(word_list1)
    for word2 in word_list2:
        if word2 not in new_word_list:
            new_word_list.append(word2)
    return new_word_list

def make_word_list(complete_word_dic):
    acq_word_list = complete_word_dic['acq']
    etc_word_list = complete_word_dic['etc']
    new_word_list =  combine_two_word_list(acq_word_list,etc_word_list)
    return new_word_list

def make_output_dictionary(file_name, stemmer, stopword_list ):
    output_dic = {}
    f = open(file_name)
    num_of_doc = 0
    # make dictionary => {'acq' : { 'word' : 0, ... } , 'etc' : { 'word' : 0, ... } }

    for line in f:
        num_of_doc += 1
        # line list contains => first element : class , last element : newline
        line_list = re.split('\t| ', line)
        delete_stopword(line_list, stopword_list)
        UseStemmer(line_list, stemmer)
        if line_list[0] == 'acq':
            input_class = line_list[0]
        else:
            input_class = 'etc'
        word_cnt = len(line_list) - 2
        # Check input_class is in input_dic
        if input_class not in output_dic:
            output_dic[input_class] = {}

        word_list = output_dic[input_class]
        # make word in class list or increase the count
        for i in range(1, word_cnt + 1):
            input_word = line_list[i]
            if input_word in word_list:
                word_list[input_word] += 1
            else:
                word_list[input_word] = 1
    return output_dic,num_of_doc

def calc_all_value(dic,list):
    all_num = 0
    for temp in list:
        all_num += dic[temp]
    return all_num

def calc_tf_idf(word_cnt,all_acq_word_num,num_of_doc,doc_cnt):
    term_freq = word_cnt/all_acq_word_num
    inv_doc_freq = math.log(num_of_doc/doc_cnt)
    result = term_freq * inv_doc_freq
    return result

def make_doc_cnt_dic(file_name, stemmer, stopword_list,complete_word_list):
    word_doc_cnt = {}

    for word in complete_word_list:
        word_doc_cnt[word] = 0

    f = open(file_name)

    for line in f:
        line_list = re.split('\t| ', line)
        delete_stopword(line_list, stopword_list)
        UseStemmer(line_list, stemmer)
        for word in complete_word_list:
            if word in line_list:
                word_doc_cnt[word] += 1

    return word_doc_cnt


def make_tf_idf_dic(complete_dic,complete_word_dic,all_word_list,num_of_doc,doc_cnt_dic):
    output_dic = {}
    acq_cnt_dic = complete_dic['acq']
    acq_word_list = complete_word_dic['acq']
    all_acq_word_num = calc_all_value(acq_cnt_dic,acq_word_list)

    for word in all_word_list:
        if word in acq_word_list:
            output_dic[word] = calc_tf_idf(acq_cnt_dic[word],all_acq_word_num,num_of_doc,doc_cnt_dic[word])
        else:
            output_dic[word] = 0

    return output_dic

def make_svm_input(input_file_name,stopword_list,stemmer,output_file_name,tf_idf_dic,all_word_list):
    input_file = open(input_file_name)
    output_file = open(output_file_name,'w')

    for line in input_file:
        # line list contains => first element : class , last element : newline
        output_str = ''
        line_list = re.split('\t| ', line)
        delete_stopword(line_list, stopword_list)
        UseStemmer(line_list, stemmer)
        word_cnt = len(line_list) - 2
        #extract words from line_list AND make sorted list ascending order
        temp_list = line_list[1:word_cnt+1]

        sorted_list = []
        for word in all_word_list:
            if word in temp_list:
                sorted_list.append(word)
        #make output stirng
        if line_list[0] == 'acq':
            output_str += '1'
        else:
            output_str += '-1'
        for word in sorted_list:
            index = all_word_list.index(word)+1
            if tf_idf_dic[word] == 0:
                continue
            output_str += ' '+str(index) + ':' + str(tf_idf_dic[word])
        output_str += '\n'
        output_file.write(output_str)
    return


##################################MAIN###################################
#make stemmer object and stopword list
stemmer = SnowballStemmer("english", ignore_stopwords=False)
stop_word_f_name = 'stopword.txt'
stopword_list = make_stopword_list(stop_word_f_name)

#make word list from train set, tf*idf dictionay => {word1 : 0.xxxx, word2 : 0.xxxx, word3 : 0.xxxx, ....}
train_input_f_name = 'train.txt'
complete_output_dic,num_of_doc = make_output_dictionary(train_input_f_name,stemmer,stopword_list)
complete_word_dic = make_word_dic(train_input_f_name,stemmer,stopword_list)
complete_word_list = make_word_list(complete_word_dic)
doc_cnt_dic = make_doc_cnt_dic(train_input_f_name,stemmer,stopword_list,complete_word_list)
tf_idf_dic = make_tf_idf_dic(complete_output_dic,complete_word_dic,complete_word_list,num_of_doc,doc_cnt_dic)

#make output files
test_input_f_name = 'test.txt'
train_output_f_name = 'BNP_train.txt'
test_output_f_name = 'BNP_test.txt'
make_svm_input(train_input_f_name,stopword_list,stemmer,train_output_f_name,tf_idf_dic,complete_word_list)
make_svm_input(test_input_f_name,stopword_list,stemmer,test_output_f_name,tf_idf_dic,complete_word_list)
<file_sep>import numpy as np

x = np.array([2,3,1,0])
x = np.array([2, 3, 1, 0])
x = np.array([[1,2.0],[0,0],(1+1j,3.)]) #허수 값을로 이루어진 배열
print(x)
x = np.array([[ 1.+0.j, 2.+0.j], [ 0.+0.j, 0.+0.j], [ 1.+1.j, 3.+0.j]]) #5행과 같은표현
print(x)

np.arange(10) #array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(2, 10, dtype=np.float) #array([ 2., 3., 4., 5., 6., 7., 8., 9.])
np.arange(2, 3, 0.1) #array([ 2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9]) , #2이상 3미만인 수들중 0.1간격으로 출력

np.linspace(1., 4., 6) #array([ 1. ,  1.6,  2.2,  2.8,  3.4,  4. ]) , 1이상 4이하인 수중 6개의 수를 선택
<file_sep>class Node:
    data = ''
    child_dic = {}
    attr_name_list = []
    def __init__(self, attr_name = None, attr_list = None):
        if attr_name is None:
            return
        self.data = attr_name
        if attr_list is None:
            return

        for temp in attr_list:
            self.child_dic[temp] = Node()
            self.attr_name_list.append(temp)

    def print_child_dic(self):
        print(self.child_dic)

    def get_child_dic(self):
        return self.child_dic

    def print_nodes(self):
        if self.data == 'empty':
            print('empty terminal node')
            return
        print(self.data)
        if self.data is 'e' or self.data is 'p': #terminal node이면 return
            return


test_node = Node('abcd')
test_node = Node()
print(test_node.data)<file_sep>training_data = open('xxx.data.txt','r')

for line in training_data:
    temp1 = line.split(',')
    if (temp1[22].strip('\n') == 'p' or temp1[22].strip('\n') == 'u'):
        print(temp1)
print('없음')

<file_sep>
def func_word(result_dic, words):
    for word in words:
        if word in result_dic:
            result_dic[word]+=1
        else:
            result_dic[word] = 1


def find_max_in_dic(result_dic):
    max = -1
    str = ''
    result_str = []
    result_str2 = []
    for key in result_dic:
        if result_dic[key] > max:
            max = result_dic[key]

    for key in result_dic:
        if result_dic[key] == max:
            result_str.append(key)
            result_str2.append(result_dic[key])
    return result_str, result_str2


file_name = input('파일명을 입력하세요 : ')
file_obj = open(file_name, 'r')
list = []
word_cnt = 0
line_cnt = 0
word_dic = {}
word_cnt_dic = {}

for line in file_obj:
    temp_word_list = line.split()
    func_word(word_dic, temp_word_list )
    word_cnt += len(temp_word_list)
    line_cnt += 1
    word_cnt_dic[line_cnt] = len(temp_word_list)

print('단어수 : ' + str(word_cnt))
print('줄수 : ' + str(line_cnt))

max_word_list, temp = find_max_in_dic(word_dic)
max_word_str= ''.join(max_word_list)
print('가장 많이 쓰인 단어 : ' + max_word_str)

temp_str1, temp_str2 = find_max_in_dic(word_cnt_dic)
max_line_num = ', '.join(str(x) for x in temp_str1)
max_word_num = ', '.join(str(x) for x in temp_str2)
print('단어가 가장 많은 라인 : ' + max_line_num)
print('단어가 가장 많이 포함된 라인에서의 단어수 : ' + max_word_num)<file_sep>import numpy as np
from scipy import linalg
import matplotlib.pyplot as plt

#numpy.matrix vs 2D numpy.ndarray
A = np.mat('[1 2;3 4]')
print(A)
print(A.I) #A의 역행렬 출력

b = np.mat('[5 6]')
print(b.T) #B의 transpose 행렬
print(A*b.T)

A = np.array([[1,2], [3,4]])
print(A)
print(linalg.inv(A)) #scipy모듈로 역행렬 구하기

b = np.array([[5,6]]) #2D array

#finding inverse
A = np.array([[1,3,5], [2,5,1], [2,3,8]])
print(A)
print(linalg.inv(A))

print(A.dot(linalg.inv(A)))

#solving linear system
A = np.array([[1, 2], [3, 4]]) #x+2y = 5 와 3x+4y = 6 연립방정식 해구하기
b = np.array([[5], [6]])
linalg.inv(A).dot(b)
A.dot(linalg.inv(A).dot(b)) - b #0이 되는지 확인
np.linalg.solve(A, b) #해를 구하는 더 빠른 방법
A.dot(np.linalg.solve(A, b)) - b

#Finding Determinant
A = np.array([[1,2],[-3,4]])
linalg.det(A) #determinant 구하기

#computing norms
A=np.array([[1,2],[3,4]])
linalg.norm(A)

#Solving linear least-squares problems and pseudo-inverses
c1, c2 = 5.0, 2.0
i = np.r_[1:11]
xi = 0.1*i
yi = c1*np.exp(-xi) + c2*xi
zi = yi + 0.05 * np.max(yi) * np.random.randn(len(yi))
A = np.c_[np.exp(-xi)[:, np.newaxis], xi[:, np.newaxis]]
c, resid, rank, sigma = linalg.lstsq(A, zi)

xi2 = np.r_[0.1:1.0:100j]
yi2 = c[0]*np.exp(-xi2) + c[1]*xi2
plt.plot(xi,zi,'x',xi2,yi2)
plt.axis([0,1.1,3.0,5.5])
plt.xlabel('$x_i$')
plt.title('Data fitting with linalg.lstsq')
plt.show()<file_sep>from __future__ import unicode_literals, print_function

import pycrfsuite

def index2feature(sent, i, offset): #index 정보를 첨가하여 string 반환
    word, tag = sent[i + offset]
    if offset < 0:
        sign = ''
    else:
        sign = '+'
    return '{}{}:word={}'.format(sign, offset, word)

def word2features(sent, i): #현재 형태소 앞에 2개와 뒤에 2개정보를 첨가하여 리스트로 반환
    L = len(sent)
    word, tag = sent[i]
    features = ['bias']
    features.append(index2feature(sent, i, 0))

    if i > 1:
        features.append(index2feature(sent, i, -2))
    if i > 0:
        features.append(index2feature(sent, i, -1))
    else:
        features.append('bos')

    if i < L - 2:
        features.append(index2feature(sent, i, 2))
    if i < L - 1:
        features.append(index2feature(sent, i, 1))
    else:
        features.append('eos')
    return features

def sent2tags(sent):
    return [tag for word, tag in sent]

def sent2features(sent):
    return [word2features(sent, i) for i in range(len(sent))]

def inputToList(f_name,true_list): #train.txt를 읽어서 정답리스트와 [형태소,태그] 리스트 생성
    input_f = open(f_name, 'r', encoding='utf-8')
    total_sentence = []

    for line in input_f: #한줄씩 읽기
        if (line == '\n'): #개행이면 무시
            continue
        word,tags = line.split(' ')
        tags = tags.rstrip() # \n제거
        #print(tags)
        if '+' in tags: #'+'문자 기준으로 나누기
            tags = tags.split('+')
            sentences1 = []
            temp_sentence = []
            for temp2 in tags:
                sentence = []
                if temp2[0] == '/': #첫문자가 '/' 인경우
                    temp_word = '/'
                    temp_tag = temp2[2:]
                else:
                    temp_word, temp_tag = temp2.split('/')
                sentence.append(temp_word)
                temp_sentence.append(temp_tag)
                sentence.append(temp_tag)
                sentences1.append(sentence)
            total_sentence.append(sentences1)
            true_list.append(temp_sentence)
        else: #'+' 없는경우(그 자체가 하나의 형태소인경우)
            sentence = []
            sentences1 = []
            temp_sentence = []
            if tags[0] == '/': #첫문자가 '/' 인경우
                temp_word = '/'
                temp_tag = tags[2:]
            else:
                temp_word, temp_tag = tags.split('/')
            temp_sentence.append(temp_tag)
            sentence.append(temp_word)
            sentence.append(temp_tag)
            sentences1.append(sentence)
            total_sentence.append(sentences1)
            true_list.append(temp_sentence)

    return total_sentence #전체 [형태소, 태그] 리스트 반환

def test_result(list1,list2):
    print('******test******')
    all_num = len(list1) #전체 항목갯수
    true_num = 0 #맞은 갯수
    for i in range(all_num):
        if list1[i] == list2[i]: #두 태깅 결과가 일치
            true_num += 1 #맞은 갯수 카운트 +1
    success_rate = true_num/all_num #정답률 계산
    print('Success Rate : '+str(success_rate))


true_list = [] #정답을 저장할 리스트
train_sents = inputToList('train.txt',true_list) #train.txt를 읽어서 [형태소,태그]들의 리스트로 변환
print(train_sents)

train_x = [sent2features(sent) for sent in train_sents] #[형태소,태그] 리스트를 가지고 형태소간의 index 정보를 추가한 리스트로 변환
print(train_x)
train_y = [sent2tags(sent) for sent in train_sents] #[형태소, 태그] 리스트를 가지고 tag정보들 반환
print(train_y)

trainer = pycrfsuite.Trainer() #학습객체 생성
for x, y in zip(train_x, train_y):
    trainer.append(x, y) #해당 형태소의 앞뒤의 형태소정보와 태그 정보를 가지고 학습
trainer.train('model.crfsuite')


tagger = pycrfsuite.Tagger()
tagger.open('model.crfsuite')

true_list = [] #정답을 저장할 리스트
test_sents = inputToList('train.txt',true_list) #train.txt를 읽어서 [형태소,태그]들의 리스트로 변환
test_x = [sent2features(sent) for sent in test_sents] #[형태소,태그] 리스트를 가지고 형태소간의 index 정보를 추가한 리스트로 변환
test_y = [sent2tags(sent) for sent in test_sents] #[형태소, 태그] 리스트를 가지고 tag정보들 반환

pred_y = [tagger.tag(x) for x in test_x] #예측값 도출
print(pred_y)

test_result(true_list,pred_y) #정답과 예측값을 비교하는함수<file_sep>from nltk.stem.snowball import SnowballStemmer
import re
import math
import operator

#parameter => file name, stemmer obj, stopword_list
#return complete_dictionary,class name list, number of class, class count dictionary
def make_dictionary(file_name, stemmer, stopword_list ):
    input_dic = {}
    cnt_in_class = {}
    class_name_list = []
    f = open(file_name)
    num_of_class = 0
    num_of_training_data = 0
    # make dictionary => {'earn' : { 'word' : 0, ... } , ... }

    for line in f:
        num_of_training_data += 1
        # line list contains => first element : class , last element : newline
        line_list = re.split('\t| ', line)
        delete_stopword(line_list, stopword_list)
        UseStemmer(line_list, stemmer)
        input_class = line_list[0]
        word_cnt = len(line_list) - 2
        # Check input_class is in input_dic
        if input_class not in input_dic:
            input_dic[input_class] = {}
            num_of_class+=1
            cnt_in_class[input_class] = 1
            class_name_list.append(input_class)
        else:
            cnt_in_class[input_class] += 1

        word_list = input_dic[input_class]
        # make word in class list or increase the count
        for i in range(1, word_cnt + 1):
            input_word = line_list[i]
            if input_word in word_list:
                word_list[input_word] += 1
            else:
                word_list[input_word] = 1
    return input_dic, cnt_in_class,num_of_class,class_name_list, num_of_training_data

#parameter =>  file_name, complete dictionary form training set, class name lsit, class probability list, stemmer obj, stopword list
#return => number of test data, success rate from test set
def exec_test(file_name,input_dic,class_name_list,class_prob_dic,stemmer,stopword_list):
    f = open(file_name)
    all_num = 0
    true_num = 0
    num_of_test_data = 0

    for line in f:
        num_of_test_data += 1
        all_num += 1
        # line list contains => first element : class , last element : newline
        line_list = re.split('\t| ', line)
        delete_stopword(line_list, stopword_list)
        UseStemmer(line_list, stemmer)
        true_class = line_list[0]
        answer = calc_answer(input_dic, class_name_list, class_prob_dic, line_list)
        if answer == true_class:
            true_num += 1
    success_rate = true_num/all_num
    return num_of_test_data,success_rate


#delete stopword(s) in word list
def delete_stopword(word_list, stopword_list):
    for i in range(len(stopword_list)):
        if stopword_list[i] in word_list:
            word_list.remove(stopword_list[i])
    return


def UseStemmer(word_list,stemmer_obj):
    word_cnt = len(word_list)-2
    for i in range(1,word_cnt+1):
        word_list[i] = stemmer_obj.stem(word_list[i])
    return


def make_class_prob_dic(cnt_class_dic, class_list):
    class_prob_dic = {}
    num_of_input = sum(cnt_class_dic.values())

    for class_name in class_list:
        class_prob_dic[class_name] = cnt_class_dic[class_name]/num_of_input

    return class_prob_dic


def calc_BayesClassifier(word_cnt_info, class_name, class_prob_dic, input_list):
    input_word_cnt = len(input_list) - 2
    log_sum = 0
    temp_word_cnt_dic = {}
    temp_word_list = []
    zero_cnt = 0

    #count zero count in input list if word is not in complete_word_dic, save word and word count to temp_dic
    for i in range(1,input_word_cnt+1):
        temp_word_list.append(input_list[i]) #append word
        if input_list[i] in word_cnt_info.keys():
            temp_word_cnt_dic[input_list[i]] = word_cnt_info[input_list[i]]
        else:
            temp_word_cnt_dic[input_list[i]] = 1
            zero_cnt += 1

    #calculate log summation
    for temp_word in temp_word_list:
        numerator = temp_word_cnt_dic[temp_word]
        denominator = sum(word_cnt_info.values()) + zero_cnt #분모에 zero개수 만큼 더해준다
        log_sum += math.log10(numerator/denominator)
    result = math.log10(class_prob_dic[class_name]) + log_sum
    return result


def calc_answer(complete_word_dic, class_name_list, class_prob_dic, input_list):
    BayesClassifier_dic = {}
    for class_name in class_name_list:
        BayesClassifier_dic[class_name] = calc_BayesClassifier(complete_word_dic[class_name],class_name,class_prob_dic,input_list)
    answer = max(BayesClassifier_dic.items(), key=operator.itemgetter(1))[0]
    return answer


def make_stopword_list(f_name):
    f = open(f_name)
    for stopword in f:
        stopword_list = stopword.split(' ')
    return stopword_list



###################################MAIN###################################
#make stemmer object and stopword list
stemmer = SnowballStemmer("english", ignore_stopwords=False)
f_name = 'stopword.txt'
stopword_list = make_stopword_list(f_name)

######training#####
f_name = 'train.txt'
input_dic,cnt_in_class,num_of_class,class_name_list,num_of_training_data = make_dictionary(f_name,stemmer,stopword_list)
class_prob_dic = make_class_prob_dic(cnt_in_class,class_name_list)

######testing#####
f_name = 'test.txt'
num_of_test_data, success_rate = exec_test(f_name,input_dic,class_name_list,class_prob_dic,stemmer,stopword_list)

print('##############test################')
print('num of training data : '+str(num_of_training_data))
print('num of test data : '+str(num_of_test_data))
print('Success Rate : ' + str(success_rate))
<file_sep>import numpy as np
x = np.arange(10) #size가 10인 배열생성
print(x[2]) #2
print(x[-2]) #8
x.shape = (2,5) #이제 x는 2치원배열이됨
print(x[1,3]) #8
print(x[1,-1]) #9
print(x[0])

x = np.arange(10) #size 10인 배열생성
print(x[2:5]) #2,3,4
print(x[1:7:2]) #1,3,5
y = np.arange(35).reshape(5,7)
print(y[1:5:2, ::3]) #1,3행에 3열 간격으로 선택하여 배열 구성 => [ [ 7 10 13 ] [ 21 24 27 ] ]

x = np.arange(10,1,-1)
print(x) #array([10,  9,  8,  7,  6,  5,  4,  3,  2])
temp = x[np.array([3, 3, 1, 8])]
print(temp) #array([7, 7, 9, 2])

x[np.array([3,3,-3,8])] #array([7, 7, 4, 2])

temp = y[np.array([0,2,4]), np.array([0,1,2])]
print(temp) # y[0,2], y[2,1], y[4,2]

temp = y[np.array([0,2,4]),1]
print(temp) # y[0,1], y[2,1], y[4,1]

temp = y[np.array([0,2,4])]
print(temp) # 0행,2행,4행 원소들 출력

#Bollean or "mask" index arrays
b = y>20
temp = y[b]
print(temp) #20보다 큰 원소들 모두 출력

x = np.arange(30).reshape(2,3,5) #0~29까지의 원소로 3차원 행렬 생성
print(x)
b = np.array([[True,True,False], [False,True, True]])
print(x[b]) #[x[0][0].x[0][1],x[1][1],x[1][2] 원소출력

#Structural indexing tools
print(y.shape) # (5,7)
temp = y[:,np.newaxis,:].shape
print(temp) #(5,1,7)

x = np.arange(10)
x[2:7] = 1
print(x)
x[2:7] = np.arange(5)

x = np.arange(0,50,10) #0,10,20,30,40
x[np.array([1,1,3,1])] += 1
print(x) # 0,11,20,31,40 =>1은 한번만 가산됨


<file_sep># 경북대학교 인공지능 AI 2017 봄학기 수업과제

## 1. decision tree   
22개의 attribute로 판단하는 독버섯과 정상버섯을 판단하는 decision tree를 구현      

## 2. MLP:Perceptron   
MNIST를 판단하는 single-layer perceptron, multi-layer perceptron 구현   

## 3. MNIST_LeNet5,MNIST_rnn
PyTorch로 LeNet-5 convolution neural network 구현 torchvision을 이용하여 MNIST 데이터셋을 import하여 학습 및 테스트
PyTorch로 RNN 구현보고서 작성 및 MNIST 데이터셋을 import하여 학습및 테스트      

## 4. bayes_classifier    
Naive Bayes로 reuters 기사를 8개의 클래스로 분류하는 분류기 구현   

## 5. svm,adaboost   
SVM으로 reuters 기사를 acq클래스인지 아닌지 분류하는 모델 생성및 테스트
기존의 svm 결과에서 틀린부분과 맞는부분을 바탕으로 가중치를 변경하여 학습시키는 adaboost 구현   

## 6. korean_spacing_predictor   
CRF(Conditional Random Field)를 이용하여 한국어 띄어쓰기 프로그램 구현   

## 7. korean_lexical_predictor(incomplete)   
CRF를 이용하여 한국어 형태소 분류기 구현
<file_sep>import random

def calc_output(x_list,w_list):
    result = 0
    for i in range(65):
        result = result + x_list[i]*w_list[i]
    if result > 0:
        result = 1
    else:
        result = 0
    return result

def calc_output2(x_list,w_list):
    result = 0
    for i in range(65):
        result = result + x_list[i]*w_list[i]
    return result

def change_w_list(w_list,x_list,learning_rate,target,output):
    for i in range(len(w_list)):
        w_list[i] = w_list[i] + learning_rate*(target-output)*x_list[i]
    return


learning_rate = 0.1
#perceptron 초기화
percept_dic = {0 : [], 1 : [], 2 : [], 3 : [], 4 : [], 5 : [], 6 : [], 7 : [], 8 : [], 9 : []}

for i in range(65):
    percept_dic[0].append(random.uniform(0, 1))
    percept_dic[1].append(random.uniform(0, 1))
    percept_dic[2].append(random.uniform(0, 1))
    percept_dic[3].append(random.uniform(0, 1))
    percept_dic[4].append(random.uniform(0, 1))
    percept_dic[5].append(random.uniform(0, 1))
    percept_dic[6].append(random.uniform(0, 1))
    percept_dic[7].append(random.uniform(0, 1))
    percept_dic[8].append(random.uniform(0, 1))
    percept_dic[9].append(random.uniform(0, 1))

#training_data = open('a.txt','r')

for temp in range(3):
    training_data = open('optdigits.tra', 'r')
    for line in training_data:
        x_list = [1]
        temp_list = line.split(',')
        for i in range(64):
            x_list.append(int(temp_list[i]))
        target_val = int(temp_list[64])
        #output_val = calc_output(calc_output(x_list,percept_dic[target_val])) #target value 값과 같은 perceptron 에 대한 output 계산
        for i in range(10): #0~9까지의 perceptron에 대하여
            if i == target_val:
                output_val = calc_output(x_list,percept_dic[i])
                change_w_list(percept_dic[i],x_list,learning_rate,1,output_val) #해당 perceptron만 1로 학습
            else:
                output_val = calc_output(x_list,percept_dic[i])
                change_w_list(percept_dic[i], x_list, learning_rate, 0, output_val) #나머지 perceptron은 0으로 학습

for i in range(10):
    print(percept_dic[i])

test_data = open('optdigits.tes','r')
#test_data = open('b.txt','r')

correct_num = 0
wrong_num = 0
result_dic = {0 : 0 , 1 : 0, 2 : 0, 3 : 0, 4 : 0, 5: 0, 6 : 0, 7 : 0, 8 : 0, 9 : 0}

for line in test_data:
    x_list = [1]
    temp_list = line.split(',')
    for i in range(64):
        x_list.append(float(temp_list[i]))
    for i in range(10):
        #result_dic[i] = 1 - calc_output(x_list,percept_dic[i])
        result_dic[i] = calc_output2(x_list, percept_dic[i])
    print(result_dic)
    #answer = min(result_dic, key=lambda key: result_dic[key])
    answer = max(result_dic, key=lambda key: result_dic[key])

    #print('답 : ' + str(answer))
    true_num = int(temp_list[64])
    if answer == true_num:
        #print('correct')
        correct_num = correct_num+1
    else:
        #print('wrong')
        #print(result_dic)
        #print(answer)
        #print(true_num)
        wrong_num = wrong_num+1

all_num = correct_num + wrong_num
success_percent = correct_num/all_num * 100
fail_percent = wrong_num/all_num * 100
print('Success Percent :' + str(success_percent))
print('Failure Percent :' + str(fail_percent))
#print(w_list)
<file_sep>import numpy as np

#2*3 array 생성 data type은 int32
x = np.array([[1,2,3], [4,5,6]], np.int32)
print(type(x)) #<class 'numpy.ndarray'>
print(x.shape) #(2,3)
print(x.dtype) #dtype('int32')
print(x[1,2]) #6

y = x[:,1]
print(y) #[2 5]
y[0] = 9
print(y) #[9 5]
print(x) #[[1 9 3]  [4 5 6]]

x = np.ndarray(shape=(2,2), dtype=float, order='C') #2*2 float 타입행렬 생성
print(x)

y = np.ndarray((2,), buffer=np.array([1,2,3]),offset=np.int_().itemsize,
dtype=int)
print(y)<file_sep>#TSLA 회사의 주식데이터(2010-2014)를 가져와서 화면에 그래프로 변화를 그려주는 프로그램입니다.

import matplotlib.pyplot as plt
import numpy as np
from numpy import genfromtxt

#CSV 파일 열기
my_data = genfromtxt('TSLA_stockprice2.csv', delimiter=',' ,names=True)
fname = open('TSLA_stockprice2.csv','r')
#0번째에서 6번째 칼럼을 대상으로 각각의 그래프 생성
plt.plotfile(fname, (0, 1, 2, 3, 4, 5, 6))

#Open,High,Low,Close 각각의 최대값을 받아옴
max_open = np.max(my_data['Open'])
max_high = np.max(my_data['High'])
max_low = np.max(my_data['Low'])
max_close = np.max(my_data['Close'])

#분석결과를 콘솔창에 출력
print('***** TSLA_stockprice 분석 결과값 *****')
print('open의 최대값 : '+str(max_open))
print('high의 최대값 : '+str(max_high))
print('low의 최대값 : '+str(max_low))
print('close의 최대값 : '+str(max_close))

#최종적인 결과를 화면에 출력하기
plt.suptitle('TSLA_stockprice',fontsize = 20) #그래프 제목설정
plt.show()
<file_sep>file = open("input.txt",'r')
file2 = open("data_set.txt",'w')
file3 = open("test_set.txt",'w')
file4 = open("validation_set.txt",'w')

for i in range(5000):
    file2.write(file.readline())

for i in range(500):
    file4.write(file.readline())

for i in range(2624):
    file3.write(file.readline())
<file_sep>import numpy as np
x = np.float32(1.0)
print(x)
y = np.int_([1,2,4])
print(y)
z = np.arange(3, dtype=np.uint8)
print(z)

temp = np.array([1,2,3], dtype = 'f')
print(temp)

temp = z.astype(float) #uint8 datatype을 float로 변경
print(temp)

temp = np.int8(z)
print(temp)

temp = z.dtype
print(temp)

d = np.dtype(int)
print(d)

temp = np.issubdtype(d, int) #d가 int타입이면 true 아니면 false를 반환
print(temp) # true

temp = np.issubdtype(d, float) #d가 float타입이면 true 아니면 false를 반환
print(temp) #false
<file_sep>import numpy as np
import copy
from collections import OrderedDict

class Node:
    data = ''

    def __init__(self):
       self.child_dic = {}
       self.attr_list = []

    def print_child_dic(self): #child_dic를 만들고, attr_list도 만든다
        print(self.child_dic)

    def set_child_dic(self,attr_list):
        for temp in attr_list:
            self.child_dic[temp] = Node()
            self.attr_list.append(temp)

    def get_child_dic(self):
        return self.child_dic

    def print_nodes(self):
        if self.data == 'empty':
            print('empty')
            return
        print(self.data)
        if self.data is 'e' or self.data is 'p': #terminal node이면 return
            return
        for temp in self.attr_list:
            print(str(temp) + '일때')
            self.child_dic[temp].print_nodes()


def calc_antrophy(list):
    e_num = 0
    p_num = 0
    for temp in list:
        if temp['classes'] == 'e':
            e_num = e_num+1
        elif temp['classes'] == 'p':
            p_num = p_num+1
    all_num = e_num + p_num
    result = (-1) * e_num/all_num * np.log2(e_num/all_num) + (-1) * p_num/all_num * np.log2(p_num/all_num)
    return result


def calc_antrophy2(list):
    e_num = list[0]
    p_num = list[1]
    all_num = e_num + p_num
    if e_num == 0 and p_num == 0:
        return 0
    elif e_num == 0 and p_num != 0:
        result = (-1) * p_num / all_num * np.log2(p_num / all_num)
        return result
    elif p_num == 0 and e_num != 0:
        result = (-1) * e_num / all_num * np.log2(e_num / all_num)
        return result
    result = (-1) * e_num / all_num * np.log2(e_num / all_num) + (-1) * p_num / all_num * np.log2(p_num / all_num)
    return result


def calc_gain(total_entrophy, total_num, temp, dic_name, ent_list, attr_array):
    result = total_entrophy
    for i in dic_name[temp]:
        result -= (attr_array[temp][i][0] + attr_array[temp][i][1])/total_num * ent_list[temp][i]
    return result


def all_same(items): #list안에 값들이 서로 같은지 검사하는 함수
    return all(x == items[0] for x in items)


def check_end(training_dic_list, attr_name_list): #끝인경우는 true를 반환 끝이 아닌경우는 false
    #attrbute_name_list에 아무것도 없는 경우 or e,p값이 섞여있지 않은경우
    if not attr_name_list:
        print("List is empty!")
        return True

    #e,p 값 섞여있는지 판단
    result_list = []
    for temp in training_dic_list:
        result_list.append(temp['classes'])
    if all_same(result_list) is True:
        return True
    return False


def make_decision_tree(root_node,training_dic_list,attr_name_list,dic_name):

    if not training_dic_list: #training_dic_list에 아무값도 없는경우(특정값을 가지는 데이터가 없는경우
        print('empty node')
        root_node.data = 'empty'
        return

    if check_end(training_dic_list,attr_name_list) is True: #종료조건
        #terminal node를 등록
        temp_node = make_terminal_node(training_dic_list,attr_name_list)
        root_node.data = temp_node.data
        return

    total_num = len(training_dic_list)
    print('total_num : ' + str(total_num))
    D = calc_antrophy(training_dic_list)
    print('D : '+str(D))
    # 2) 22개의 attr 각각에 대한 Gain값 계산
    attr_list = {}
    attr_temp1 = {}

    attr_ent = {}
    attr_temp2 = {}

    gain_list = {}
    attr_temp3 = {}

    for attr_name in attr_name_list[1:]:
        for j in dic_name[attr_name]:
            attr_temp1[j] = [0, 0]
            attr_temp2[j] = 0
        attr_list[attr_name] = attr_temp1
        attr_ent[attr_name] = attr_temp2
        gain_list[attr_name] = 0
        attr_temp1 = {}
        attr_temp2 = {}

    # e,p 구분
    for line in training_dic_list:
        for temp in attr_name_list[1:]:
            if line['classes'] == 'e':
                attr_list[temp][line[temp]][0] += 1
            elif line['classes'] == 'p':
                attr_list[temp][line[temp]][1] += 1

    # entrophy 계산
    for temp in attr_name_list[1:]:
        for temp2 in dic_name[temp]:
            attr_ent[temp][temp2] = calc_antrophy2(attr_list[temp][temp2])

    # gain 값 구하기
    for temp in attr_name_list[1:]:
        gain_list[temp] = calc_gain(D, total_num, temp, dic_name, attr_ent, attr_list)

    # gain이 최대인 칼럼찾기
    max_attr_name = max(gain_list, key=gain_list.get)
    print('max_attr_name')
    print(max_attr_name)
    # 최대인 칼럼으로 노드생성 => attribute 종류만큼 child node 생성
    #root_node = Node(max_attr_name,dic_name[max_attr_name])
    root_node.data = max_attr_name
    root_node.set_child_dic(dic_name[max_attr_name])
    print('child_dic')
    root_node.print_child_dic()

    new_attribute_name_list = make_new_attribute_name_list(attr_name_list, max_attr_name)

    #training data set 가공
    for temp in dic_name[max_attr_name]:
        print(temp)
        new_training_data_list = make_new_training_data_list(training_dic_list,max_attr_name,temp)
        make_decision_tree(root_node.child_dic[temp],new_training_data_list,new_attribute_name_list,dic_name)

    return root_node


def make_terminal_node(training_dic_list,attr_name_list):
    #결과값 모두 저장
    result_list = []
    for temp in training_dic_list:
        result_list.append(temp['classes'])
    print('attr_name_list')
    print(attr_name_list)

    if not attr_name_list: #attr_name_list가 비어있는경우
        print('empty_attr_list!!!!!!!!!!!')
        if all_same(result_list) is True:  # attr_name_list에 아무것도 없으면서(끝까지 가지를 뻗은경우) 값이 모두 동일한 경우
            terminal_node = Node(result_list[0])
            print("List is empty and all value is same!")
        elif all_same(result_list) is False:  # attr_name_list에 아무것도 없으면서(끝까지 가지를 뻗은경우) 값이 섞여있는 경우
            print("terminal에서 다수결로 결정")
            # result_set에서 더 많은 값으로 선택
            if result_list.count('e') >= result_list.count('p'):
                terminal_node = Node('e')
            else:
                terminal_node = Node('p')
    else: #attr_name_list에 값이 있지만 결과값이 모두 동일한 경우
        print('NOT empty')
        terminal_node = Node()
        terminal_node.data = result_list[0]
        print("List is NOT empty BUT all value is same!")

    return terminal_node


def make_new_training_data_list(training_data_list, attr_name, value): #value와 일치하는 값을 가지는 데이터셋만 찾아서 반환
    new_training_data_list = []
    for temp in training_data_list:
        if temp[attr_name] == value:
            new_training_data_list.append(temp)
    return new_training_data_list


def make_new_attribute_name_list(attr_name_list,attr_name):
    for i in range(len(attr_name_list)):
        if attr_name_list[i] == attr_name:
            attr_name_list.pop(i)
            return attr_name_list


def func_test_data(root_node, test_dic_list):
    all_num = len(test_dic_list)
    correct_num = 0
    wrong_num = 0

    for test_dic in test_dic_list: #총 데이터의 수만큼 반복
        temp_node = root_node
        print(temp_node.data)
        while is_terminal(temp_node.data) is False: #terminal이 될때 까지 반복
            print('node_data')
            node_data = test_dic[temp_node.data]
            print(node_data)
            temp_node = temp_node.child_dic[node_data]
        print('result : ' + temp_node.data)
        print('real value : ' + test_dic['classes'])
        if (temp_node.data == test_dic['classes']):
            correct_num+=1
        else:
            wrong_num+=1


    suc_percent = (correct_num/all_num) * 100
    fail_percent = (wrong_num/all_num) * 100

    print(correct_num)
    print(wrong_num)
    print('success percentage : ' + str(suc_percent))
    print('fail percentage : ' + str(fail_percent))


def is_terminal(data):
    if data == 'e' or data == 'p' or data == 'empty':
        return True
    return False

#################################################################
#mushroom.names file 읽어서, dictionary에 저장 eg.  { 'classes' : {e,p}, ....}
file_msuhroom_names = open("mushroom.names",'r')
dic_name = OrderedDict()
for line in file_msuhroom_names:
    temp = line.split(' ')
    attr_values = []
    for item in temp[1:]:
        attr_values.append(item.strip('\n'))
    dic_name[temp[0]] = attr_values
print('dic_name')
print(dic_name)
#attr name을 저장
items = list(dic_name.items())
attr_name_list = []
for temp in items:
    attr_name_list.append(temp[0])
print('attr_name_list')
print(attr_name_list)

#training data를 파일로 부터 읽어서 dictionary 형태로 변환
training_data = open('training_set.txt','r')
#training_data = open('a.txt','r')

training_dic_list = []

for line in training_data:
    temp1 = line.split(',')
    i = 0
    training_dic = {}
    for temp2 in attr_name_list:
        training_dic[temp2] = temp1[i].strip('\n')
        i+=1
    training_dic_list.append(training_dic)

print('********************************************')
new_attr_name_list = copy.deepcopy(attr_name_list)
root_node = Node()
root_node = make_decision_tree(root_node,training_dic_list,new_attr_name_list, dic_name)
print('*********************************************')

root_node.print_nodes()

#test_data = open('test_set.txt','r')
#test_data = open('validation_set.txt','r')
#test_data = open('b.txt','r')
test_data = open('input.txt','r')

test_dic_list = []
for line in test_data:
    temp1 = line.split(',')
    i = 0
    test_dic = {}
    for temp2 in attr_name_list:
        test_dic[temp2] = temp1[i].strip('\n')
        i+=1
    test_dic_list.append(test_dic)

func_test_data(root_node,test_dic_list)
<file_sep>from scipy.integrate import quad

def integrand(x,a,b):
    return a*x**2 + b

a = 2
b = 1
I = quad(integrand, 0, 1, args=(a,b)) #ax제곱 + b 함수를 0~1 구간으로 정적분
print(I)
<file_sep>from nltk.stem.snowball import SnowballStemmer
import re
import math
import copy
import os


def make_stopword_list(f_name):
    f = open(f_name)
    for stopword in f:
        stopword_list = stopword.split(' ')
    return stopword_list

#delete stopword(s) in word list
def delete_stopword(word_list, stopword_list):
    for i in range(len(stopword_list)):
        if stopword_list[i] in word_list:
            word_list.remove(stopword_list[i])
    return


def UseStemmer(word_list,stemmer_obj):
    word_cnt = len(word_list)-2
    for i in range(1,word_cnt+1):
        word_list[i] = stemmer_obj.stem(word_list[i])
    return

def delete_word(complete_dic, word_dic):
    acq_word_list = word_dic['acq']
    etc_word_list = word_dic['etc']
    delete_acq_word = []
    delete_etc_word = []

    acq_word_cnt_dic = complete_dic['acq']
    etc_word_cnt_dic = complete_dic['etc']

    #delete from cnt_dic
    for word in acq_word_list:
        if acq_word_cnt_dic[word] < 5:
            delete_acq_word.append(word)
            acq_word_cnt_dic.pop(word)

    for word in etc_word_list:
        if etc_word_cnt_dic[word] < 5:
            delete_etc_word.append(word)
            etc_word_cnt_dic.pop(word)

    #delete form word_list
    for word in acq_word_list:
        if word in delete_acq_word:
            acq_word_list.pop(word)

    for word in etc_word_list:
        if word in delete_etc_word:
            etc_word_list.pop(word)
    return

def make_word_dic(file_name, stemmer, stopword_list):
    word_dic = {}

    f = open(file_name)

    for line in f:
        line_list = re.split('\t| ', line)
        delete_stopword(line_list, stopword_list)
        UseStemmer(line_list, stemmer)
        if line_list[0] == 'acq':
            input_class = line_list[0]
        else:
            input_class = 'etc'
        word_cnt = len(line_list) - 2
        # Check input_class is in input_dic
        if input_class not in word_dic:
            word_dic[input_class] = []

        word_list = word_dic[input_class]
        for i in range(1, word_cnt + 1):
            input_word = line_list[i]
            if input_word in word_list:
                continue
            else:
                word_list.append(input_word)
    return word_dic

def combine_two_word_list(word_list1, word_list2):
    new_word_list = copy.deepcopy(word_list1)
    for word2 in word_list2:
        if word2 not in new_word_list:
            new_word_list.append(word2)
    return new_word_list

def make_word_list(complete_word_dic):
    acq_word_list = complete_word_dic['acq']
    etc_word_list = complete_word_dic['etc']
    new_word_list =  combine_two_word_list(acq_word_list,etc_word_list)
    return new_word_list

def make_output_dictionary(file_name, stemmer, stopword_list ):
    output_dic = {}
    f = open(file_name)
    num_of_doc = 0
    # make dictionary => {'acq' : { 'word' : 0, ... } , 'etc' : { 'word' : 0, ... } }

    for line in f:
        num_of_doc += 1
        # line list contains => first element : class , last element : newline
        line_list = re.split('\t| ', line)
        delete_stopword(line_list, stopword_list)
        UseStemmer(line_list, stemmer)
        if line_list[0] == 'acq':
            input_class = line_list[0]
        else:
            input_class = 'etc'
        word_cnt = len(line_list) - 2
        # Check input_class is in input_dic
        if input_class not in output_dic:
            output_dic[input_class] = {}

        word_list = output_dic[input_class]
        # make word in class list or increase the count
        for i in range(1, word_cnt + 1):
            input_word = line_list[i]
            if input_word in word_list:
                word_list[input_word] += 1
            else:
                word_list[input_word] = 1
    return output_dic,num_of_doc

def calc_all_value(dic,list):
    all_num = 0
    for temp in list:
        all_num += dic[temp]
    return all_num

def calc_tf_idf(word_cnt,all_acq_word_num,num_of_doc,doc_cnt):
    term_freq = word_cnt/all_acq_word_num
    inv_doc_freq = math.log(num_of_doc/doc_cnt)
    result = term_freq * inv_doc_freq
    return result

def make_doc_cnt_dic(file_name, stemmer, stopword_list,complete_word_list):
    word_doc_cnt = {}

    for word in complete_word_list:
        word_doc_cnt[word] = 0

    f = open(file_name)

    for line in f:
        line_list = re.split('\t| ', line)
        delete_stopword(line_list, stopword_list)
        UseStemmer(line_list, stemmer)
        for word in complete_word_list:
            if word in line_list:
                word_doc_cnt[word] += 1

    return word_doc_cnt


def make_tf_idf_dic(complete_dic,complete_word_dic,all_word_list,num_of_doc,doc_cnt_dic):
    output_dic = {}
    acq_cnt_dic = complete_dic['acq']
    acq_word_list = complete_word_dic['acq']
    all_acq_word_num = calc_all_value(acq_cnt_dic,acq_word_list)

    for word in all_word_list:
        if word in acq_word_list:
            output_dic[word] = calc_tf_idf(acq_cnt_dic[word],all_acq_word_num,num_of_doc,doc_cnt_dic[word])
        else:
            output_dic[word] = 0

    return output_dic

##################functions for Adaboost###################
def make_expected_val_list(file_name):
    expected_val_list = []
    f = open(file_name)

    for line in f:
        line_list = re.split('\t| ', line)
        if line_list[0] == 'acq':
            expected_val_list.append(1)
        else:
            expected_val_list.append(-1)

    return expected_val_list

def make_output_list(input_file_name):
    input_file = open(input_file_name)
    output_list = []

    for output_val in input_file:
        if float(output_val) > 0 :
            output_list.append(1)
        else:
            output_list.append(-1)

    return output_list

def init_weight_list(weight_list,num_of_data):
    for i in range(num_of_data):
        weight_list.append(1/num_of_data)
    return

def calc_sum_of_weight(expected_val_list, weight_list, output_list, num_of_data):
    sum = 0
    for i in range(num_of_data):
        sum += weight_list[i] * math.exp( (-1) * expected_val_list[i]  * output_list[i])
    return sum

def updata_weights(weight_list,expected_val_list,output_list,alpha_val,num_of_data):
    for i in range(num_of_data):
        weight_list[i] = weight_list[i] * math.exp((-1)*expected_val_list[i]*output_list[i]*alpha_val)
    return

def make_new_weight_list(input_file_name, expected_val_list, num_of_data):
    weight_list = []
    init_weight_list(weight_list,num_of_data)
    output_list = make_output_list(input_file_name)
    sum_of_weight = calc_sum_of_weight(expected_val_list, weight_list,output_list,num_of_data)
    alpha_val = math.log((1-sum_of_weight)/sum_of_weight)/2
    updata_weights(weight_list,expected_val_list,output_list,alpha_val,num_of_data)
    return weight_list

def make_new_svm_input(input_file_name,stopword_list,stemmer,output_file_name,tf_idf_dic,all_word_list,weight_list):
    input_file = open(input_file_name)
    output_file = open(output_file_name,'w')

    i = 0
    for line in input_file:
        # line list contains => first element : class , last element : newline
        output_str = ''
        line_list = re.split('\t| ', line)
        delete_stopword(line_list, stopword_list)
        UseStemmer(line_list, stemmer)
        word_cnt = len(line_list) - 2
        #extract words from line_list AND make sorted list ascending order
        temp_list = line_list[1:word_cnt+1]

        sorted_list = []
        for word in all_word_list:
            if word in temp_list:
                sorted_list.append(word)
        #make output stirng
        if line_list[0] == 'acq':
            output_str += '1'
        else:
            output_str += '-1'
        for word in sorted_list:
            index = all_word_list.index(word)+1
            if tf_idf_dic[word] == 0:
                continue
            output_str += ' '+str(index) + ':' + str(tf_idf_dic[word]*weight_list[i])
        output_str += '\n'
        output_file.write(output_str)
        i+=1
    return

def make_svm_input(input_file_name,stopword_list,stemmer,output_file_name,tf_idf_dic,all_word_list):
    input_file = open(input_file_name)
    output_file = open(output_file_name,'w')

    for line in input_file:
        # line list contains => first element : class , last element : newline
        output_str = ''
        line_list = re.split('\t| ', line)
        delete_stopword(line_list, stopword_list)
        UseStemmer(line_list, stemmer)
        word_cnt = len(line_list) - 2
        #extract words from line_list AND make sorted list ascending order
        temp_list = line_list[1:word_cnt+1]

        sorted_list = []
        for word in all_word_list:
            if word in temp_list:
                sorted_list.append(word)
        #make output stirng
        if line_list[0] == 'acq':
            output_str += '1'
        else:
            output_str += '-1'
        for word in sorted_list:
            index = all_word_list.index(word)+1
            if tf_idf_dic[word] == 0:
                continue
            output_str += ' '+str(index) + ':' + str(tf_idf_dic[word])
        output_str += '\n'
        output_file.write(output_str)
    return

def test_exec(output_f_name1,output_f_name2,expected_list,alpha1,alpha2,len):
    output_list1 = make_output_list(output_f_name1)
    output_list2 = make_output_list(output_f_name2)
    true_num = 0
    for i in range(len):
        new_output = output_list1[i] * alpha1 + output_list2[i] * alpha2
        if (new_output > 0):
            new_output = 1
        else:
            new_output = -1
        if new_output == expected_list[i]:
            true_num += 1

    print('success rate : ' + (str)(true_num/len))



##################################MAIN###################################
alpha1 = 0
alpha2 = 0
#make stemmer object and stopword list
stemmer = SnowballStemmer("english", ignore_stopwords=False)
stop_word_f_name = 'stopword.txt'
stopword_list = make_stopword_list(stop_word_f_name)

#make word list from train set, tf*idf dictionay => {word1 : 0.xxxx, word2 : 0.xxxx, word3 : 0.xxxx, ....}
train_input_f_name = 'train.txt'
complete_output_dic,num_of_doc = make_output_dictionary(train_input_f_name,stemmer,stopword_list)
complete_word_dic = make_word_dic(train_input_f_name,stemmer,stopword_list)
complete_word_list = make_word_list(complete_word_dic)
doc_cnt_dic = make_doc_cnt_dic(train_input_f_name,stemmer,stopword_list,complete_word_list)
tf_idf_dic = make_tf_idf_dic(complete_output_dic,complete_word_dic,complete_word_list,num_of_doc,doc_cnt_dic)

#make expected value list
expected_val_list = make_expected_val_list(train_input_f_name)
#make output files
first_output_name = 'input1.txt'
second_output_name = 'input2.txt'

output_f1_name = 'output1.txt'
output_f2_name = 'output2.txt'
weight_list = []
init_weight_list(weight_list,len(expected_val_list))
make_new_svm_input(train_input_f_name,stopword_list,stemmer,first_output_name,tf_idf_dic,complete_word_list,weight_list)
print('################### model1 생성 ######################')
os.system("./svm_learn input1.txt model1.txt")
os.system("./svm_classify input1.txt model1.txt "+ output_f1_name)
new_weight_list = make_new_weight_list(output_f1_name,expected_val_list,len(expected_val_list))
make_new_svm_input(train_input_f_name,stopword_list,stemmer,second_output_name,tf_idf_dic,complete_word_list,new_weight_list)
print('################### model2 생성 ######################')
os.system("./svm_learn input2.txt model2.txt")
os.system("./svm_classify input1.txt model2.txt "+ output_f2_name)

#expected_val_list2 = make_expected_val_list('test.txt')
'''
test_input_f_name = 'test.txt'
test_output_f_name = 'new_test.txt'
output_f_name = 'output.txt'
make_new_svm_input(test_input_f_name,stopword_list,stemmer,test_output_f_name,tf_idf_dic,complete_word_list,new_weight_list)
make_new_svm_input(test_input_f_name,stopword_list,stemmer,test_output_f_name,tf_idf_dic,complete_word_list,new_weight_list)

'''

<file_sep>#polynomials
import numpy as np
from numpy import poly1d

p = poly1d([3,4,5]) #3x+4x+5
print(p)

print(p*p) #9x + 24x + 46x + 40x + 25

#Vectorizing functions

def addsubtract(a,b):
    if a > b:
        return a-b
    else:
        return a+b


vec_addsubtract = np.vectorize(addsubtract)
print(vec_addsubtract([0,3,6,9], [1,3,5,7]))

#Type handling
print(np.cast['f'](np.pi)) #3.1415927410125732

#Other useful functions
x = np.r_[-2:3]
print(x)
print(np.select([x > 3, x >= 0], [0,x+2]))



<file_sep>import numpy as np

a = np.array([1.0,2.0,3.0])
b = np.array([2.0,2.0,2.0])
print(a*b)

a = np.array([1.0,2.0,3.0])
b= 2.0
print(a*b)

#General Boradcasting Rules
x = np.arange(4)
xx = x.reshape(4,1)
y = np.ones(5) #1 다섯개로 이루어진 1D 배열
print(y)
z = np.ones((3,4))

print(xx + y) # 4X5행렬 생성
a = np.array([0.0,10.0,20.0,30.0])
b = np.array([1.0,2.0,3.0])
temp = a[:,np.newaxis] + b
print(temp.shape) #4X3 행렬생성
print(temp)

<file_sep>import random
import math

def sigmoid(x):
  return 1 / (1 + math.exp(-x))

def calc_output(x_list,w_list):
    result = 0
    for i in range(len(x_list)):
        result = result + x_list[i]*w_list[i]
    result = sigmoid(result)
    return result

def calc_real_output(x_list,w_list):
    result = 0
    for i in range(len(x_list)):
        result = result + x_list[i] * w_list[i]
    return result

def calc_output_delta(target, output_val):
    result = output_val * (1-output_val) * (target - output_val)
    return result

def calc_hidden_delta(output_percept_dic,output_delta_dic,hidden_node_num, output_val):
    sum = 0
    for i  in range(10):
        sum += output_delta_dic[i]*output_percept_dic[i][hidden_node_num]
    result = output_val * (1-output_val)*sum
    return result

#output layer에서 output node 한개에 대한 w_list 수정
def change_w_list_output(w_list,x_list,learning_rate,target,output):
    for i in range(len(w_list)):
        w_list[i] = w_list[i] + learning_rate*calc_output_delta(target,output)*x_list[i]
    return

#hidden layer에서 hidden node 한개에 대한 w_list 수정
def change_w_list_hidden(hidden_w_list,learning_rate,hidden_delta_val,x_list):
    for i in range(len(hidden_w_list)):
        hidden_w_list[i] = hidden_w_list[i] + learning_rate*hidden_delta_val*x_list[i]
    return


learning_rate = 0.05
#output layer, hidden layer 선언
output_percept_dic = {0 : [], 1 : [], 2 : [], 3 : [], 4 : [], 5 : [], 6 : [], 7 : [], 8 : [], 9 : []}
hidden_percept_dic = {0 : [], 1 : [], 2 : [], 3 : [], 4 : [], 5 : [], 6 : [], 7 : [], 8 : [], 9 : []}
output_delta_dic = {0 : 0, 1 : 0, 2 : 0, 3 : 0, 4 : 0, 5 : 0, 6 : 0, 7 : 0, 8 : 0, 9 : 0}
hidden_delta_dic = {0 : 0, 1 : 0, 2 : 0, 3 : 0, 4 : 0, 5 : 0, 6 : 0, 7 : 0, 8 : 0, 9 : 0}

for i in range(10):
    for j in range(10):
        output_percept_dic[j].append(random.uniform(-0.125,0.125))

for i in range(10):
    for j in range(65):
        hidden_percept_dic[i].append(random.uniform(-0.125,0.125))

training_data = open('optdigits.tra','r')
#training_data = open('a.txt','r')

for i in range(50):
    training_data = open('optdigits.tra', 'r')
    for line in training_data:
        output_layer_output = []
        hidden_layer_output = []
        x_list = [1]
        temp_list = line.split(',')
        for i in range(64):
            x_list.append(int(temp_list[i]))
        target_val = int(temp_list[64])
        #Forward Propagation
        for i in range(10):
            hidden_layer_output.append(calc_output(x_list,hidden_percept_dic[i]))
        for i in range(10):
            output_layer_output.append(calc_output(hidden_layer_output,output_percept_dic[i]))
        #Backward Propagation
        #output layer w값수정
        for i in range(10):
            if target_val == i:
                change_w_list_output(output_percept_dic[i],hidden_layer_output,learning_rate,1,output_layer_output[i])
                # output_delta 값 구하기
                output_delta_dic[i] = calc_output_delta(1, output_layer_output[i])
            else:
                change_w_list_output(output_percept_dic[i], hidden_layer_output, learning_rate, 0, output_layer_output[i])
                #output_delta 값 구하기
                output_delta_dic[i] = calc_output_delta(0, output_layer_output[i])

        #hidden layer의 delta값 구하기
        for i in range(10):
            hidden_delta_dic[i] = calc_hidden_delta(output_percept_dic,output_delta_dic,i,hidden_layer_output[i])

        #hidden layer w값수정
        for i in range(10):
            change_w_list_hidden(hidden_percept_dic[i],learning_rate,hidden_delta_dic[i],x_list)

print('output layer:')
for i in range(10):
    print(i)
    print(output_percept_dic[i])

print('hidden layer:')
for i in range(10):
    print(i)
    print(hidden_percept_dic[i])


test_data = open('optdigits.tes','r')
#test_data = open('b.txt','r')

correct_num = 0
wrong_num = 0
result_dic = {0 : 0 , 1 : 0, 2 : 0, 3 : 0, 4 : 0, 5: 0, 6 : 0, 7 : 0, 8 : 0, 9 : 0}

print('####### test #######')
for line in test_data:
    hidden_layer_output = []
    x_list = [1]
    temp_list = line.split(',')
    for i in range(64):
        x_list.append(int(temp_list[i]))

    #hidden_layer_output 생성
    for i in range(10):
        hidden_layer_output.append(calc_output(x_list,hidden_percept_dic[i]))
    #output_layer_output 생성 -> result_dic에 저장
    for i in range(10):
        result_dic[i] = calc_output(hidden_layer_output,output_percept_dic[i])

    #answer = min(result_dic, key=lambda key: result_dic[key])
    answer = max(result_dic, key=lambda key: result_dic[key]) #값중에서 최대값을 구함
    #print(answer)
    #print('답 : ' + str(answer))
    true_num = int(temp_list[64])
    if answer == true_num:
        print(true_num)
        print(result_dic)
        correct_num = correct_num+1
    else:
        #print('wrong')
        #print(result_dic)
        wrong_num = wrong_num+1

all_num = correct_num + wrong_num
success_percent = correct_num/all_num * 100
fail_percent = wrong_num/all_num * 100
print('Success Percent :' + str(success_percent))
print('Failure Percent :' + str(fail_percent))
#print(w_list)
