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

SIMPLE PYTHON HTTP SERVER

SIMPLE PYTHON HTTP SERVER In many use cases, we come across the situation where we need a simple server to test some command or test some applications. But creating a server programmatically is a time taking process. To solve the problem Python has created SimpleHTTPServer module. In this post, we will learn how to create a simple HTTP server in a single command on Ubuntu.  Create SimpleHTTPServe Using Commandline  python -m SimpleHTTPServer 8001 Above command will create a simple HTTP server for you at port 8001 Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Golang - Run & Compile

GOLANG RUN & COMPILE In the previous post, we have discussed how to install Go on Linux. In this post, We will discuss how to compile and run a Go program. Create folder HelloWorld mkdir HelloWorld cd HelloWorld Create file HelloWorld.go touch HelloWorld.go Copy below code into HelloWorld.go file package main import "fmt" func main() { fmt.Println("Hello, World") } Compile go build Run ./HelloWorld Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

BSNL Recruitment

BSNL Recruitment      Bharat Sanchar Nigam Limited (BSNL)  has released a notification for the post of   Management Trainees . Company Name BSNL Post Name    Management Trainees State India Salary Rs.  24,900-50,500 Number of Vacancy 300 Eligibility B.Tech Last Date 26-01-2019 Application Details Job Notification Apply Online Job Application BSNL Recruitment       Government Jobs in India Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Rozgardesh ( रोजगार देश )

South East Central Railway: SECR

South East Central Railway: SECR Company Name         :  South East Central Railway: SECR Post Name                       :   Apprentice State                            :  Maharashtra Salary                           :  Number of Vacancy   :  313 Eligibility                     :  10th Pass  Last Date to Apply     :  15-09-2018 Application Details    :  Notification Apply Online              :  Apply Here Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.