Linear Regression in ML and AI with Examples and Project

 Introduction to linear Regression











Code python:-

Import numpy as np
Import pandas as pd
Import matplotlib.pyplot as plt
Import seaborn as sns
Df = pd.read_csv(‘USA_Housing.csv’)
Df.head()
Df.info()-- total information of dataset
Df.describe()-calculated estimation
Df.columns-names of columns
Sns.pairplot(df)
Sns.distplot(df[‘Price’])
Sns.heatmap(df.corr(),annot=True)shows correlation
Training a data using Linear regression
First of all designing the features
We need all the columns name
Df.columns
Select the names of the columns leaving price and address
X = df[[names of columns]]
y= df[‘Price’] the labels
From sklearn.cross_validation import train_test_split
X_train,X_test,y_train,y_test =
train_test_split(X,y,test_size=0.4,random_state=101)
Test_size is that amount of total data you want to allocate for testset
40% given here
Random_state is a number given to randomly split the data
Creating and training the model
From sklearn.linear_model import LinearRegression
Initialize linear model
Lm = LinearRegression()
Fit the data
Lm.fit(X_train,y_train)
Print(lm.intercept_)
Lm.coef_
Cf = Pd.Dataframe(lm.coef_,X.columns,columns=[‘Coeff’])
Print(cf)
Applying ml to boston dataset
From sklearn.datasets import load_boston
Boston = load_boston()
Boston.keys()
Print(Boston[‘DESCR’])
Print(Boston[‘data’])
Print(Boston[‘feature_names’])
Print(Boston[‘target’])
Getting Prediction from our model
Predictions = lm.predict(X_test)  pass features of testing
Predictions  print
Plt.scatter(y_test,predictions)
Sns.distplot((y_test-predictions))residuals or difference between original
result and your prediction
Evalution Matrix


From sklearn import metrics
Metrics.mean_absolute_error(y_test,predictions)
Metrics.mean_squared_error(y_test,predictions)
Np.sqrt(Metrics.mean_absolute_error(y_test,predictions))
Print all the three above given expressions to find out the errors












Comments

Popular posts from this blog

Logistic Regression in ML and AI with Examples and Projects