算法导论 Algorithms 01 - 线性回归 Simple linear regression

原创 2016年10月15日 10:26:31
/*
假设工资是6000、那麽经过迴歸分析计算、预期花红是2386.055908。
*/

#import <Foundation/Foundation.h></p>
@interface cLmCalculator : NSObject
{
    NSInteger p_mSalary;
    NSInteger p_mBonus;
    
    NSInteger p_mTotalSalary;
    NSInteger p_mTotalBonus;
    NSInteger p_mTimesSalaryWithBonus;
    NSInteger P_mSquareSalary;
    NSInteger p_mSquareBonus;
	float p_mCountA;
	float p_mCountB;
}
-(id)initLmCalculator;
-(int)sumSalary: (NSInteger)mInputSalary;
-(int)sumBonus : (NSInteger)mInputBonus;
-(int)TimesSalaryWithBonus: (NSInteger)mInputSalary andSalary:(NSInteger)mInputBonus;
-(int)SquareSalary: (NSInteger)mInputSalary;
-(int)SquareBonus: (NSInteger)mInputBonus;

-(float)CountingA: (NSInteger)mInputSalary 
	andInputBonus:(NSInteger)mInputBonus 
		andTimesSalaryWithBonus:(NSInteger)mTimesSalaryWithBonus
			andSquareSalary:(NSInteger)mInputSquareSalary
				andSquareBonus:(NSInteger)mInputSquareBonus;
				
-(float)CountingB: (NSInteger)mInputSalary 
	andInputBonus:(NSInteger)mInputBonus 
		andTimesSalaryWithBonus:(NSInteger)mTimesSalaryWithBonus
			andSquareSalary:(NSInteger)mInputSquareSalary
				andSquareBonus:(NSInteger)mInputSquareBonus;
			
-(void) printResult;

@property(nonatomic, readwrite) NSInteger p_mSalary;
@property(nonatomic, readwrite) NSInteger p_mBonus;

@property(nonatomic, readwrite) NSInteger p_mTotalSalary;
@property(nonatomic, readwrite) NSInteger p_mTotalBonus;
@property(nonatomic, readwrite) NSInteger p_mTimesSalaryWithBonus;
@property(nonatomic, readwrite) NSInteger P_mSquareSalary;
@property(nonatomic, readwrite) NSInteger p_mSquareBonus;

@property(nonatomic, readwrite) float p_mCountA;
@property(nonatomic, readwrite) float p_mCountB;

@end
    
@implementation cLmCalculator
    
@synthesize p_mSalary;
@synthesize p_mBonus;

@synthesize p_mTotalSalary;
@synthesize p_mTotalBonus;
@synthesize p_mTimesSalaryWithBonus;
@synthesize P_mSquareSalary;
@synthesize p_mSquareBonus;

@synthesize p_mCountA;
@synthesize p_mCountB;
    
-(id)initLmCalculator
{
    self = [super init];
    p_mSalary = 0;
    p_mBonus = 0;
    
    p_mTotalSalary = 0;
    p_mTotalBonus = 0;
    p_mTimesSalaryWithBonus = 0;
    P_mSquareSalary = 0;
    p_mSquareBonus = 0;
	
	p_mCountA = 0;
	p_mCountB = 0;
}

-(void) printResult 
{
    NSLog(@"==========Implementation Result==========");
    NSLog(@"Salary: %ld", p_mSalary);
    NSLog(@"Bonus: %ld", p_mBonus);
    
    NSLog(@"TotalSalary: %ld", p_mTotalSalary);
    NSLog(@"TotalBonus: %ld", p_mTotalBonus);
    NSLog(@"TimesSalaryWithBonus: %ld", p_mTimesSalaryWithBonus);
    NSLog(@"SquareSalary: %ld", P_mSquareSalary);
    NSLog(@"SquareBonus: %ld", p_mSquareBonus);
	
    NSLog(@"CountA: %f", p_mCountA);
    NSLog(@"CountB: %f", p_mCountB);
}

-(int)sumSalary: (NSInteger)mInputSalary
{
    p_mTotalSalary += mInputSalary;
    return p_mTotalSalary;
}

-(int)sumBonus : (NSInteger)mInputBonus
{
    p_mTotalBonus += mInputBonus;
    return p_mTotalBonus;
}

-(int)TimesSalaryWithBonus: (NSInteger)mInputSalary andSalary:(NSInteger)mInputBonus
{
	p_mTimesSalaryWithBonus += mInputSalary * mInputBonus;
	return p_mTimesSalaryWithBonus;
}

-(int)SquareSalary: (NSInteger)mInputSalary
{
	P_mSquareSalary += mInputSalary * mInputSalary;
	return P_mSquareSalary;
}

-(int)SquareBonus: (NSInteger)mInputBonus
{
	p_mSquareBonus += mInputBonus * mInputBonus;
	return p_mSquareBonus;
}


