《最小二乘法求线性回归参数.doc》由会员分享,可在线阅读,更多相关《最小二乘法求线性回归参数.doc(3页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1.最小二乘法求线性回归参数线性回归公式损失函数最小二乘法求参数令,因此,损失函数转换为求导求出驻点得到2.代码package weka.classifiers.myAlgorithm;import weka.classifiers.Classifier;import weka.core.Instance;import weka.core.Instances;public class LinearRegression_LeastSquare extends Classifierpublic int m_NumInstance;public int m_NumAttribute;/* * 参数列
2、表 */public double m_Parameter;Overridepublic void buildClassifier(Instances data) throws Exception/ TODO Auto-generated method stubm_NumInstance = data.numInstances();m_NumAttribute = data.numAttributes();m_Parameter = new doublem_NumAttribute;/训练数据集的属性值矩阵double mat_X = new doublem_NumInstancem_NumA
3、ttribute;/训练数据集的类属性值矩阵double mat_Y = new doublem_NumInstance1;for (int i = 0; i m_NumInstance; i+)Instance instance = data.instance(i);mat_Yi0 = instance.classValue();for (int j = 0; j m_NumAttribute; j+)if (j != data.classIndex()mat_Xij = instance.value(j);elsemat_Xij = 1.0;/最小二乘法求线性回归的参数weka.core.
4、matrix.Matrix X = new weka.core.matrix.Matrix(mat_X);weka.core.matrix.Matrix Y = new weka.core.matrix.Matrix(mat_Y);weka.core.matrix.Matrix tranposeX = X.transpose();weka.core.matrix.Matrix temp1 = tranposeX.times(X);weka.core.matrix.Matrix temp2 = temp1.inverse();weka.core.matrix.Matrix temp3 = tem
5、p2.times(tranposeX);weka.core.matrix.Matrix temp4 = temp3.times(Y);m_Parameter = temp4.getRowPackedCopy();public double classifyInstance(Instance instance)double result = 0.0;for (int i = 0; i m_NumAttribute; i+)if (i != instance.classIndex()result += m_Parameteri * instance.value(i);elseresult += m_Parameteri;return result;