MachineLearningHomework1: LinearRegresshion (python)

算是自己整个完成的,没有参考别人的,留念。

import numpy as np
import pandas as pd
import math
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import pickle

#读取数据
data = pd.read_csv(r'G:\Code\Python\untitled\ML_wu_homework\machine-learning-ex1\ex1\ex1data1.txt', \
                   header=None, names=['Population', 'Profit'])

#数据的预处理
x = data['Population']
x = np.array(x)

#x = preprocessing.scale(x)
y = data['Profit']
y_raw = np.array(y)

#留出test数据集
test_num = int(math.ceil((len(x) * 0.2)))
x_test = x[-test_num:]
y_test = y[-test_num:]
x = x[:-test_num]
y = y[:-test_num]

#划分数据集
x_train, x_validation, y_train, y_validation = train_test_split(x, y, test_size=0.25)

#线性回归部分
clf = LinearRegression(n_jobs=-1)

clf.fit(x_train.reshape(-1, 1), y_train)

#保存结果,以免每次计算
with open('homework1.pickle', 'wb') as wf:
    pickle.dump(clf, wf)
with open('homework1.pickle', 'rb') as rf:
     clf = pickle.load(rf)

#输出拟合的r^2值
r2_train = clf.score(x_train.reshape(-1, 1), y_train)
print('train r2 is: ' + str(r2_train))

r2_validation = clf.score(x_validation.reshape(-1, 1), y_validation)
print('validation r2 is: ' + str(r2_validation))

r2_test = clf.score(x_test.reshape(-1, 1), y_test)
print('test r2 is: ' + str(r2_validation))

#画图,看看拟合结果
plt.figure(num=1)
y_train_pre = clf.predict(x_train.reshape(-1, 1))
plt.plot(x_train, y_train_pre, 'g')

plt.scatter(x_train, y_train)
plt.plot()

plt.figure(num=2)
y_validition_pre = clf.predict(x_validation.reshape(-1, 1))
plt.plot(x_validation, y_validition_pre, 'r')
plt.scatter(x_validation, y_validation)

plt.plot()
plt.show()

#输出拟合出来的系数
print('Estimated coefficients for the linear regression problem is: ')
print(clf.coef_)
print('Independent term in the linear model is: ')
print(clf.intercept_)







i

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用boost::python调用C结构体需要以下几个步骤: 1. 定义C结构体 示例代码如下: ``` typedef struct { int val1; float val2; char str1[20]; } MyStruct; ``` 2. 在Python中导入对应的C库并使用boost::python定义Python模块 ``` #include <boost/python.hpp> #include "mylibrary.h" using namespace boost::python; BOOST_PYTHON_MODULE(mymodule) { class_<MyStruct>("MyStruct") .def_readwrite("val1", &MyStruct::val1) .def_readwrite("val2", &MyStruct::val2) .def_readwrite("str1", &MyStruct::str1) ; } ``` 上述代码导入了C库mylibrary.h,并定义了Python模块mymodule。在Python中,可以使用以下代码导入该模块并创建MyStruct对象: ``` import mymodule mystruct = mymodule.MyStruct() mystruct.val1 = 10 mystruct.val2 = 3.14 mystruct.str1 = "hello world" ``` 3. 在C++代码中使用boost::python调用C结构体 ``` #include <boost/python.hpp> #include "mylibrary.h" int main() { Py_Initialize(); object main_module(( handle<>(borrowed(PyImport_AddModule("__main__"))))); object main_namespace = main_module.attr("__dict__"); try { exec_file("my_python_file.py", main_namespace, main_namespace); MyStruct my_struct; my_struct.val1 = 2; my_struct.val2 = 4.56; strcpy(my_struct.str1, "hello"); PyObject* pStruct = boost::python::incref(boost::python::ptr(&my_struct)); main_namespace["my_function"](pStruct); } catch (error_already_set) { PyErr_Print(); } Py_Finalize(); return 0; } ``` 上述代码中,首先使用Py_Initialize()初始化Python环境,然后使用exec_file函数执行Python脚本。在Python脚本中,可以使用MyStruct对象和其他Python对象一样来进行操作。最后,在C++中创建MyStruct对象并将其转换为PyObject*,然后调用Python函数。在Python函数中,可以使用boost::python获取MyStruct对象并进行操作。 综上所述,使用boost::python调用C结构体需要对C结构体进行定义,使用boost::python定义Python模块并在C++中调用Python函数,同时需要注意C++和Python对象之间的转换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值