사전에 모든 문장이있는 경우, 단어 (중지 단어 제거)지도의 목록을 준비 할 수 있습니다 모든 단어를 기능으로 가져옵니다. 벡터의 크기 은 사전에있는 단어의 수입니다.
일단, 당신은 퍼셉트론을 훈련시킬 수 있습니다.
은 내가 한 펄의 매핑이 어떻게 작동하는지 이해하고 단어 모델의 가방을 준비 파이썬
에서 비슷한 구현을 작성하는 MATLAB에서 구현을 퍼셉트론 다음에있는 내 코드에서보세요 (펄)
use warnings;
use strict;
my %positions =();
my $n = 0;
my $spam = -1;
open (INFILE, "q4train.dat");
open (OUTFILE, ">q4train_mod.dat");
while (<INFILE>) {
chomp;
my @values = split(' ', $_);
my %frequencies =();
for (my $i = 0; $i < scalar(@values); $i = $i+2) {
if ($i==0) {
if ($values[1] eq 'spam') {
$spam = 1;
}
else {
$spam = -1;
}
}
else {
$frequencies{$values[$i]} = $values[$i+1];
if (!exists ($positions{$values[$i]})) {
$n++;
$positions{$values[$i]} = $n;
}
}
}
print OUTFILE $spam." ";
my @keys = sort { $positions{$a} <=> $positions{$b} } keys %positions;
foreach my $word (@keys) {
if (exists ($frequencies{$word})) {
print OUTFILE " ".$positions{$word}.":".$frequencies{$word};
}
}
print OUTFILE "\n";
}
close (INFILE);
close (OUTFILE);
open (INFILE, "q4test.dat");
open (OUTFILE, ">q4test_mod.dat");
while (<INFILE>) {
chomp;
my @values = split(' ', $_);
my %frequencies =();
for (my $i = 0; $i < scalar(@values); $i = $i+2) {
if ($i==0) {
if ($values[1] eq 'spam') {
$spam = 1;
}
else {
$spam = -1;
}
}
else {
$frequencies{$values[$i]} = $values[$i+1];
if (!exists ($positions{$values[$i]})) {
$n++;
$positions{$values[$i]} = $n;
}
}
}
print OUTFILE $spam." ";
my @keys = sort { $positions{$a} <=> $positions{$b} } keys %positions;
foreach my $word (@keys) {
if (exists ($frequencies{$word})) {
print OUTFILE " ".$positions{$word}.":".$frequencies{$word};
}
}
print OUTFILE "\n";
}
close (INFILE);
close (OUTFILE);
open (OUTFILE, ">wordlist.dat");
my @keys = sort { $positions{$a} <=> $positions{$b} } keys %positions;
foreach my $word (@keys) {
print OUTFILE $word."\n";
}
퍼셉트론 구현 (matlab에)
,
clc; clear; close all;
[Ytrain, Xtrain] = libsvmread('q4train_mod.dat');
[Ytest, Xtest] = libsvmread('q4test_mod.dat');
mtrain = size(Xtrain,1);
mtest = size(Xtest,1);
n = size(Xtrain,2);
% part a
% learn perceptron
Xtrain_perceptron = [ones(mtrain,1) Xtrain];
Xtest_perceptron = [ones(mtest,1) Xtest];
alpha = 0.1;
%initialize
theta_perceptron = zeros(n+1,1);
trainerror_mag = 100000;
iteration = 0;
%loop
while (trainerror_mag>1000)
iteration = iteration+1;
for i = 1 : mtrain
Ypredict_temp = sign(theta_perceptron'*Xtrain_perceptron(i,:)');
theta_perceptron = theta_perceptron + alpha*(Ytrain(i)-Ypredict_temp)*Xtrain_perceptron(i,:)';
end
Ytrainpredict_perceptron = sign(theta_perceptron'*Xtrain_perceptron')';
trainerror_mag = (Ytrainpredict_perceptron - Ytrain)'*(Ytrainpredict_perceptron - Ytrain)
end
Ytestpredict_perceptron = sign(theta_perceptron'*Xtest_perceptron')';
testerror_mag = (Ytestpredict_perceptron - Ytest)'*(Ytestpredict_perceptron - Ytest)
나는 다시 파이썬에서 같은 일을 코딩하고 싶지 않아 그러나 이것은 당신에게
@Dua 대답을 주셔서 감사 진행 방법에 대한 방향을 제공해야합니다. 문제는 분류 할 문장에 새 단어를 추가 할 때 기능 (단어)의 수를 늘려야한다는 것입니다. 파이썬에서 nltk 및 mlpy로 이미 작성한 라이브러리를 사용하면서 가능한 한 최소 코드를 작성하면서 파이썬에서이를 구현하는 가장 좋은 방법을 찾고 있습니다. – DiSol
당신이 분류해야 할 문장에 새로운 단어가 생기면이 단어에 대한 정보가 없으므로 그 단어를 무시하는 것이 가장 안전합니다. 이것이 표준 분류가 이루어지는 방식입니다. 이 기사 http://www.nltk.org/book/ch06.html은 이론을 아주 잘 설명합니다 –
또한 도움이되는 답변을 upvote 기억하십시오! 고마워 –