Linear Regression in Machine learning :
Where can Linear Regressions be used?
It is a very powerful techniques and can be used to understand the factors that influence profitability. It can be used to forecast sale in the coming months by analyzing the sales data for previous month. It can also be used to gain various insights into customers behaviour. By the end of the blog, we will build a model which looks like the below picture i.e, determine a line which best fit the data.
Example
In this example, we will use Pima Indian Diabetes dataset to select four of the attributes having best features with the help of chi-square statistical test.
from pandas import read_csv
from numpy import set_printoptions
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
path = r'C:\pima-indians-diabetes.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'age', 'class']
dataframe = read_csv(path, names=names)
array = dataframe.value
from pandas import read_csvfrom numpy import set_printoptionsfrom sklearn.feature_selection import SelectKBestfrom sklearn.feature_selection import chi2path = r'C:\pima-indians-diabetes.csv'names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'age', 'class']dataframe = read_csv(path, names=names)array = dataframe.value
X = array[:,0:8]
Y = array[:,8]
The following line of code will select the best features from dataset −
test = SelectKBest(score_func=chi2, k=4)
fit = test.fit(X,Y)
set_printoptions(precision=2)
print(fit.scores_)
featured_data = fit.transform(X)
print ("\nFeatured data:\n", featured_data[0:4])
OUTPUT:
[ 111.52 1411.89 17.61 53.11 2175.57 127.67 5.39 181.3
Featured data:
[[148. 0. 33.6 50.
[ 89. 94. 28.1 21. ]]
[ 85. 0. 26.6 31. ]
[ 183. 0. 23.3 32. ]
(Note: for python top 15 interview question click here)
https://www.facebook.com/pirawenpython/ https://www.facebook.com/groups/pirawenpython/