Skip to main content

Simple linear regression model with scikit-learn

Simple Leaner Regression Model is use to find the relation ship between two variable. It is commonly used in the predict analysis. Suppose we want to know price of pizza on the basis of size. We will train a model on the different size of pizza and its price. Then we will give the size of the pizza to train model it will predict its price. suppose we have different size of pizza x =  [[6], [8], [10], [14], [18]]] and its price y = [[7], [9], [13], [17.5], [18]]. Let's implement this problem it scikit-learn. Firs import Linear Regress from scikit-learn pakage.
from sklearn.linear_model import LinearRegression
Import Numpy module because when b give the data to model it will only accept if the data in Numpy array.
from sklearn.linear_model import LinearRegression
import numpy as np
Import matplot libraray which use to Draw a plot of our data
import matplotlib.pyplot as plt
x = [[6], [8], [10], [14], [18]]
x = np.reshape(x, (-1, 1))
Her we rehshape array to 2d because it accept 2d array otherwise it will show error.
y = [[7], [9], [13], [17.5], [18]]
y = np.reshape(y, (-1, 1))
model = LinearRegression()
model.fit(x, y)
train model using fit function. model.predict is use to get the output of the model

print('A 12" pizza shuld cost: $%.2f' % model.predict([12])[0])
plt.figure()
plt.title('Piza price ploted againt diameter')
plt.xlabel('Diameter in inch')
plt.ylabel('Pice in Dollars')
plt.plot(x, y, 'k.')
plt.axis([0, 25, 0, 25])
plt.grid(True)
plt.show()

Here complete code

import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
x = [[6], [8], [10], [14], [18]]
x = np.reshape(x, (-1, 1))
y = [[7], [9], [13], [17.5], [18]]
y = np.reshape(y, (-1, 1))
model = LinearRegression()
model.fit(x, y)
print('A 12" pizza shuld cost: $%.2f' % model.predict([12])[0])
plt.figure()
plt.title('Piza price ploted againt diameter')
plt.xlabel('Diameter in inch')
plt.ylabel('Pice in Dollars')
plt.plot(x, y, 'k.')
plt.axis([0, 25, 0, 25])
plt.grid(True)
plt.show()


Comments

Popular posts from this blog

How parse XML file Dataset using python

Parse XML file and Store data in CSV file for machine learning Algorithms. import xml.etree.ElementTree as ET import os import csv path = 'G:\salman' with open('names.csv', 'a') as csvfile:     fieldnames = ['pair_id', 'e1', 'e2', 'Sentance']     writer = csv.DictWriter(csvfile, fieldnames=fieldnames)     for filename in os.listdir(path):         if not filename.endswith('.xml'): continue         fullname = os.path.join(path, filename)         tree = ET.parse(fullname)         lst = tree.findall('sentence')         for i in lst:             i_ = i.findall('pair')             for elem in i_:                 if elem.attrib['ddi'] == 'true':                     writer.writerow({'pair_id': elem.att...

How start Data scientist journey

(Read only if you're interested in getting into Data Science or tag, if you know someone who is) Everyday, I get inboxes from people on LinkedIn/Fb who are passionate about learning Data Science, but they don't know how to start because they have no prior knowledge on this field. The internet has a lot of useful resources that can be availed to get into this field and I thought I take out the time to share a few based on my little experience in this domain. The following is one of the lists of steps that you can follow if you want to jump into learning Data Science. 1. The initial and most important thing is to have your core concepts on machine learning to be strong. I believe that just like OOP concepts are the basic pillars for a Software Developer, same goes to ML concepts for a Data Scientist. This course is 11 weeks long, but it will develop your ML base. Therefore, this course from Andrew Ng on ML should be your first step. And as most of you already know, you...