Latihas的南方孤岛
Latihas的南方孤岛

机器学习——线性回归

线性回归的方式有两种,分别是 最小二乘法(正规方程法)与梯度下降法

最小二乘法

\(\Theta =(X^TX)^{-1}X^T y\)

梯度下降法

repeat{ \(\Theta {j}=\Theta {j}-\alpha \frac{1}{m}\sum_{i=1}^{m}(h_{\theta }(x^{(i)})-y^{(i)})x_{j}^{(i)}\) }

代码

package com.latihas.MachineLearning;

import org.ujmp.core.Matrix;

import java.util.Arrays;

import static java.lang.Math.pow;
import static java.lang.System.out;
import static java.math.BigDecimal.valueOf;
import static java.util.Arrays.fill;
import static org.ujmp.core.Matrix.Factory;

public class Main {
	public static void main(String[] args) {
		//matrix
		int featureNum = 3;
		double[][] input = {
				{-1, 2},
				{1, 1},
				{2, 1},
				{3, 0},
				{5, 3}
		};
		
		Matrix A = Factory.zeros(input.length, featureNum),
				Y = Factory.zeros(input.length, 1),
				X = Factory.zeros(input.length, 1);
		for (int i = 0; i < input.length; i++) {
			double tmp = input[i][0];
			for (int j = 0; j < featureNum; j++)
				A.setAsBigDecimal(valueOf(pow(tmp, j)), i, j);
			Y.setAsBigDecimal(valueOf(input[i][1]), i, 0);
			X.setAsBigDecimal(valueOf(input[i][0]), i, 0);
		}
		Matrix t3 = A.transpose().mtimes(A).pinv().mtimes(A.transpose()).mtimes(Y);
		out.println(t3);
		//cal
		int x = 0;
		double alpha = 0.001;
		double[] bY = Y.transpose().toDoubleArray()[0],
				bX = X.transpose().toDoubleArray()[0],
				theta = new double[featureNum],
				otheta;
		double[][]tmp=new double[input.length][featureNum];
		fill(theta, 0);
		for (int i = 0; i < input.length; i++)
			for (int k = 0; k < featureNum; k++)
				tmp[i][k]=pow(bX[i], k);
		
		do {
			otheta = theta;
			for (int j = 0; j < featureNum; j++) {
				double sig = 0;
				for (int i = 0; i < input.length; i++) {
					double h = 0;
					for (int k = 0; k < featureNum; k++)
						h += theta[k] * tmp[i][k];
					sig += (h - bY[i]) * tmp[i][j];
				}
				sig = sig * alpha / input.length;
				otheta[j] = otheta[j] - (sig);
			}
			theta = otheta;
			x++;
		} while (x < 10000000);
		out.println(Arrays.toString(theta));
	}
	
}

输出(样例数据来自算法导论)

    1.2000
   -0.7571
    0.2143

[1.1999999999997506, -0.7571428571427327, 0.21428571428570048]

在矩阵规模<10000×10000时建议使用最小二乘法,而在处理规模远大于此的数据时建议用梯度下降。

Latihas

文章作者

发表评论

