6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPからPYTHONに配列を渡してnumpy処理して結果を得る

6
Last updated at Posted at 2016-09-07

参考
http://stackoverflow.com/questions/25845600/passing-php-array-to-python-doesnt-work

   $numbers = [1, 2, 3, 4, 5];
   $pythonScript = "./test.py";
   $cmd = array("python", $pythonScript, escapeshellarg(json_encode($numbers)));
   $cmdText = implode(' ', $cmd);
   
   echo "Running command: " . $cmdText . "\n";
   $result = shell_exec($cmdText);
  var_dump(json_decode($result, true));
   # -*- coding: utf-8 -*-                                                                                                                                                                                                                                                     
import numpy as np
import sys, json

try:
    data = np.array(json.loads(sys.argv[1]))
    std = np.std(data)
    avg = np.mean(data)
    min = np.min(data)
    max = np.max(data)
    result = {'std':std, 'avg':avg, 'min':min, 'max':max}

    print json.dumps(result)
except Exception as e:
    print str(e)
       
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?