Skip to main content

Machine Learning Linear Regression


Machine Learning Linear Regression

In this post, we will learn Machine Learning Techniques Linear Regression using  in Python.

Requirement

For this tutorial, following library  should be installed in your system.
  1. Pandas
  2. Quandl
  3. numpy
  4. sklearn
Linear Regression: Taking continuous data and fitting a best possible function in it.
References:
Regression identifying data-set and importing it and making it into useful format.
Code snippet used in video:
 import pandas as pd  
   
 import quandl  
   
 import math  
   
 df=quandl.get('WIKI/GOOGL')   
   
 df=df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume']]  
   
 df['HL_PCT']=(df['Adj. High']-df['Adj. Close'])/df['Adj. Close']*100.00  
   
 df['PCT_Change']=(df['Adj. Close']-df['Adj. Open'])/df['Adj. Open']*100.00  
   
 df=df[['Adj. Close','HL_PCT','PCT_Change','Adj. Volume']]  
   
 print(df.head())  

Regression Feature Identification:  

References 

 Further code:   
   
 forecast_col='Adj. Close'  
   
 df.fillna(-99999,inplace =True)  
   
 forecast_out=int(math.ceil(0.01*len(df)))  
   
 df['label']=df[forecast_col].shift(-forecast_out)  
   
 df.dropna(inplace=True)  
   
 print(df.head())  
   
 print(df.tail()) 

Regression Training and Testing:

 import pandas as pd  
   
 import quandl  
   
 import math  
   
 import numpy as np #Used in creating arrays etc as python doesn't supports array  
   
 from sklearn import preprocessing, model_selection , svm  
   
 from sklearn.linear_model import LinearRegression  
   
 df=quandl.get('WIKI/GOOGL')  
   
 df=df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume']]  
   
 df['HL_PCT']=(df['Adj. High']-df['Adj. Close'])/df['Adj. Close']*100.00  
   
 df['PCT_Change']=(df['Adj. Close']-df['Adj. Open'])/df['Adj. Open']*100.00  
   
 df=df[['Adj. Close','HL_PCT','PCT_Change','Adj. Volume']]  
   
 forecast_col='Adj. Close'  
   
 df.fillna(-99999,inplace =True)  
   
   
   
 forecast_out=int(math.ceil(0.01*len(df)))  
   
 print(forecast_out)  
   
 df['label']=df[forecast_col].shift(-forecast_out)  
   
 df.dropna(inplace=True)  
   
 print(df.head())  
   
 print(df.tail())  
   
   
   
 X=np.array(df.drop(['label'],1)) #Our features  
   
 y=np.array(df['label'])  
   
 X=preprocessing.scale(X)  
   
 df.dropna(inplace=True)  
   
 X_train, X_test, y_train, y_test=model_selection.train_test_split(X,y, test_size=0.2)  
   
 clf=LinearRegression(n_jobs=100)  
   
 clf.fit(X_train, y_train)  
   
 accuracy=clf.score(X_test, y_test) #Accuracy is squared error  
   
 print(accuracy)  

Using Support vector regression classifier

 clf=svm.SVR()  
   
 clf.fit(X_train, y_train)  
   
 accuracy=clf.score(X_test, y_test) #Accuracy is squared error  
   
 print(accuracy)  
   
   



###Regression forecasting and predicting



Comments

Popular posts from this blog

Time, Speed and Distance

Time, Speed And Distance  In this post, We will learn about time, speed and distance. Important Formula Distance = Time x Speed Time = Distance/Speed Speed = Distance/time Important Units Distance: meter (m), kilometre (km) Time: Hour (h), second (s) speed: meter/second (m/s), kilometre/hour  (km/h) Important Conversion 1 km = 1000 meter 1 h = 3600 s 1 km/h = (5/18) m/s 1 m/s = (18/5) km/h Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 

Joint Entrance Exam (JEE): Probability Syllabus

JEE: Probability Syllabus Probability  is a very interesting and different topic of mathematics which doesn't require your previous knowledge of any other chapters other than permutation and combination.  Following topics are the part of Jee syllabus Addition and multiplication rules of probability Addition and multiplication rules of probability Conditional Probability Bayes Theorem Independence of events Computation of probability of events using permutations and combinations Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Haryana Public Service Commission: HPSC 2018

Haryana Public Service Commission: HPSC 2018 Company Name         :  Haryana Public Service Commission: HPSC 2018 Recruitment Post Name                       :   Civil Judge State                            :  Haryana Salary                           :  Rs   27,700-44,770 Number of Vacancy   :  107 Eligibility                     :  Bachelor's Degree in Law Last Date to Apply     :  30-09-2018 Application Details    :  Notification Apply Online              :  Apply Here Government Jobs In  Haryana Please write comments if you f...

Securities and Exchange Board of India Recruitment 2018

Securities and Exchange Board of India  Company Name SEBI Post Name Assistant Manager State India Salary RS 28150-55600 Number of Vacancy 120 Eligibility Graduation Last Date 07-10-2018 Application Details Job Notification Apply Online Job Application Securities and Exchange Board of India (SEBI) Recruitment 2018    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.