gpt4 book ai didi

Python mlpy 文本分类

转载 作者:行者123 更新时间:2023-11-30 09:13:02 25 4
gpt4 key购买 nike

我是 mlpy 库的新手,正在寻找实现句子分类的最佳方法。我正在考虑使用 mply 基本感知器来做到这一点,但根据我的理解,它使用预定义的向量大小,但我需要在机器学习时动态增加向量的大小,因为我不想创建一个巨大的向量(所有英语单词)。我实际上需要做的是获取句子列表并从中构建分类器向量,然后当应用程序获得新句子时,它将尝试自动将其分类到其中一个标签(监督学习)。

任何想法、想法和例子都会非常有帮助,

谢谢

最佳答案

  1. 如果你事先有了所有的句子,你可以准备一个列表单词(删除停用词)将每个单词映射到一个特征。尺寸向量的值就是字典中的单词数。

  2. 一旦有了它,您就可以训练感知器了。

看看我的代码,其中我在 Perl 中进行了映射,然后在 matlab 中进行了感知器实现,以了解它是如何工作的,并在 python 中编写了类似的实现

准备词袋模型(Perl)

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)

我不想再次用 Python 编写相同的代码,但这应该为您提供如何继续的指导

关于Python mlpy 文本分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24838422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com