그래서 naive bayes clasifier를 교육하려고합니다. 데이터 전처리의 문제가 많이 들어가서 지금 두 RDDs을 생산 :NaiveBayes는 별도의 교육 세트 및 pyspark를 사용한 데이터를 사용한 모델 교육
- Traininng 세트 : 스파 스 벡터들의 세트로 구성;
- 레이블 : 해당 모든 벡터에 대한 레이블 목록 (0,1).
# Train a naive Bayes model. model = NaiveBayes.train(training, 1.0)
그러나 "훈련"을 실행에서 파생 된 데이터 세트입니다 :
나는 이런 식으로 뭔가를 실행해야 파이썬 here에 대한 문서를 기반으로
def parseLine(line):
parts = line.split(',')
label = float(parts[0])
features = Vectors.dense([float(x) for x in parts[1].split(' ')])
return LabeledPoint(label, features)
data = sc.textFile('data/mllib/sample_naive_bayes_data.txt').map(parseLine)
. 내 질문은 txt 파일에서 데이터를로드하고 싶지 않아 스파 스 - 벡터 (RDD) 및 해당 레이블이있는 목록에 매핑 된 레코드 형태로 트레이닝 집합을 이미 만들었 기 때문에 어떻게해야합니까? 순진한 베이를 돌린다. 여기
내 코드의 일부입니다
# Function
def featurize(tokens_kv, dictionary):
"""
:param tokens_kv: list of tuples of the form (word, tf-idf score)
:param dictionary: list of n words
:return: sparse_vector of size n
"""
# MUST sort tokens_kv by key
tokens_kv = collections.OrderedDict(sorted(tokens_kv.items()))
vector_size = len(dictionary)
non_zero_indexes = []
index_tfidf_values = []
for key, value in tokens_kv.iteritems():
index = 0
for word in dictionary:
if key == word:
non_zero_indexes.append(index)
index_tfidf_values.append(value)
index += 1
print non_zero_indexes
print index_tfidf_values
return SparseVector(vector_size, non_zero_indexes, index_tfidf_values)
# Feature Extraction
Training_Set_Vectors = (TFsIDFs_Vector_Weights_RDDs
.map(lambda (tokens): featurize(tokens, Dictionary_BV.value))
.cache())
... 라벨은 1과 0의 단지 목록입니다. 나는 어떻게 든 labelpoint를 어떻게 든 사용해야한다고 이해하지만, 나는 혼란 스럽다. RDD는리스트가 아니고리스트는 레이블이있는 objets [i]를 만드는 방법을 생각해내는 단순한 것을 기대하고있다. 스파 스 - 벡터 [i], 해당 레이블 [i] 각각의 값 ... 어떤 아이디어를 결합합니까?