当我尝试使用具有一项功能的数据集时出现Phpml \ Exception \ LibsvmCommandException

时间:2019-01-05 20:25:16

标签: php laravel laravel-5 logistic-regression

我正在使用php-ai/php-ml库将回归平台构建为云基础服务。

当我上传具有2个功能的数据集时,人工智能可以正常工作。我没有错误,也得到了预期的输出。但是,当我上传仅具有1个功能的数据集时,会出现此错误:

  

Phpml \ Exception \ LibsvmCommandException:vendor \ php-ai \ php-ml \ bin \ libsvm \ svm-train.exe -s 3 -t 2 -c 10 -n 0.500000 -d 3 -r 0 -p 0.100000- m 100.000000 -e 0.001000 -h 1 -b 0,原因:第1行输入格式错误

当我尝试使用分类器训练AI时,会抛出此错误(请注意,我将分类器存储在数据库中以重用其训练后的状态):

$classifier->train($split->getTrainSamples(), $split->getTrainLabels());

这是我的完整代码段:

public function trainOrTest(Request $request) {

    $csvFile = $request->file('dataframe');

    if(isset($csvFile) && isset($request->features) && isset($request->ainame)) {

        $regression = $this->getClassifierByName($request->ainame);

        $dataset    = new CsvDataset($csvFile, $request->features, true);

        $classifier = !isset($regression) ? new SVR(Kernel::RBF, 3, 0.1, 10) : unserialize($regression->classifier);

        $display    = [];
        $correct    = 0;
        $incorrect  = 0;

        if(!isset($request->predict)) {

            $split      = new StratifiedRandomSplit($dataset);
            $classifier->train($split->getTrainSamples(), $split->getTrainLabels());
            $predicted  = $classifier->predict($split->getTestSamples());

            foreach ($predicted as &$target) $target = round($target, 0);
            $score = round((float)Accuracy::score($split->getTestLabels(), $predicted)) * 100;

            foreach($split->getTestSamples() as $key => $sample)
                $display[] = (object) ['output' => $predicted[$key], 'input' => $sample];

            for($i = 0; $i <= count($predicted) -1; $i++) {
                if($predicted[$i] == $split->getTestLabels()[$i]) {
                    $correct++;
                }
                else {
                    $incorrect++;
                }
            }

        }
        else {

            $predicted  = $classifier->predict($dataset->getSamples());

            foreach ($predicted as &$target) $target = round($target, 0);
            $score = round((float)Accuracy::score($dataset->getTargets(), $predicted)) * 100;

            foreach($dataset->getSamples() as $key => $sample)
                $display[] = (object) ['output' => $predicted[$key], 'input' => $sample];

            for($i = 0; $i <= count($predicted) -1; $i++) {
                if($predicted[$i] == $dataset->getTargets()[$i]) {
                    $correct++;
                }
                else {
                    $incorrect++;
                }
            }

        }

        # View Data

        $stats = [['y' => $correct, 'label' => 'Correct'], ['y' => $incorrect, 'label' => 'Incorrect']];

        $viewVar = (object) [
            'results'   => $display,
            'score'     => $score . '%',
            'correct'   => $correct,
            'incorrect' => $incorrect
        ];

        if(!isset($regression)) {
            # Save the AI
            $this->storeClassifier(serialize($classifier), $request->ainame);
        }
        else {
            # Update the AI
            $this->updateClassifier(serialize($classifier), $request->ainame);
        }

        # Store the compile statistics
        $this->storeCompile($score, $correct, $incorrect, $this->getClassifierByName($request->ainame)->id);

        # Return view
        return view('home')->with('prediction', $viewVar)->with('statNew', $stats);

    }

    return back()->withInput();

}

这是我的CSV文件,当我运行此功能时,该文件似乎工作正常:

"SibSp","Parch","Survived",
"1", "1", "1",
"3", "3", "1",
"4", "1", "0"
"4", "0", "1",
"5", "2", "0"
"3", "1", "0",
"2", "2", "1",
"0", "0", "1",
"1", "1", "1",
"3", "3", "1",
"4", "1", "0"
"4", "0", "1",
"5", "2", "0"
"3", "1", "0",
"2", "2", "1",
"0", "0", "1"

这是导致此功能错误的CSV文件:

"sentence","language"
"Hello, do you know what time the movie is tonight?","english"
"I am calling to make reservations","english"
"I would like to know if it is at all possible to check in","english"
"What time does the swimming pool open?","english"
"Where is the games room?","english"
"We must call the police.","english"
"How many pupils are there in your school?","english"
"Twenty litres of unleaded, please.","english"
"Is it near here?","english"
"Do I have to change?","english"
"Which counter do I go to to change money?","english"
"I would like two postcards, please.","english"
"There was a big explosion.","english"
"Where is the television room?","english"
"Where's the nearest railway station?","english"
"The storms caused flooding.","english"
"Where is the duty free shop?","english"
"I witnessed it happening.","english"
"I would like two postcards, please.","english"
"How about going to the cinema?","english"
"Où est la boulangerie?","french"
"Je voudrais une boîte de chocolats.","french"
"Y a-t-il un autre hôtel près d'ici?","french"
"Vérifiez la batterie, s'il vous plaît.","french"
"La banque ouvre à quelle heure?","french"
"Est-ce que je peux l'écouter?","french"
"Vous devez faire une déclaration de perte.","french"
"Combien d'élèves y a-t-il dans votre collège?","french"
"Est-ce qu-il y a un car qui va à l'aéroport?","french"
"Nous voudrions rester jusqu'à dimanche prochain.","french"
"Je ne comprends pas.","french"
"Voici une ordonnance pour des comprimés.","french"
"Je n'ai qu'un billet de cinquante francs.","french"
"À quelle heure commence la dernière séance?","french"
"Faut-il changer?","french"
"Il n'avait pas la priorité.","french"
"Puis-je prendre les places à l'avance?","french"
"Elle a trente et un ans.","french"
"Pour aller à la pharmacie, s'il vous plaît?","french"
"Voulez-vous me peser ce colis, s'il vous plaît.","french"
"Ciao, ti va di andare al cinema?","italian"
"Mi sai dire quando aprono il negozio?","italian"
"Quando c'è l'ultimo autobus?","italian"
"Lo posso provare addosso?","italian"
"C'è una presa elettrica per il nostro caravan?","italian"
"Vorrei una scatola di cioccolatini.","italian"
"Non e' stata colpa mia.","italian"
"Sono molto scarso in fisica.","italian"
"Vorrei del vino bianco.","italian"
"Me lo puo' spiegare, per piacere.","italian"
"Vorrei chiamare a spese del ricevente.","italian"
"Ho perso il mio passaporto.","italian"
"Posso avere i biglietti del cinema per favore?","italian"
"Vorrei un pacchetto di biscotti.","italian"
"Quali materie studi (studia)?","italian"
"Quanto frequenti sono gli autobus?","italian"
"A che ora sara' la prossima raccolta?","italian"
"Vorrei prenotare un posto.","italian"
"Per favore, quanto costa spedire una lettera in Germania?","italian"
"Mio padre e' un programmatore.","italian"

为什么这会导致错误?我已经调试了代码,以确保$request->features的值保持为1,但是尝试找出其他值没有运气。

0 个答案:

没有答案