机器学习笔记

    返回首页    发表留言
本文作者:李德强
          第五节 矩阵法实现
 
 

        现在我们来看一下多元线性回归的程序实现(关于矩阵计算的程序部分请参见《矩阵基本运算》):

	//读取训练样本
	s_Sample *sample = sample_load("data/data.txt");
	//样本数
	int M = sample->countx;
	//特征数
	int N = sample->countf;

	s_Matrix X;
	s_Matrix Y;
	s_Matrix P;
	matrix_init(&X, M, N);
	matrix_init(&Y, M, 1);
	matrix_init(&P, N, 1);
	s_Matrix X_T;
	s_Matrix X_INV;
	s_Matrix X_temp;
	matrix_init(&X_T, N, M);
	matrix_init(&X_INV, N, N);
	matrix_init(&X_temp, N, N);

	//将样本数据生成矩阵
	for (int i = 0; i < M; i++)
	{
		for (int j = 0; j < N; j++)
		{
			if (j == 0)
			{
				X.v[i * N + j] = 1;
				continue;
			}

			X.v[i * N + j] = sample->x[i * N + (j - 1)];
		}
		Y.v[i] = sample->x[i * N + (N - 1)];
	}

	//转置矩阵
	matrix_transposition(&X_T, &X);
	//矩阵乘法
	matrix_mult(&X_temp, &X_T, &X);
	//逆矩阵
	matrix_inverse(&X_INV, &X_temp);

	matrix_destory(&X_temp);
	matrix_init(&X_temp, M, N);

	//矩阵乘法
	matrix_mult(&X_temp, &X_INV, &X_T);
	matrix_mult(&P, &X_temp, &Y);
	//显示参数
	printf("B:\n");
	matrix_display(&P);

	matrix_destory(&X_temp);
	matrix_destory(&X_INV);
	matrix_destory(&X_T);
	matrix_destory(&X);
	matrix_destory(&Y);
	matrix_destory(&P);

	//释放训练样本内存
	sample_free(sample);

        样本数据:

1.000000, 15.150000
3.000000, 14.850000
3.000000, 20.850000
12.000000, 26.000000
5.000000, 15.550000
11.000000, 17.650000
11.000000, 25.650000
10.000000, 20.300000
15.000000, 20.050000
12.000000, 19.000000
17.000000, 22.750000
12.000000, 21.000000
19.000000, 29.450000
17.000000, 21.750000
21.000000, 26.150000
24.000000, 30.200000
25.000000, 31.550000
26.000000, 30.900000
25.000000, 31.550000
28.000000, 31.600000
30.000000, 27.300000
22.000000, 26.500000
26.000000, 24.900000
24.000000, 22.200000
31.000000, 28.650000
29.000000, 25.950000
34.000000, 28.700000
35.000000, 29.050000
33.000000, 28.350000
35.000000, 26.050000
39.000000, 32.450000
40.000000, 36.800000
38.000000, 34.100000
43.000000, 31.850000
41.000000, 36.150000
39.000000, 34.450000
40.000000, 30.800000
43.000000, 35.850000
44.000000, 34.200000
49.000000, 31.950000
41.000000, 30.150000
48.000000, 37.600000
48.000000, 35.600000
47.000000, 30.250000
53.000000, 32.350000
46.000000, 38.900000
53.000000, 33.350000
56.000000, 36.400000
57.000000, 40.750000
56.000000, 39.400000
58.000000, 43.100000
57.000000, 34.750000
57.000000, 33.750000
62.000000, 44.500000
61.000000, 42.150000
59.000000, 40.450000
66.000000, 37.900000
63.000000, 41.850000
67.000000, 45.250000
68.000000, 44.600000
62.000000, 44.500000
70.000000, 45.300000
65.000000, 44.550000
64.000000, 38.200000
72.000000, 47.000000
74.000000, 44.700000
74.000000, 42.700000
74.000000, 42.700000
73.000000, 45.350000
72.000000, 41.000000
76.000000, 47.400000
72.000000, 45.000000
73.000000, 44.350000
76.000000, 49.400000
79.000000, 42.450000
84.000000, 48.200000
79.000000, 47.450000
82.000000, 46.500000
85.000000, 47.550000
86.000000, 46.900000
83.000000, 48.850000
90.000000, 46.300000
84.000000, 49.200000
88.000000, 51.600000
87.000000, 52.250000
95.000000, 56.050000
95.000000, 56.050000
93.000000, 54.350000
94.000000, 46.700000
99.000000, 49.450000
92.000000, 53.000000
100.000000, 53.800000
98.000000, 50.100000
95.000000, 48.050000
103.000000, 58.850000
101.000000, 51.150000
102.000000, 54.500000
103.000000, 57.850000
101.000000, 50.150000
106.000000, 56.900000

        运行结果:

B:
17.67547		
0.3648939

        图形显示结果:

    返回首页    返回顶部
  看不清?点击刷新

 

  Copyright © 2015-2018 问渠网 辽ICP备15013245号