textsms
account_circle
email

  • Thank you ever so for you post. Much thanks again. Great. Gabi Giustino Damarra

    6月前 回复
  • Very nice blog post. I certainly love this website. Stick with it! Sylvia Joshuah Saul

    5月前 回复
  • Music is truly amazing in so many ways. While I am not a musician or singer, I so appreciate the many benefits and attributes that music brings to my soul. It truly moves me. Katina Jase Stevana

    5月前 回复
  • Received iTunes Connect review feedback today for our first Apppresser based app: Lottie Othello Hamilton

    5月前 回复
  • Greetings! Very helpful advice within this post! It is the little changes that will make the biggest changes. Thanks a lot for sharing! Marcile Jermain Yvette

    5月前 回复
  • He was from a big litter, there was a 100lb weight fishing rod mounts on the end of the bass you may get. Jodee Stu Mylor

    5月前 回复
  • Hmm is anyone else experiencing problems with the images on this blog loading? Blondelle Tito Derman

    5月前 回复
  • I got what you mean,saved to my bookmarks, very nice internet site. Deloris Brent McCarty

    5月前 回复
  • We are a group of volunteers and opening a brand new scheme in our community. Blondelle Tito Derman

    5月前 回复
  • Very nice article, totally what I was looking for. Alayne Wayland Garret

    5月前 回复
  • Hi there colleagues, its impressive article concerning cultureand completely explained, keep it up all the time. Caryn Loren Darrell

    5月前 回复
  • naturally like your website but you need to test the spelling on quite a few of your posts. Several of them are rife with spelling issues and I in finding it very troublesome to inform the truth nevertheless I will surely come again again. Jennilee Shermy Taub

    5月前 回复
  • How many remixes can you have, is it allowed to submit two versions of the Remix??? Rozamond Averil Malinde

    5月前 回复
  • I am so grateful for your article. Really looking forward to read more. Great. Jeniece Roderick Ive

    5月前 回复
  • To adjust in a new country is always a nightmare for first instance but when adjusted, it is a bliss. You may be finding driving odd on left side but as in India it is also on left side we find it difficult on right side in US and other countries. You are quite a good observant, I must say. Manda Michel Undine

    5月前 回复
  • A speed exercise you possibly can perform on the treadmill to extend yoour working speed and improve your health. Indira Matthaeus Aloise

    5月前 回复
  • It is appropriate time to make some plans for the future and it is time to be happy. I have read this post and if I could I desire to suggest you few interesting things or advice. Maybe you can write next articles referring to this article. I wish to read even more things about it! Georgena Filbert Gladdie

    4月前 回复
  • se usan para fijar los tirantes de madera a la pared Lorrin Delmore Egbert

    4月前 回复
  • that is the finish of this article. Right here you will obtain some sites that we believe youll value, just click the hyperlinks over Cathy Marwin Sigismond

    4月前 回复
  • Hey there! I simply would like to give you a huge thumbs up for your great info you have here on this post. I will be returning to your site for more soon. Hedwiga Olivier Shanks

    4月前 回复
  • You are a great writer. Do you have any more webpages? I love sucking dick btw hmu Kate Ev Drucill

    4月前 回复
  • Howdy! I realize this is sort of off-topic but I had to ask. Caitrin Jackie Mimi

    4月前 回复
  • I appreciate you sharing this blog article. Thanks Again. Really Great. Flora Ive Moynahan

    4月前 回复
  • Good day! I just would like to give you a big thumbs up for your excellent info you have got here on this post. I will be returning to your site for more soon. Gilberta Harwilll Laraine

    4月前 回复
  • Pretty! This was an incredibly wonderful post. Thanks for providing this info. Torie Nathan Herschel

    4月前 回复
  • d dolor. Mauris lectus mi, fringilla tempor, varius sit amet, placerat a, velit. Suspendisse justo. In vehicula urna fringilla neque. Suspendisse cursus, magna a imperdiet pellentesque, lacus velit dignissim urna, vel suscipit massa enim ac nunc. Proin porta aliquet eros. Curabitur ut erat Beatrice Fredrick Anuska

    4月前 回复
  • Thank you for sharing your thoughts. I really appreciate your efforts and I am waiting for your further write ups thank you once again. Malanie Kellen Chadabe Donna Max Ostraw

    4月前 回复
  • Great delivery. Outstanding arguments. Keep up the great effort. Carolyn Ettore Cherilynn

    4月前 回复
  • Thanks again for the post. Really looking forward to read more. Really Cool. Berget See Kaete Remy Gawain Booker

    4月前 回复
  • An interesting discussion is definitely worth comment. I think that you need to write more on this subject matter, it may not be a taboo subject but typically people do not talk about such topics. To the next! Many thanks. Cathie Mikey Bauer

    4月前 回复
  • Alacakaranlik Safak Vakti izle, Alacakaranlik Safak Vakti full izle, Alacakaranlik Safak Vakti t�rk�e dublaj izle, Alacakaranlik Safak Vakti hd izle, The Twilight Saga: Breaking Dawn – Part 1 izle, Sonunda iki asik bela ve edward evlenmek’dedir. Edward ile Jacop arasinda bir se�im bir se�im yapmak zorunda kalan bela tercihini tutkuyla asik oldugu edward’dan yana kullanmaktadir. Film Bela ve Edward’in �evresinde d�nerken esrarengiz olaylar olur ikili tatildeyken �ocuklarinin olacaginin farkina varir bu olay akillara sigmayacak bir sey vampir adam ve insan kani tasiyan kisin �ocuklari olmasi bu konuyu aile ile konusmak isteyen ikili bakalim ne diyecekler nasil bir durum ortaya �ikacaktir. Sabrina Leupold Ricca

    4月前 回复
  • Alacakaranlik Safak Vakti izle, Alacakaranlik Safak Vakti full izle, Alacakaranlik Safak Vakti t�rk�e dublaj izle, Alacakaranlik Safak Vakti hd izle, The Twilight Saga: Breaking Dawn – Part 1 izle, Sonunda iki asik bela ve edward evlenmek’dedir. Edward ile Jacop arasinda bir se�im bir se�im yapmak zorunda kalan bela tercihini tutkuyla asik oldugu edward’dan yana kullanmaktadir. Film Bela ve Edward’in �evresinde d�nerken esrarengiz olaylar olur ikili tatildeyken �ocuklarinin olacaginin farkina varir bu olay akillara sigmayacak bir sey vampir adam ve insan kani tasiyan kisin �ocuklari olmasi bu konuyu aile ile konusmak isteyen ikili bakalim ne diyecekler nasil bir durum ortaya �ikacaktir. Bonnee Salmon Kris

    4月前 回复
  • Alacakaranlik Safak Vakti izle, Alacakaranlik Safak Vakti full izle, Alacakaranlik Safak Vakti t�rk�e dublaj izle, Alacakaranlik Safak Vakti hd izle, The Twilight Saga: Breaking Dawn – Part 1 izle, Sonunda iki asik bela ve edward evlenmek’dedir. Edward ile Jacop arasinda bir se�im bir se�im yapmak zorunda kalan bela tercihini tutkuyla asik oldugu edward’dan yana kullanmaktadir. Film Bela ve Edward’in �evresinde d�nerken esrarengiz olaylar olur ikili tatildeyken �ocuklarinin olacaginin farkina varir bu olay akillara sigmayacak bir sey vampir adam ve insan kani tasiyan kisin �ocuklari olmasi bu konuyu aile ile konusmak isteyen ikili bakalim ne diyecekler nasil bir durum ortaya �ikacaktir. Polly Redford Prober

    4月前 回复

Latihas的南方孤岛

机器学习——线性回归
线性回归的方式有两种,分别是 最小二乘法(正规方程法)与梯度下降法 最小二乘法 \(\Theta =(X^TX)^{-1}X^T y\) 梯度下降法 repeat{ \(\Theta {j}=\Theta {j}-\alpha \frac{…
扫描二维码继续阅读
2020-03-04