나는 KNN의 유클리드 거리를 계산하는 예를 많이 보았지만 정서적 인 분류는 아니다.KNN의 유클리드 거리 계산하기
예를 들어나는이 문장을 "매우 가까운 게임"
어떻게 문장 "좋은 게임"에 대한 유클리드 거리를 계산합니까?
나는 KNN의 유클리드 거리를 계산하는 예를 많이 보았지만 정서적 인 분류는 아니다.KNN의 유클리드 거리 계산하기
예를 들어나는이 문장을 "매우 가까운 게임"
어떻게 문장 "좋은 게임"에 대한 유클리드 거리를 계산합니까?
유클리드 거리를 계산할 수있는 좌표계를 정의한 후에 만 문장을 다차원 공간으로 생각하십시오. 예를 들어. 당신은
O2 - 알파벳 센터 (난 그냥 생각). 문장에서 각 작업의 알파벳 중심의 산술 평균으로 계산할 수 있습니다. 그래서
CharsIndex = Sum(Char.indexInWord)/CharsCountInWord; CharsCode = Sum(Char.charCode)/CharsCount; AlphWordCoordinate = [CharsIndex, CharsCode]; WordsIndex = Sum(Words.CharsIndex)/WordsCount; WordsCode = Sum(Words.CharsCode)/WordsCount; AlphaSentenceCoordinate = (WordsIndex ^2+WordsCode^2+WordIndexInSentence^2)^1/2;
EuclidianSentenceDistance = (WordsCount^2 + Length^2 + AlphaSentenceCoordinate^2)^1/2
없음 모든 문장 P [길이와 같은 삼차원 공간에서 가리 키도록 변형 될 수있다 , Words, AlphaCoordinate]. 거리가 있으면 문장을 비교하고 분류 할 수 있습니다.
제가 생각하기에 이상적인 방법은 아니지만 아이디어를 보여 드리고자합니다.
import math
def calc_word_alpha_center(word):
chars_index = 0;
chars_codes = 0;
for index, char in enumerate(word):
chars_index += index
chars_codes += ord(char)
chars_count = len(word)
index = chars_index/len(word)
code = chars_codes/len(word)
return (index, code)
def calc_alpha_distance(words):
word_chars_index = 0;
word_code = 0;
word_index = 0;
for index, word in enumerate(words):
point = calc_word_alpha_center(word)
word_chars_index += point[0]
word_code += point[1]
word_index += index
chars_index = word_chars_index/len(words)
code = word_code/len(words)
index = word_index/len(words)
return math.sqrt(math.pow(chars_index, 2) + math.pow(code, 2) + math.pow(index, 2))
def calc_sentence_euclidean_distance(sentence):
length = len(sentence)
words = sentence.split(" ")
words_count = len(words)
alpha_distance = calc_alpha_distance(words)
return math.sqrt(math.pow(length, 2) + math.pow(words_count, 2) + math.pow(alpha_distance, 2))
sentence1 = "a great game"
sentence2 = "A great game"
distance1 = calc_sentence_euclidean_distance(sentence1)
distance2 = calc_sentence_euclidean_distance(sentence2)
print(sentence1)
print(str(distance1))
print(sentence2)
print(str(distance2))
콘솔 출력
a great game
101.764433866
A great game
91.8477000256
메신저 혼란스러워 ... 내가 가지고있는 예제를 사용하여 계산을 할 수 있습니까? 예 : https://stackoverflow.com/questions/17053459/how-to-transform-a-text-to-vector – xx4xx4
코드 샘플을 추가했습니다. 당신은 그것으로 놀고 좋은 품질의 기능을 얻을 수 있습니다. 왜냐하면 지금은 함수가 char 등록자와 같은 사소한 변경에 신속하게 민감하다는 것을 알기 때문입니다. – slesh
내가 코드를 읽은하지만 난 할 노력하고있어로부터 다른 생각 ... 는 생각한다 : "위대한 게임" 레이블이없는 문장 : "아주 가까이 게임" I 교육 문장 두 문장 사이의 유클리드 거리를 계산하고 싶습니다. from iv'e read 각 문장을 이전의 코멘트에있는 링크와 마찬가지로 바이너리로 변환해야합니다 ... – xx4xx4
그것은 당신이 문장은 '유클리드 거리'에 의해 뜻 불분명합니다. 어떤 종류의 거리라도 얻으려면 몇 가지 인코딩을 수정해야합니다. 예를 들어 카운트 벡터, 바이너리 버전 또는 tfidf 벡터를 사용할 수 있습니다. –
[link] (https://i.stack.imgur.com/PrqAF.png)의 훈련 데이터가 있고 KNN을 사용하여 "매우 가까운 게임"이라는 문장을 분류해야한다고 가정 해 봅시다. – xx4xx4
이 데이터는 문장 문자열을 가지고 있습니다. 앞서 언급했듯이 벡터화하는 방법은 여러 가지가 있습니다. –