未知类型名称';PyModuleDef'; 我正在使用Python和C++集成,我在MaOS.< /P>上。
当我使用所有的未知类型名称';PyModuleDef'; 我正在使用Python和C++集成,我在MaOS.< /P>上。,python,c++,Python,C++,当我使用所有的PyObject方法时,它们似乎都能工作,但是PyModule方法似乎都不能工作 这是我的代码: #include <iostream> #include <cmath> #include <vector> #if PY_MAJOR_VERSION >= 3 #define PY3K #endif #ifdef __APPLE__ #include <Python/Python.h> #else #include <P
PyObject
方法时,它们似乎都能工作,但是PyModule
方法似乎都不能工作
这是我的代码:
#include <iostream>
#include <cmath>
#include <vector>
#if PY_MAJOR_VERSION >= 3
#define PY3K
#endif
#ifdef __APPLE__
#include <Python/Python.h>
#else
#include <Python.h>
#endif
#define space " "
#define epoch int(1000)
using namespace std;
double hypothesis(const std::vector<double>& b_val, double x){
return b_val[0] + b_val[1] * x;
}
std::vector<double> regression(const std::vector<double>& x, const std::vector<double>& y,
int epochs, double learning_rate){
if(!epochs) epochs = epoch;
double _m = 0;
double _b = 0;
std::vector<double> _values(2);
int N = x.size();
for(int i = 0; i < epochs; i++){
_values[0] = _b, _values[1] = _m;
double dm = 0;
double db = 0;
double cost = 0;
for(int j = 0; j < N; j++){
double p = hypothesis(_values, x[j]);
cost += pow(y[j] - p, 2);
dm += (-2.0 / N) * (x[j] * (y[j] - p));
db += (-2.0 / N) * (y[j] - p);
}
cost /= N;
_m = _m - (learning_rate * dm);
_b = _b - (learning_rate * db);
if ((i + 1) % 100 == 0)
std::cout << "Epoch: " << (i + 1) << " Cost: " << cost << std::endl;
}
std::vector<double> result(2);
result[0] = _m, result[1] = _b;
return result;
}
static PyObject * fit(PyObject * self, PyObject * args){
PyObject *x;
PyObject *y;
double learning_rate;
int epochs;
int N;
if(!PyArg_ParseTuple(args, "00di0i", &x, &y, &epochs, &learning_rate, &N)){
return nullptr;
}
std::vector<double> _x(N), _y(N);
for(int i = 0; i < N; i++){
_x[i] = PyFloat_AsDouble(PyList_GetItem(x, (Py_ssize_t)i));
_y[i] = PyFloat_AsDouble(PyList_GetItem(y, (Py_ssize_t)i));
}
std::vector<double> _result = regression(_x, _y, epochs, learning_rate);
PyObject *result = PyTuple_New(2);
for(int i = 0; i < 2; i++){
PyTuple_SetItem(result, i, PyFloat_FromDouble(_result[i]));
}
return Py_BuildValue("s", result);
}
static PyMethodDef linreg_methods[] = {
{"fit", (PyCFunction)fit, METH_VARARGS, "Linear Regression"},
{nullptr}
};
static PyModuleDef linear_regression = {
PyModuleDef_HEAD_INIT,
"linear_regression"
"Linear Regression",
-1,
linreg_methods
};
PyMODINIT_FUNC PyInit_linear_regression(void){
return PyModule_Create(&linear_regression);
}
#包括
#包括
#包括
#如果PY\u主要版本>=3
#定义PY3K
#恩迪夫
#苹果__
#包括
#否则
#包括
#恩迪夫
#定义空间“”
#定义历元整数(1000)
使用名称空间std;
双重假设(const std::vector&b_val,double x){
返回b_val[0]+b_val[1]*x;
}
标准::向量回归(常数标准::向量和x,常数标准::向量和y,
整数时代,双倍学习率){
如果(!epochs)epochs=epoch;
double _m=0;
double _b=0;
标准::向量_值(2);
int N=x.size();
for(int i=0;i std::cout您将需要确保包含的Python.h
来自Python3.x版本,并且您链接到相应的库,例如,在Linux上:
g++my_module.cpp-I/usr/include/python3.8/-lpython3.8
但在MacOS上,此文件将取决于您安装Python3的位置/方式。您应该能够使用定位Python.h
并找到Python3目录。问题是,我似乎没有Python3.x目录,只有python2.7目录。您知道如何才能找到Python3.x目录吗?我可以建议查找一下吗将Python作为一种替代C++函数映射到Python的方法?它是一个库,它封装了我个人推荐的C++扩展API,因为它能取出很多痛苦的样板安装代码,而且对于更大的代码库来说,它更干净。