2015-05-07 20 views
5

çalıştıran benim C++ kodu "t-testi" hesaplamak için bir piton işlevini arıyorum. aşağıdaki gibiPython Zamanı Hatası T-Testi

#include <iostream> 
#include "Python.h" 
#include "/usr/local/lib/python2.7/site-packages/numpy/core/include/numpy/arrayobject.h" 

#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 

int main(int argc, char** argv) 
{ 
    Py_Initialize(); 

    PyRun_SimpleString("import sys"); 
    PyRun_SimpleString("sys.path.append(\"PATH_TO_MOD\")"); 
    PyObject *pName = PyString_FromString("tmpPyth"); 
    PyObject *pModule = PyImport_Import(pName); 


    double arr[] ={9.74219, 10.2226, 8.7469, 8.69791, 9.96442, 9.96472, 9.37913, 9.75004}; 
    double arr1[] ={9.74219, 10.2226, 8.7469, 8.69791, 9.96442, 9.96472, 9.37913, 9.75004}; 

    PyObject *lst = PyList_New(8); 
    PyObject *lst1 = PyList_New(8); 
    // if (!lst) 
    //  return NULL; 
    for (int i = 0; i < 8; i++) { 
     PyObject *num = PyFloat_FromDouble(arr[i]); 
     PyObject *num1 = PyFloat_FromDouble(arr1[i]); 
     PyList_SET_ITEM(lst, i, num); 
     PyList_SET_ITEM(lst1, i, num1); 
    } 

    PyObject *pArgs = PyTuple_New(2); 
    PyTuple_SetItem(pArgs, 0, lst); 
    PyTuple_SetItem(pArgs, 1, lst1); 

    if (pModule != NULL) { 
     PyObject *pFunc = PyObject_GetAttrString(pModule, "blah"); 

     if(pFunc != NULL){ 
      PyObject_CallObject(pFunc, pArgs); 
     } 
    } 
    else 
     std::cout << "Module path provided may be wrong. Module not found.\n\n"; 
    return 0; 
} 

Ve piton modülü tanımlanır: şu şekildedir: işlev çağrısı yapıldığında

import numpy 
import scipy 
import matplotlib 

from scipy import stats 
def blah(baseline, follow_up): 
    paired_sample = stats.ttest_rel(baseline , follow_up) 
    print "The t-statistic is %.3f and the p-value is %.3f." % paired_sample 

Şimdi ben bu çalıştırmayı denediğinizde aşağıdaki çalışma zamanı özel olsun:

/usr/local/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/stats/stats.py:3458: RuntimeWarning: invalid value encountered in divide 
    t = np.divide(dm, denom) 

Ama açıkça bir liste tanımlamak ve "t-testi" fonksiyonunu denemek ve yürütmek eğer sadece iyi çalışır. aşağıdaki gibi çalışan fonksiyon tanımı: Ben piton komut dosyasına geçirilen listeleri tanımlanmasında bazı hata yapma olduğumu varsayıyorum ama ne çözemiyorum

import numpy 
import scipy 
import matplotlib 

from scipy import stats 

    def blah(): 
     baseline = [9.74219, 10.2226, 8.7469, 8.69791, 9.96442, 9.96472, 9.37913, 9.75004] 
     follow_up = [9.94227,9.46763,8.53081,9.43679,9.97695,10.4285,10.159,8.86134] 
     paired_sample = stats.ttest_rel(baseline , follow_up) 
     print "The t-statistic is %.3f and the p-value is %.3f." % paired_sample 

. Herhangi bir yardım takdir edilecektir. daki c

+3

Orada açıklamanızı okuyarak bana atlar şey yok, bu yüzden sesi görünüyor. Sadece merak için, 'blah()' diye adlandırırsınız ve hata 'numpy.stats()' dır, 'blah()' 'a ulaşan değerler nelerdir? Temsillerini çıktılamak için 'print repr (baseline)' ve 'print repr (follow_up)' seçeneklerini kullanın. Dedi, neden hala Python 2 kullanıyorsunuz? En azından Numpy mevcut Pythons'ta mevcuttur. –

+0

Hmm, belki aptalca bir soru, ama ([SciPy gelen t-testleri] önceden var kullanmayı düşünün vermedi http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind.html) yeniden uygulamak yerine? – mtzl

cevap

0

arr1 ++ kod sıfıra dolayısıyla bölme, arr özdeştir. senin piton kodunda baseline ve follow_up farklı olduğu için bir hata alamadım.

Daha büyük diziler için bunları python listeleri aracılığıyla sıralamak istemezsiniz, bunun yerine dizileri doğrudan python'a gönderin.

-- pyfromc.cc -- 
#include <iostream> 
#include <Python.h> 

int main(int argc, char** argv) 
{ 
    Py_Initialize(); 

    PyRun_SimpleString("import sys; sys.path.append('.')"); 
    // PyRun_SimpleString("print '\\n'.join(sys.path)"); 
    PyObject *pName = PyString_FromString("ttest"); 
    PyObject *pModule = PyImport_Import(pName); 

    double arr[] ={9.74219, 10.2226, 8.7469, 8.69791, 9.96442, 9.96472, 9.37913, 9.75004}; 
    double arr1[] ={9.94227,9.46763,8.53081,9.43679,9.97695,10.4285,10.159,8.86134}; 

    PyObject *pArgs = PyTuple_New(3); 
    PyTuple_SetItem(pArgs, 0, PyLong_FromLong(8)); 
    PyTuple_SetItem(pArgs, 1, PyLong_FromVoidPtr(arr)); 
    PyTuple_SetItem(pArgs, 2, PyLong_FromVoidPtr(arr1)); 

    if (pModule != NULL) { 
     PyObject *pFunc = PyObject_GetAttrString(pModule, "blahptr"); 

     if(pFunc != NULL){ 
      PyObject_CallObject(pFunc, pArgs); 
     } 
    } 
    else 
     std::cout << "Module path provided may be wrong. Module not found.\n\n"; 
    return 0; 
} 

ve piton tarafında

:

:

derlemek ve Mac OS XI kod çalıştırmak için
-- ttest.py -- 
from ctypes import POINTER, c_double, cast 
c_double_p = POINTER(c_double) 

import numpy as np 
from scipy import stats 

def blahptr(n, baseline_ptr, follow_up_ptr): 
    baseline = np.ctypeslib.as_array(cast(baseline_ptr, c_double_p), shape=(n,)) 
    follow_up = np.ctypeslib.as_array(cast(follow_up_ptr, c_double_p), shape=(n,)) 
    return blah(baseline, follow_up) 

def blah(baseline, follow_up): 
    paired_sample = stats.ttest_rel(baseline , follow_up) 
    print "The t-statistic is %.3f and the p-value is %.3f." % paired_sample 
    return paired_sample 

aşağıdaki kullanılan bunu yapmak için yukarıdaki kodunuzu modifiye ettik

$ PYENV=/path/to/python/env 
$ c++ pyfromc.cc -I$PYENV/include/python2.7 -L$PYENV/lib -lpython2.7  
$ PYTHONHOME=$PYENV DYLD_LIBRARY_PATH=$PYENV/lib ./a.out 
The t-statistic is -0.187 and the p-value is 0.857. 

Yürütülebilir dosya ile aynı satırdaki ortam değişkenlerini ayarlayarak, bash yorumlayıcısı bunları yalnızca komutun süresi boyunca değil, diğer komutlar için ayarlar.

İlgili konular