목표는 감정 분류입니다. 3 xlsx 파일을 열고 읽고 gensim.doc2vec 메서드로 처리하며 SGDClassificator로 분류하는 단계입니다. this code on doc2vec을 반복 해보십시오.xlsx 파일의 Readlines 함수가 적합하지 않음
print 'length of pos_reviews is %s' % len(pos_reviews)
>>> length of pos_reviews is 1
파일은 상응 18, 1221 및 2203 RAWS를 포함 : 파이썬 2.7
with open('C:/doc2v/trainpos.xlsx','r') as infile:
pos_reviews = infile.readlines()
with open('C:/doc2v/trainneg.xlsx','r') as infile:
neg_reviews = infile.readlines()
with open('C:/doc2v/unsup.xlsx','r') as infile:
unsup_reviews = infile.readlines()
그러나 결과 목록이 그들이 것으로 예상된다하지 않는 것을 밝혀졌다. 목록에 같은 수의 요소가 있다고 생각했습니다.
다음 단계는 모든 문장을 연결하는 것입니다.
y = np.concatenate((np.ones(len(pos_reviews)), np.zeros(len(neg_reviews))))
x_train, x_test, y_train, y_test = train_test_split(np.concatenate((pos_reviews, neg_reviews)), y, test_size=0.2)
X-기차, X-시험이 분할 후
y_train = [0.]
y_test = [1.]
은 모든 문장이 레이블을 느끼는 동안 그들이해야로서 문장의 목록입니다 때 상황에 이르게 :
def labelizeReviews(reviews, label_type):
labelized = []
for i,v in enumerate(reviews):
label = '%s_%s'%(label_type,i)
labelized.append(LabeledSentence(v, [label]))
return labelized
x_train = labelizeReviews(x_train, 'TRAIN')
x_test = labelizeReviews(x_test, 'TEST')
unsup_reviews = labelizeReviews(unsup_reviews, 'UNSUP')
가
the numpy documentation으로 작성된 것과 같이 배열의 크기는 동일해야합니다. 그러나 더 큰 파일을 18 줄로 줄이면 아무 것도 바뀌지 않습니다. 포럼에서 검색 한 결과 noone도 비슷한 오류가 발생했습니다. 나는 무엇이 잘못되었는지, 어떻게 고쳐야하는지 내 머리를 부러 뜨 렸습니다. 도와 주셔서 감사합니다!
나는이 사이트에서 한 번도 인용되지 않은 [link] (https://automatetheboringstuff.com/) '에 놀랐습니다. readlines() 메소드는 Excel 파일 작업을 권장했습니다. – Talka
그러면 list.append 메서드를 나중에 적용 할 수 있도록 목록 유형의 객체를 반환하는 모듈 메서드를 알 수 있습니까? 레이블 기능을 추가하는 코드를 편집했습니다. – Talka
@Talka [This code] (http://pastebin.com/rzi57bhE)는'xlrd' 모듈을 가진'python3'에서 작동합니다. «지루한 것들 자동화하기»»책 모듈'openpyxl'이 설명되어 있습니다. 이 모듈은 MS Office 2007 파일 ('.xlsx')에서만 작동합니다. –