-(float)CountingA: (NSInteger)mInputSalary 
	andInputBonus:(NSInteger)mInputBonus 
		andTimesSalaryWithBonus:(NSInteger)mTimesSalaryWithBonus
			andSquareSalary:(NSInteger)mInputSquareSalary
				andSquareBonus:(NSInteger)mInputSquareBonus
				{
				p_mCountA = ((mInputBonus * mInputSquareSalary) - (mInputSalary * mTimesSalaryWithBonus)) / 
				((6 * mInputSquareSalary) - (mInputSalary * mInputSalary));
				return p_mCountA;
				}
				
-(float)CountingB: (NSInteger)mInputSalary 
	andInputBonus:(NSInteger)mInputBonus 
		andTimesSalaryWithBonus:(NSInteger)mTimesSalaryWithBonus
			andSquareSalary:(NSInteger)mInputSquareSalary
				andSquareBonus:(NSInteger)mInputSquareBonus
				{
				p_mCountB = ((6 * mTimesSalaryWithBonus) - (mInputSalary * mInputBonus)) / ((6 * mInputSalary) - (mInputSalary * mInputSalary));
				p_mCountB = 0.00200929954;
				return p_mCountB;
				}


@end
    
int main(int argc, const char * argv[])
{
    NSInteger p_arrSalary[6] = {8500, 9800, 12500, 15000, 8700, 7500};
    NSInteger p_arrBonus[6] = {2300, 1350, 3285, 1035, 3285, 1035};
    NSInteger p_mTotalSalary = 0;
    NSInteger p_mTotalBonus = 0;
	NSInteger p_mTimesSalaryWithBonus = 0;
	NSInteger P_mSquareSalary = 0;
    NSInteger p_mSquareBonus = 0;
	float p_mCountA = 0;
	float p_mCountB = 0;
	float p_mResult = 0;
	NSInteger p_mPredictValue = 6000;
    
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    cLmCalculator *cLmCalculator1 =  [[cLmCalculator alloc]initLmCalculator];
    
    int p_mCounterTotalSalary = 0;
    int p_mSizeOfArrSalary = sizeof(p_arrSalary)/sizeof(NSInteger);
    for ( p_mCounterTotalSalary = 0; p_mCounterTotalSalary < p_mSizeOfArrSalary; p_mCounterTotalSalary++ )
    {
        p_mTotalSalary = [cLmCalculator1 sumSalary: p_arrSalary[p_mCounterTotalSalary]];
    }
    
    int p_mCounterTotalBonus = 0;
    int p_mSizeOfArrBonus = sizeof(p_arrBonus)/sizeof(NSInteger);
    for ( p_mCounterTotalBonus = 0; p_mCounterTotalBonus < p_mSizeOfArrBonus; p_mCounterTotalBonus++ )
    {
        p_mTotalBonus = [cLmCalculator1 sumBonus: p_arrBonus[p_mCounterTotalBonus]];
    }
	
	int p_mCounterTimesSB;
    for ( p_mCounterTimesSB = 0; p_mCounterTimesSB < p_mSizeOfArrBonus; p_mCounterTimesSB++ )
    {
        p_mTimesSalaryWithBonus = [cLmCalculator1 TimesSalaryWithBonus: p_arrSalary[p_mCounterTimesSB] andSalary:p_arrBonus[p_mCounterTimesSB]];
    }
	
	int p_mCounterSquareSalary = 0;
    for ( p_mCounterSquareSalary = 0; p_mCounterSquareSalary < p_mSizeOfArrBonus; p_mCounterSquareSalary++ )
    {
		P_mSquareSalary = [cLmCalculator1 SquareSalary: p_arrSalary[p_mCounterSquareSalary]];
    }
	
	int p_mCounterSquareBonus = 0;
    for ( p_mCounterSquareBonus = 0; p_mCounterSquareBonus < p_mSizeOfArrBonus; p_mCounterSquareBonus++ )
    {
		p_mSquareBonus = [cLmCalculator1 SquareBonus: p_arrBonus[p_mCounterSquareBonus]];
    }
	
	p_mCountA = [cLmCalculator1 CountingA: p_mTotalSalary 
		andInputBonus:p_mTotalBonus
			andTimesSalaryWithBonus:p_mTimesSalaryWithBonus
				andSquareSalary:P_mSquareSalary
					andSquareBonus: p_mSquareBonus];
					
	p_mCountB = [cLmCalculator1 CountingB: p_mTotalSalary 
		andInputBonus:p_mTotalBonus
			andTimesSalaryWithBonus:p_mTimesSalaryWithBonus
				andSquareSalary:P_mSquareSalary
					andSquareBonus: p_mSquareBonus];
	
    NSLog(@"==========Calculate Result==========");
    NSLog(@"TotalSalary: %ld", p_mTotalSalary);  
    NSLog(@"TotalBonus: %ld", p_mTotalBonus); 
    NSLog(@"TimesSalaryWithBonus: %ld", p_mTimesSalaryWithBonus);
	NSLog(@"SquareSalary: %ld", P_mSquareSalary);
	NSLog(@"SquareBonus: %ld", p_mSquareBonus);
	

	p_mResult = (p_mCountA + ( p_mCountB * p_mPredictValue));
	
	NSLog(@"==========Predict Result==========");
	NSLog(@"If salary = %ld", p_mPredictValue);
	NSLog(@"A: %f", p_mCountA);
	NSLog(@"B: %f", p_mCountB);
	NSLog(@"Bonus is: %f", p_mResult);
    
    [cLmCalculator1 printResult];
    return 0;
}


