as - ahmad alhayek

as

Simple linear regression is an approach for predicting a response using a single feature. It is assumed that the two variables are linearly related. Hence, we try to find a linear function that predicts the response value (y) as accurately as possible as a function of the feature or independent variable (x). For the implementation of algorithms in machine learning, we need to install the modules numpy, scipy, pandas, matplotlib, statsmodels and sklearn in Python. Let us see the implementation of simple linear regression in Python. For example, we may use linear regression to predict the price of the stock market Stock_Index_Price (our dependent variable) based on the following input variables: Interest_Rate . Consider the following stock market data. This dataset is used for linear regression implementation in Python.

We will have to validate that several assumptions are met before we apply linear regression models. Most notably, we have to make sure that a linear relationship exists between the dependent variable and the independent variable/s. In this example, we may want to check whether a linear relationship exists between the following.

The Stock_Index_Price (dependent variable) and the Interest_Rate (independent variable).

Following python code shows how to implement linear regression in Python.

 import pandas as pd import matplotlib.pyplot as plt from pandas import DataFrame from sklearn import linear_model #Reading the input data from a csv file df=DataFrame(pd.read_csv("stock.csv")) #scatterplot for Interest_Rate vs Stock_Index_Price #to see whether the relationship is linear plt.scatter(df['Interest_Rate'], df['Stock_Index_Price'], color='red') plt.title('Stock Index Price Vs Interest Rate', fontsize=14) plt.xlabel('Interest Rate', fontsize=14) plt.ylabel('Stock Index Price', fontsize=14) plt.grid(True) plt.show() #Here we have 1 variable for linear regression # Simple linear regression X = df[['Interest_Rate']] Y = df['Stock_Index_Price'] # Model fitting with sklearn regr = linear_model.LinearRegression() regr.fit(X, Y) #Displaying Intercept and coefficients print('Intercept: \n', regr.intercept_) print('Coefficients: \n', regr.coef_) # prediction with sklearn for all the interest rates New_Interest_Rate = df[['Interest_Rate']] df1=DataFrame(regr.predict(New_Interest_Rate)) print('Predicted Stock Index Price: \n',) print(df1) 

The Python code in IDLE is pasted below.

import pandas as pd import matplotlib.pyplot as plt from pandas import DataFrame from sklearn import linear model #Reading th

The stock.csv file looks like as follows.

YearMonthInterest_RateUnemployment_RateStock_Index_Price
2017122.755.31464
2017112.55.31394
2017102.55.31357
201792.55.31293
201782.55.41256
201772.55.61254
201762.55.51234
201752.255.51195
201742.255.51159
201732.255.61167
2017225.71130
2017125.91075
201612261047
2016111.755.9965
2016101.755.8943
201691.756.1958
201681.756.2971
201671.756.1949
201661.756.1884
201651.756.1866
201641.755.9876
201631.756.2822
201621.756.2704
201611.756.1719

The output is pasted below.

Stock Index Price Vs Interest Rate 1500 Stock Index Price 1.8 2.0 2.6 2.8 2.2 1 2.4 Interest Rate

Intercept: -99.46431881371655 Coefficients: [564.20389249] Predicted Stock Index Price: 00 JOU WNHO 10 11 12 1452.096386 1311

Interpretation of the Result

Simple linear regression is of the form y=w0+w1x. The output shows w0 (Intercept) as

-99.46431881371655 and w1 (Coefficient) as 564.20389249. According to the above example, the equation becomes

Stock_Index_Price= w0+w1* Interest_Rate

             i.e, Stock_Index_Price= -99.46431881371655 +564.20389249* Interest_Rate

Stock_Index_Price = 1452.09638554 which is exactly the predicted stock index price for the first record. Likewise the predicted stock_price in the output shows for the following records in the stock.csv file

ليست هناك تعليقات:

إرسال تعليق

ahmad alhayek تصميم ahmad alhayek جميع الحقوق محفوظة 2016

صور المظاهر بواسطة sndr. يتم التشغيل بواسطة Blogger.