Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (19k points)

Could anyone of you share a piece of Machine Learning code, and explain in short what it does?

1 Answer

0 votes
by (11.7k points)
edited by

Well, let’s discuss Linear Regression.

There is a scenario where you need to know the price of the pizza. You have all the records of pizza you ate previously like the diameters and prices.

So let’s apply Machine Learning on this. 

Training Instance||  Diameter(in) |Price($)

       1                                  6               7

       2                                  8               9

       3                                 10             13

       4                                 14             17.5

       5                                 18             18

now visualize your data graphically :

  1. import matplotlib.pyplot as plt 

  2. X = [[6], [8], [10], [14], [18]] 

  3. y = [[7], [9], [13], [17.5], [18]] 

  4. plt.figure() 

  5. plt.title('Pizza price plotted against diameter') 

  6. plt.xlabel('Diameter in inches') 

  7. plt.ylabel('Price in dollars') 

  8. plt.plot(X, y, 'k.') 

  9. plt.axis([0, 25, 0, 25]) 

  10. plt.grid(True) 

  11. plt.show() 

Now let’s model this relationship using linear regression-

  1. from sklearn.linear_model import LinearRegression 

  2. # Training data 

  3. X = [[6], [8], [10], [14], [18]] 

  4. y = [[7], [9], [13], [17.5], [18]] 

  5. # Now Create and fit the model 

  6. model = LinearRegression() 

  7. model.fit(X, y) 

  8. print(A 12" pizza should cost: $%.2f' % model.predict([12])[0]) 

the output will be something like:

A 12" pizza should cost: $13.68

So this was a very basic, simple ML program.

If you want to become an expert in Machine Learning, checkout this Machine Learning Certification Course offered by Intellipaat. 

See this Machine Learning Course for more information :

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...