提交 fe2970bd 编写于 作者: sahduashufa's avatar sahduashufa

1.0

上级 aa81b524
......@@ -6,27 +6,99 @@
## Edge : 一个开源的科学计算引擎
[![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000) ](https://github.com/AllenZYJ/Edge-Computing-Engine/blob/add-license-1/LICENSE)![](https://img.shields.io/badge/Bulid-Passed-green.svg)
目前实现的:
[![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000) ](https://github.com/AllenZYJ/Edge-Computing-Engine/blob/add-license-1/LICENSE)![](https://img.shields.io/badge/Bulid-Version1.0-green.svg)
> 项目开始日期 : 2019/10/01
> 目前项目总代码 : 709 行
>
> 测试代码 : 810 行
>
> 测试环境:
>
> MacBook Pro
>
> 编译器环境:
>
> Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
> Apple LLVM version 10.0.1 (clang-1001.0.46.4)
> Target: x86_64-apple-darwin18.7.0
> Thread model: posix
>
> 实现的:
如何安装和运行基于Edeg的逻辑回归demo.
`git clone git@github.com:AllenZYJ/Edge-Computing-Engine.git`
进入到clone 之后的目录
cd to this dir
编译demo入口程序
`g++ main.cpp -o main`
`'./main'`
输出最终损失和参数迭代结果.
-----------split-line-----------
2.79955
0.36431
-0.451694
epoch: 100 error: 6.05895
-----------split-line-----------
0.009167(sum of loss)
目前实现的程序接口
Matrix API:
- [x] Create a matrix : create(row,cols)
- [x] Change the element for matrix void move_ele(int &ele1, int &ele2)
- [x] Matrix1+Matrix2 : Matrix add(Matrix mid1,Matrix mid2,int flag=1)
- [x] Matrix read_csv(string &file_path)读取格式化文件(csv),返回一个自动计算长度的矩阵.
- [ ] Create a matrix : create(row,cols)开辟一个矩阵结构的内存,元素初值为0;
- [x] Change the element for matrix void move_ele(int &ele1, int &ele2),修改某一个位置的元素的值.
- [x] Matrix1+Matrix2 : Matrix add(Matrix mid1,Matrix mid2,int flag=1),矩阵加和操作接口,可选位运算加速.
- [x] Flag is how to compete the ele ,default 1 ,bitwise operation(位运算加速).
- [x] Matrix1-Matrix2 : Matrix subtract(Matrix mid1,Matrix mid2)
- [x] Matrix1*Matrix2 : Matrix mul(Matrix mid1,Matrix mid2)
- [x] Matrix1*n : Matrix times_mat(int times,Matrix mid1)
- [x] Matrix1's Transposition : Matrix get_T(Matrix mid1)
- [x] Mul(matrix1,matrix2)
- [x] double* flatten(Matrix mid1) : Return a flattened array.
- [x] Matrix matrix_rs(Matrix mid1,int rs_row,int rs_col)
- [x] double matrix_sum(Matrix mid1)
- [x] double matrix_mean(Matrix mid1)
- [x] Matrix appply(Matrix mid1,Matrix mid2,int axis = 0)
- [x] Matrix iloc(Matrix mid1,int start_x=0,int end_x=0,int start_y=0,int end_y=0)
- [x] Matrix1's Transposition : Matrix get_T(Matrix mid1)矩阵转置
- [x] Mul(matrix1,matrix2)矩阵乘积(完整数学定义).
- [x] double* flatten(Matrix mid1) : Return a flattened array.矩阵展开
- [x] Matrix matrix_rs(Matrix mid1,int rs_row,int rs_col) 矩阵的结构压缩
- [x] double matrix_sum(Matrix mid1)矩阵求和
- [x] double matrix_mean(Matrix mid1)均值
- [x] Matrix appply(Matrix mid1,Matrix mid2,int axis = 0)矩阵拼接
- [x] Matrix iloc(Matrix mid1,int start_x=0,int end_x=0,int start_y=0,int end_y=0)矩阵切片
- [x] Matrix mul_simple(Matrix mid1,Matrix mid2)为了贴合机器学习的需要,实现了矩阵对应元素相乘,请与传统意义的矩阵乘法区分开.
- [ ] 卷积神经网络定义(包括但不限于卷积核,池化层定义,自定义损失接口).
- [ ] 随机森林迭代过程封装.
- [ ] …...
## 演示:矩阵乘法
Matrix **A**
......@@ -60,7 +132,9 @@ To
| 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
- [x] double* flatten(Matrix mid1)
## 演示: 矩阵展开(flatten).
double* flatten(Matrix mid1)
| 1 | 2 | 3 |
| :--: | :--: | :--: |
......@@ -73,12 +147,10 @@ To
| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | :----------------- |
| | | | | | | | | Like numpy.flatten |
update 2019/11/20/00:06
apply API:
function:
## 演示: 邻接矩阵的参数定义:
​ Matrix appply(Matrix mid1,Matrix mid2,int axis = 0)
> 参数 axis=0 :
......@@ -104,13 +176,11 @@ function:
------
更新2019/11/18/00:12
## 更新2019/11/18/00:12
- [x] read_csv
- [x] read_csv 通过文件流读取逗号分隔符文件,返回一个自动计算长度的矩阵.
- [x] return a matrix
CSV head :
例如 CSV's head :
| -0.017612 | 14.053064 | 0 |
| --------- | --------- | ---- |
......@@ -126,7 +196,8 @@ function:
## demo:
## Logistic Regression demo:
```c
#include<iostream>
......@@ -145,100 +216,46 @@ clock_t start, stop;
double duration;
int main()
{
string path = "./data.csv";
welcome();
string path = "./new_data2.csv";
Matrix data = read_csv(path);
for(int index_x = 0;index_x<data.row;index_x++)
{
for(int index_y=0;index_y<data.col;index_y++)
{
cout<<data.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
cout<<"-----------split line-----------"<<endl;
welcome();
Matrix a = CreateMatrix(4,3);
double a_ = e_sigmoid(1);
cout<<a_<<endl;
change_va(a,1,2,1);
change_va(a,0,1,7);
change_va(a,0,2,2);
change_va(a,1,1,3);
change_va(a,3,2,11);
start = clock();
Matrix applyed_ma = appply(a,a,1);
for(int index_x = 0;index_x<applyed_ma.row;index_x++)
{
cout<<"|";
for(int index_y=0;index_y<applyed_ma.col;index_y++)
{
cout<<applyed_ma.matrix[index_x][index_y]<<"|";
}
cout<<endl;
}
cout<<"matrix sum: "<<matrix_sum(a)<<endl;
cout<<"matrix mean: "<<matrix_mean(a)<<endl;
cout<<"matrix T: "<<endl;
cout<<"--------split---------"<<endl;
Matrix b = get_T(a);
for(int index_x = 0;index_x<b.row;index_x++)
{
for(int index_y=0;index_y<b.col;index_y++)
{
cout<<b.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
cout<<"-----------split line-----------"<<endl;
cout<<"matrix*n"<<endl;
Matrix c = times_mat(8,b);
for(int index_x = 0;index_x<c.row;index_x++)
{
for(int index_y=0;index_y<c.col;index_y++)
{
cout<<c.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
cout<<"-----------split line-----------"<<endl;
cout<<"matrix*matrix"<<endl;
Matrix d =mul(c,get_T(c));
for(int index_x = 0;index_x<d.row;index_x++)
{
for(int index_y=0;index_y<d.col;index_y++)
{
cout<<d.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
cout<<"-----------split line-----------"<<endl;
Matrix rs = matrix_rs(a,6,5);
for(int index_x = 0;index_x<rs.row;index_x++)
Matrix bais = CreateMatrix(data.row,1);
data = appply(data,bais,1);
Matrix y = iloc(data,0,0,3,4);
Matrix x_1 = iloc(data,0,0,0,3);
Matrix x_2 = get_T(x_1);
double alpha = 0.002;
int max_epoch = 100;
Matrix weight = CreateMatrix(3,1);
change_va(weight,0,0,1);
change_va(weight,1,0,1);
change_va(weight,2,0,1);
int epoch = 0;
for(epoch = 0;epoch<=max_epoch;epoch++)
{
for(int index_y=0;index_y<rs.col;index_y++)
{
cout<<rs.matrix[index_x][index_y]<<" ";
}
cout<<endl;
cout<<"-----------split-line-----------"<<endl;
Matrix temp_mul = mul(x_1,weight);
Matrix h =e_sigmoid(temp_mul);
Matrix error = subtract(y,h);
Matrix temp_update = mul(x_2,error);
Matrix updata = add(weight,times_mat(alpha,temp_update),0);
cout_mat(weight);
cout<<"epoch: "<<epoch<<" error: "<<matrix_sum(error)<<endl;
cout<<"-----------split-line-----------"<<endl;
}
times_mat(5,a);
stop = clock();
printf("%f\n", (double)(stop - start) / CLOCKS_PER_SEC);
printf("%f\n", (double)(stop - start) / CLOCKS_PER_SEC);
return 0;
}
```
Something :
> 1. Matrix'element is default 1
> 2. Dynamically allocate memory to prevent matrix from being too large
> 3. To save memory and delete later, use pointer to open up array space temporarily
> 4. if free please delete(matrix);
> 5. Api design like numpy or pandas
> 6. Talking is cheap u can get the code
> 7. welcome 🏃watched and star.
> 1. 矩阵元素默认为1
> 2. 使用位运算加速防止填充过大的数值,但是会损失一定精度,慎用.
> 3. 记得delete(matrix)在你使用完一个矩阵计算单元以后.
> 4. api接口更多的接近于pandas和numpy的使用习惯.
> 5. 更多的细节参见目前最新的代码
> 6. 欢迎star和关注.
>
------
......
<div align=center><img src="./picture/01.svg"/></div>
# Edge-Computing-Engine
## Edge : 一个开源的科学计算引擎
[![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000) ](https://github.com/AllenZYJ/Edge-Computing-Engine/blob/add-license-1/LICENSE)![](https://img.shields.io/badge/Bulid-Version1.0-green.svg)
> 项目开始日期 : 2019/10/01
> 目前项目总代码 : 709 行
>
> 测试代码 : 810 行
>
> 测试环境:
>
> MacBook Pro
>
> 编译器环境:
>
> Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
> Apple LLVM version 10.0.1 (clang-1001.0.46.4)
> Target: x86_64-apple-darwin18.7.0
> Thread model: posix
>
> 目前实现的:
How to install and run the demo for logistic regession:
`git clone git@github.com:AllenZYJ/Edge-Computing-Engine.git`
cd to this dir
`g++ main.cpp -o main`
`./main`
Matrix API:
- [x] Matrix read_csv(string &file_path)
- [x] Create a matrix : create(row,cols)
- [x] Change the element for matrix void move_ele(int &ele1, int &ele2)
- [x] Matrix1+Matrix2 : Matrix add(Matrix mid1,Matrix mid2,int flag=1)
- [x] Flag is how to compete the ele ,default 1 ,bitwise operation(位运算加速).
- [x] Matrix1-Matrix2 : Matrix subtract(Matrix mid1,Matrix mid2)
- [x] Matrix1*Matrix2 : Matrix mul(Matrix mid1,Matrix mid2)
- [x] Matrix1*n : Matrix times_mat(int times,Matrix mid1)
- [x] Matrix1's Transposition : Matrix get_T(Matrix mid1)
- [x] Mul(matrix1,matrix2)
- [x] double* flatten(Matrix mid1) : Return a flattened array.
- [x] Matrix matrix_rs(Matrix mid1,int rs_row,int rs_col)
- [x] double matrix_sum(Matrix mid1)
- [x] double matrix_mean(Matrix mid1)
- [x] Matrix appply(Matrix mid1,Matrix mid2,int axis = 0)
- [x] Matrix iloc(Matrix mid1,int start_x=0,int end_x=0,int start_y=0,int end_y=0)
## Demo: mat*mat
Matrix **A**
| 第1列 | 第2列 | 第3列 | 第4列 | 第5列 |
| ------- | ------- | ------- | ------- | ------- |
| 72.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 64.0000 | 0.0000 | 0.0000 | 0.0000 |
| 16.0000 | 8.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 56.0000 | 16.0000 | 32.0000 |
| 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
MAtrix **B**
| 第1列 | 第2列 | 第3列 | 第4列 | 第5列 | 第6列 |
| ------- | ------- | ------- | ------- | ------ | ------ |
| 72.0000 | 0.0000 | 16.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 64.0000 | 8.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 56.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 16.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 32.0000 | 0.0000 | 0.0000 |
To
| 第1列 | 第2列 | 第3列 | 第4列 | 第5列 | 第6列 |
| --------- | --------- | --------- | --------- | ------ | ------ |
| 5184.0000 | 0.0000 | 1152.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 4096.0000 | 512.0000 | 0.0000 | 0.0000 | 0.0000 |
| 1152.0000 | 512.0000 | 320.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 4416.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
## Demo : mat.flatten
double* flatten(Matrix mid1)
| 1 | 2 | 3 |
| :--: | :--: | :--: |
| 2 | 4 | 6 |
| 7 | 8 | 9 |
​ To
| 1 | 2 | 3 | 2 | 4 | 6 | 7 | 8 | 9 |
| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | :----------------- |
| | | | | | | | | Like numpy.flatten |
## Demo : apply nearly mat
​ Matrix appply(Matrix mid1,Matrix mid2,int axis = 0)
> if axis=0 :
| 0 | 7 | 2 |
| ---- | ---- | ---- |
| 0 | 3 | 1 |
| 0 | 0 | 0 |
| 0 | 0 | 11 |
| 0 | 7 | 2 |
| 0 | 3 | 1 |
| 0 | 0 | 0 |
| 0 | 0 | 11 |
------
> axis = 1:
| 0 | 7 | 2 | 0 | 7 | 2 |
| ---- | ---- | ---- | ---- | ---- | ---- |
| 0 | 3 | 1 | 0 | 3 | 1 |
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 11 | 0 | 0 | 11 |
- [x] read_csv
- [x] return a matrix
CSV head :
| -0.017612 | 14.053064 | 0 |
| --------- | --------- | ---- |
| -1.395634 | 4.662541 | 1 |
| -0.752157 | 6.53862 | 0 |
| -1.322371 | 7.152853 | 0 |
| 0.423363 | 11.054677 | 0 |
| 0.406704 | 7.067335 | 1 |
Get:
![](./picture/WX20191119-105411@2x.png)
## demo:
```c
#include<iostream>
#include<ctime>
#include<string>
#include <time.h>
#include <math.h>
#include <fstream>
#include"./matrix/matrix_def.h"
#include"./matrix/matrix_pro.h"
#include"./welcome/score_wel.cpp"
#include"./logistic/logistic_def.h"
#include"./file_pro/data_read.h"
using namespace std;
clock_t start, stop;
double duration;
int main()
{
welcome();
string path = "./new_data2.csv";
Matrix data = read_csv(path);
Matrix bais = CreateMatrix(data.row,1);
data = appply(data,bais,1);
Matrix y = iloc(data,0,0,3,4);
Matrix x_1 = iloc(data,0,0,0,3);
Matrix x_2 = get_T(x_1);
double alpha = 0.002;
int max_epoch = 100;
Matrix weight = CreateMatrix(3,1);
change_va(weight,0,0,1);
change_va(weight,1,0,1);
change_va(weight,2,0,1);
int epoch = 0;
for(epoch = 0;epoch<=max_epoch;epoch++)
{
cout<<"-----------split-line-----------"<<endl;
Matrix temp_mul = mul(x_1,weight);
Matrix h =e_sigmoid(temp_mul);
Matrix error = subtract(y,h);
Matrix temp_update = mul(x_2,error);
Matrix updata = add(weight,times_mat(alpha,temp_update),0);
cout_mat(weight);
cout<<"epoch: "<<epoch<<" error: "<<matrix_sum(error)<<endl;
cout<<"-----------split-line-----------"<<endl;
}
stop = clock();
printf("%f\n", (double)(stop - start) / CLOCKS_PER_SEC);
return 0;
}
```
Something :
> 1. Matrix'element is default 1
> 2. Dynamically allocate memory to prevent matrix from being too large
> 3. To save memory and delete later, use pointer to open up array space temporarily
> 4. if free please delete(matrix);
> 5. Api design like numpy or pandas
> 6. Talking is cheap u can get the code
> 7. welcome 🏃watched and star.
>
------
<div align = center><img src = './picture/星月.svg'></div>
个人小站:[极度空间](likedge.top)
作者邮箱:zk@likedge.top | edge@ibooker.org.cn
QQ:2533524298
\ No newline at end of file
-0.017612 14.053064 0
-1.395634 4.662541 1
\ No newline at end of file
......@@ -3,7 +3,22 @@
#include<math.h>
#include<iostream>
using namespace std;
double e_sigmoid(int z)
double get_sigmoid(double temp)
{
return 1/(1+exp(z));
// cout<<"temp"<<temp<<endl;
return 1/1+exp(0-temp);
}
Matrix e_sigmoid(Matrix mid1)
{
Matrix result = CreateMatrix(mid1.row,mid1.col);
for(int index_x = 0;index_x<mid1.row;index_x++)
{
for(int index_y=0;index_y<mid1.col;index_y++)
{
// cout<<"ex:"<<get_sigmoid(mid1.matrix[index_x][index_y])<<endl;
result.matrix[index_x][index_y]= 1.0/(1+exp(-mid1.matrix[index_x][index_y]));
}
}
return result;
}
文件已删除
......@@ -15,78 +15,32 @@ double duration;
int main()
{
welcome();
string path = "./new_data.csv";
string path = "./new_data2.csv";
Matrix data = read_csv(path);
cout<<"new"<<endl;
cout_mat(data);
Matrix y = iloc(data,0,0,2,3);
Matrix x = iloc(data,0,0,0,2);
cout_mat(y);
Matrix bais = CreateMatrix(data.row,1);
Matrix data_new = appply(data,bais,1);
cout_mat(data_new);
cout<<"shape: "<<data_new.row<<","<<data_new.col<<endl;
cout<<"-----------split line-----------"<<endl;
Matrix test_head_mat = CreateMatrix(5,4);
cout_mat(iloc(read_csv(path),2,19,2,3));
// Matrix data = read_csv(path);
// cout_mat(data);
cout<<"-----------split line-----------"<<endl;
Matrix a = CreateMatrix(4,3);
double a_ = e_sigmoid(1);
cout<<a_<<endl;
change_va(a,1,2,1);
change_va(a,0,1,7);
change_va(a,0,2,2);
change_va(a,1,1,3);
change_va(a,3,2,11);
start = clock();
Matrix applyed_ma = appply(a,a,1);
cout_mat(applyed_ma);
cout<<"--------split---------"<<endl;
Matrix b = get_T(a);
for(int index_x = 0;index_x<b.row;index_x++)
{
for(int index_y=0;index_y<b.col;index_y++)
{
cout<<b.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
cout<<"-----------split line-----------"<<endl;
cout<<"matrix*n"<<endl;
Matrix c = times_mat(8,b);
for(int index_x = 0;index_x<c.row;index_x++)
{
for(int index_y=0;index_y<c.col;index_y++)
{
cout<<c.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
cout<<"-----------split line-----------"<<endl;
cout<<"matrix*matrix"<<endl;
Matrix d =mul(c,get_T(c));
for(int index_x = 0;index_x<d.row;index_x++)
{
for(int index_y=0;index_y<d.col;index_y++)
{
cout<<d.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
cout<<"-----------split line-----------"<<endl;
Matrix rs = matrix_rs(a,6,5);
for(int index_x = 0;index_x<rs.row;index_x++)
Matrix bais = CreateMatrix(data.row,1);
data = appply(data,bais,1);
Matrix y = iloc(data,0,0,3,4);
Matrix x_1 = iloc(data,0,0,0,3);
Matrix x_2 = get_T(x_1);
double alpha = 0.002;
int max_epoch = 100;
Matrix weight = CreateMatrix(3,1);
change_va(weight,0,0,1);
change_va(weight,1,0,1);
change_va(weight,2,0,1);
int epoch = 0;
for(epoch = 0;epoch<=max_epoch;epoch++)
{
for(int index_y=0;index_y<rs.col;index_y++)
{
cout<<rs.matrix[index_x][index_y]<<" ";
}
cout<<endl;
cout<<"-----------split-line-----------"<<endl;
Matrix temp_mul = mul(x_1,weight);
Matrix h =e_sigmoid(temp_mul);
Matrix error = subtract(y,h);
Matrix temp_update = mul(x_2,error);
Matrix updata = add(weight,times_mat(alpha,temp_update),0);
cout_mat(weight);
cout<<"epoch: "<<epoch<<" error: "<<matrix_sum(error)<<endl;
cout<<"-----------split-line-----------"<<endl;
}
times_mat(5,a);
stop = clock();
printf("%f\n", (double)(stop - start) / CLOCKS_PER_SEC);
return 0;
......
......@@ -55,18 +55,17 @@ Matrix add(Matrix mid1,Matrix mid2,int flag=1)
Matrix subtract(Matrix mid1,Matrix mid2)
{
if(mid1.row == mid2.row&&mid1.col == mid2.col)
Matrix result_subtract = CreateMatrix(mid1.row,mid2.col);
for(int i=0; i<mid1.row; i++)
{
for(int i=0; i<mid1.row; i++)
for(int j=0; j<mid1.col; j++)
{
for(int j=0; j<mid1.col; j++)
{
mid1.matrix[i][j] = mid1.matrix[i][j] -mid2.matrix[i][j];
}
//cout<<" mid1.matrix[i][j]"<< mid1.matrix[i][j]<<"mid2.matrix[i][j]"<<mid2.matrix[i][j]<<endl;
result_subtract.matrix[i][j] = mid1.matrix[i][j]-mid2.matrix[i][j];
}
}
else{return mid1;}
return mid1;
return result_subtract;
}
Matrix mul(Matrix mid1,Matrix mid2)
......@@ -84,7 +83,7 @@ for(int i = 0;i<mid1.row;i++)
}
return mid3;
}
Matrix times_mat(int times,Matrix mid1)
Matrix times_mat(double times,Matrix mid1)
{
for(int index_x=0; index_x<mid1.row; index_x++)
{
......
-0.017612,14.053064,0
-1.395634,4.662541,1
-0.752157,6.53862,0
-1.322371,7.152853,0
0.423363,11.054677,0
0.406704,7.067335,1
0.667394,12.741452,0
-2.46015,6.866805,1
0.569411,9.548755,0
-0.026632,10.427743,0
0.850433,6.920334,1
1.347183,13.1755,0
1.176813,3.16702,1
-1.781871,9.097953,0
-0.566606,5.749003,1
0.931635,1.589505,1
-0.024205,6.151823,1
-0.036453,2.690988,1
-0.196949,0.444165,1
1.014459,5.754399,1
1.985298,3.230619,1
-1.693453,-0.55754,1
-0.576525,11.778922,0
-0.346811,-1.67873,1
-2.124484,2.672471,1
1.217916,9.597015,0
-0.733928,9.098687,0
-3.642001,-1.618087,1
0.315985,3.523953,1
1.416614,9.619232,0
-0.386323,3.989286,1
0.556921,8.294984,1
1.224863,11.58736,0
-1.347803,-2.406051,1
1.196604,4.951851,1
0.275221,9.543647,0
0.470575,9.332488,0
-1.889567,9.542662,0
-1.527893,12.150579,0
-1.185247,11.309318,0
-0.445678,3.297303,1
1.042222,6.105155,1
-0.618787,10.320986,0
1.152083,0.548467,1
0.828534,2.676045,1
-1.237728,10.549033,0
-0.683565,-2.166125,1
0.229456,5.921938,1
-0.959885,11.555336,0
0.492911,10.993324,0
0.184992,8.721488,0
-0.355715,10.325976,0
-0.397822,8.058397,0
0.824839,13.730343,0
1.507278,5.027866,1
0.099671,6.835839,1
-0.344008,10.717485,0
1.785928,7.718645,1
-0.918801,11.560217,0
-0.364009,4.7473,1
-0.841722,4.119083,1
0.490426,1.960539,1
-0.007194,9.075792,0
0.356107,12.447863,0
0.342578,12.281162,0
-0.810823,-1.466018,1
2.530777,6.476801,1
1.296683,11.607559,0
0.475487,12.040035,0
-0.783277,11.009725,0
0.074798,11.02365,0
-1.337472,0.468339,1
-0.102781,13.763651,0
-0.147324,2.874846,1
0.518389,9.887035,0
1.015399,7.571882,0
-1.658086,-0.027255,1
1.319944,2.171228,1
2.056216,5.019981,1
-0.851633,4.375691,1
-1.510047,6.061992,0
-1.076637,-3.181888,1
1.821096,10.28399,0
3.01015,8.401766,1
-1.099458,1.688274,1
-0.834872,-1.733869,1
-0.846637,3.849075,1
1.400102,12.628781,0
1.752842,5.468166,1
0.078557,0.059736,1
0.089392,-0.7153,1
1.825662,12.693808,0
0.197445,9.744638,0
0.126117,0.922311,1
-0.679797,1.22053,1
0.677983,2.556666,1
0.761349,10.693862,0
-2.168791,0.143632,1
1.38861,9.341997,0
0.317029,14.739025,0
\ No newline at end of file
1,-0.017612,14.053064,0
1,-1.395634,4.662541,1
1,-0.752157,6.53862,0
1,-1.322371,7.152853,0
1,0.423363,11.054677,0
1,0.406704,7.067335,1
1,0.667394,12.741452,0
1,-2.46015,6.866805,1
1,0.569411,9.548755,0
1,-0.026632,10.427743,0
1,0.850433,6.920334,1
1,1.347183,13.1755,0
1,1.176813,3.16702,1
1,-1.781871,9.097953,0
1,-0.566606,5.749003,1
1,0.931635,1.589505,1
1,-0.024205,6.151823,1
1,-0.036453,2.690988,1
1,-0.196949,0.444165,1
1,1.014459,5.754399,1
1,1.985298,3.230619,1
1,-1.693453,-0.55754,1
1,-0.576525,11.778922,0
1,-0.346811,-1.67873,1
1,-2.124484,2.672471,1
1,1.217916,9.597015,0
1,-0.733928,9.098687,0
1,-3.642001,-1.618087,1
1,0.315985,3.523953,1
1,1.416614,9.619232,0
1,-0.386323,3.989286,1
1,0.556921,8.294984,1
1,1.224863,11.58736,0
1,-1.347803,-2.406051,1
1,1.196604,4.951851,1
1,0.275221,9.543647,0
1,0.470575,9.332488,0
1,-1.889567,9.542662,0
1,-1.527893,12.150579,0
1,-1.185247,11.309318,0
1,-0.445678,3.297303,1
1,1.042222,6.105155,1
1,-0.618787,10.320986,0
1,1.152083,0.548467,1
1,0.828534,2.676045,1
1,-1.237728,10.549033,0
1,-0.683565,-2.166125,1
1,0.229456,5.921938,1
1,-0.959885,11.555336,0
1,0.492911,10.993324,0
1,0.184992,8.721488,0
1,-0.355715,10.325976,0
1,-0.397822,8.058397,0
1,0.824839,13.730343,0
1,1.507278,5.027866,1
1,0.099671,6.835839,1
1,-0.344008,10.717485,0
1,1.785928,7.718645,1
1,-0.918801,11.560217,0
1,-0.364009,4.7473,1
1,-0.841722,4.119083,1
1,0.490426,1.960539,1
1,-0.007194,9.075792,0
1,0.356107,12.447863,0
1,0.342578,12.281162,0
1,-0.810823,-1.466018,1
1,2.530777,6.476801,1
1,1.296683,11.607559,0
1,0.475487,12.040035,0
1,-0.783277,11.009725,0
1,0.074798,11.02365,0
1,-1.337472,0.468339,1
1,-0.102781,13.763651,0
1,-0.147324,2.874846,1
1,0.518389,9.887035,0
1,1.015399,7.571882,0
1,-1.658086,-0.027255,1
1,1.319944,2.171228,1
1,2.056216,5.019981,1
1,-0.851633,4.375691,1
1,-1.510047,6.061992,0
1,-1.076637,-3.181888,1
1,1.821096,10.28399,0
1,3.01015,8.401766,1
1,-1.099458,1.688274,1
1,-0.834872,-1.733869,1
1,-0.846637,3.849075,1
1,1.400102,12.628781,0
1,1.752842,5.468166,1
1,0.078557,0.059736,1
1,0.089392,-0.7153,1
1,1.825662,12.693808,0
1,0.197445,9.744638,0
1,0.126117,0.922311,1
1,-0.679797,1.22053,1
1,0.677983,2.556666,1
1,0.761349,10.693862,0
1,-2.168791,0.143632,1
1,1.38861,9.341997,0
1,0.317029,14.739025,0
\ No newline at end of file
import re
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from numpy import exp
def data():#读取txt文本内的数据并转成需要的list
f=open('data2.txt')
data=[]
for each_line in f:
each_line=each_line.strip('\n')
res=re.split(" ",each_line)
res=list(filter(None,res))
res= list(map(float,res))
data.append(res)
for i in range(len(data)):#加一个偏执项
data[i].insert(0,1)
f.close()
print(len(data[1]))
return data
def sin_cos(data,weights):#画图
x0=[]
x1=[]
y0=[]
y1=[]
for i in data:
if(i[3]==0):
x0.append(i[1])
y0.append(i[2])
else:
x1.append(i[1])
y1.append(i[2])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x0,y0,marker='+',label='1',s=40,c='r')
ax.scatter(x1,y1,marker='*',label='1',s=40,c='b')
x = np.arange(-3, 3, 0.1)
y = (-weights[0, 0] - weights[1, 0] * x) / weights[2, 0] #matix
ax.plot(x, y)
plt.xlabel('X1')
plt.ylabel('X2')
plt.savefig('test.png')
#plt.show()
def sigmoid(inX):#逻辑函数
return 1.0/(1+exp(-inX))
def tiduxiajiang_sigmoid(data):
x=[]
y=[]
for i in range(len(data)):
x.append(data[i][:-1])
y.append(data[i][-1:])
alpha=0.002
maxcycle=1
x=np.mat(x)#转成矩阵
y=np.mat(y)
x2=x.T#矩阵转逆
weights=np.mat([[1],[1],[1]])
for i in range(maxcycle):
h = sigmoid(x* weights)
print(h.shape)
error=y-h
print("error.shape: ",error.shape)
print(error)
print("x2.h",x2.shape)
print(x2)
a = x2*error
print("alpha*x2*error: ",a.shape)
weights=weights+alpha*x2*error
print(sum(error))#看是否收敛
return weights
data=data()
weights=tiduxiajiang_sigmoid(data)
sin_cos(data,weights)
# print('\n')
#print(weights)
#to_csv(data)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册