Linear regression is used when you are predicting a continuous number, more information about this algorithm can be found here. The below code will help to create a linear regression model.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#### Creating Gym Data for regression in Python #### import pandas as pd import numpy as np ColumnNames=['Hours','Calories', 'Weight'] DataValues=[[ 1.0, 2500, 95], [ 2.0, 2000, 85], [ 2.5, 1900, 83], [ 3.0, 1850, 81], [ 3.5, 1600, 80], [ 4.0, 1500, 78], [ 5.0, 1500, 77], [ 5.5, 1600, 80], [ 6.0, 1700, 75], [ 6.5, 1500, 70]] # Create the Data Frame GymData=pd.DataFrame(data=DataValues,columns=ColumnNames) GymData.head() # Separate Target Variable and Predictor Variables TargetVariable='Weight' Predictors=['Hours','Calories'] X=GymData[Predictors].values y=GymData[TargetVariable].values # Split the data into training and testing set from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) ################################################################ ###### Linear Regression in Python ####### import pandas as pd from sklearn.linear_model import LinearRegression RegModel = LinearRegression() #Printing all the parameters of Linear regression print(RegModel) #Creating the model on Training Data LREG=RegModel.fit(X_train,y_train) prediction=LREG.predict(X_test) from sklearn import metrics #Measuring Goodness of fit in Training data print('R2 Value:',metrics.r2_score(y_train, LREG.predict(X_train))) #Measuring accuracy on Testing Data print('Accuracy',100- (np.mean(np.abs((y_test - prediction) / y_test)) * 100)) #Printing some sample values of prediction TestingDataResults=pd.DataFrame(data=X_test, columns=Predictors) TestingDataResults[TargetVariable]=y_test TestingDataResults[('Predicted'+TargetVariable)]=prediction TestingDataResults.head() |
Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!