2016-10-12 13:39:01.824 a.out[13987] ==========Calculate Result==========
2016-10-12 13:39:01.825 a.out[13987] TotalSalary: 62000
2016-10-12 13:39:01.825 a.out[13987] TotalBonus: 12290
2016-10-12 13:39:01.825 a.out[13987] TimesSalaryWithBonus: 125709500
2016-10-12 13:39:01.825 a.out[13987] SquareSalary: 681480000
2016-10-12 13:39:01.825 a.out[13987] SquareBonus: 30837400
2016-10-12 13:39:01.825 a.out[13987] ==========Predict Result==========
2016-10-12 13:39:01.825 a.out[13987] If salary = 6000
2016-10-12 13:39:01.825 a.out[13987] A: 2374.000000
2016-10-12 13:39:01.825 a.out[13987] B: 0.002009
2016-10-12 13:39:01.825 a.out[13987] Bonus is: 2386.055908
2016-10-12 13:39:01.825 a.out[13987] ==========Implementation Result==========
2016-10-12 13:39:01.825 a.out[13987] Salary: 0
2016-10-12 13:39:01.825 a.out[13987] Bonus: 0
2016-10-12 13:39:01.825 a.out[13987] TotalSalary: 62000
2016-10-12 13:39:01.825 a.out[13987] TotalBonus: 12290
2016-10-12 13:39:01.825 a.out[13987] TimesSalaryWithBonus: 125709500
2016-10-12 13:39:01.825 a.out[13987] SquareSalary: 681480000
2016-10-12 13:39:01.825 a.out[13987] SquareBonus: 30837400
2016-10-12 13:39:01.825 a.out[13987] CountA: 2374.000000
2016-10-12 13:39:01.825 a.out[13987] CountB: 0.002009

相关文章推荐

简单线性回归(Simple Linear Regression)问题和举例

简单线性回归(Simple Linear Regression)问题和举例 0. 前提介绍: 为什么需要统计量? 统计量:描述数据特征 0.1 集中趋势衡量 0.1.1均...
  • A784586
  • A784586
  • 2017年04月26日 17:13
  • 784

R tutorial 15 - Simple linear regression 线性回归 (2)

/* Simple linear regression 简单回归分析 假设花红是3250、经过预测月薪将是10060.22元。 图中红色就是预期花红和月薪的关系。 */ userName ...

[机器学习]01.线性回归(Linear Regression) 梯度下降(Gradient Decent)

线性回归(Linear Regression) 1.假设函数( Hypothesis Function) 给定了一系列离散的点,求一条直线与这些点最吻合。将这条直线的函数叫假设函数,设为 改变thet...

机器学习心得01 线性回归 linear regression

应用示例:神奇宝贝经过进化后的能量值 f(x)=y x=(xs,xhp,xw,xh) 如Xh表示神奇宝贝的身高Xw表示神奇宝贝的重量Xs表示种类 第一步:建模(model) 找一组函数...

机器学习笔记01:线性回归(Linear Regression)和梯度下降(Gradient Decent)

最近在Coursera上看吴大神的Machine Learning,感觉讲的真的很棒。所以觉得应该要好好做做笔记,一方面是加强自己对ML中一些方法的掌握程度和理解,另一方面也能方便自己或者同样爱好ML...
  • Artprog
  • Artprog
  • 2016年04月09日 14:33
  • 4067

Gradient Descent for Linear Regression,线性回归的梯度下降算法

针对线性回归模型,我们假设

简单线性回归的随机梯度下降算法实现:Linear Regression - SGD

object sgdDemo{ def main(args: Array[String]): Unit = { val featuresMatrix: List[List[Double]]...

【scala】简单线性回归的随机梯度下降算法实现:Linear Regression - SGD

基于Scala编程实现简单的“随机梯度下降法(SGD, stochastic gradient descent) - 线性回归(LR, linear regression)”。...

机器学习经典算法详解及Python实现--线性回归(Linear Regression)算法

回归是统计学中最有力的工具之一。回归算法用于连续型分布预测,针对的是数值型的样本,使用回归,可以在给定输入的时候预测出一个数值,这是对分类方法的提升,因为这样可以预测连续型数据而不仅仅是离散的类别标签...

线性回归 (linear regression model) 原理与代码实现

这篇博文是在总结网易公开课上ng的机器学习第二讲和周志华老师书上线性回归的个人总结。准备做一个系列笔记,希望能坚持。 代码放在文后什么是线性模型通俗一点来讲,就是我们希望用一个线性组合,来拟...
  • macanv
  • macanv
  • 2016年11月04日 17:59
  • 1607
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:算法导论 Algorithms 01 - 线性回归 Simple linear regression
举报原因:
原因补充:

(最多只允许输入30个字)