Beginner Machine Learning Projects

 

Machine Learning Projects - Machine Learning Applications


Title: Predicting temperature by the number of cricket chirps per second using Linear Regression model.

Problem: Many techniques and tools have been developed for measuring temperature in these decades. Some techniques are the Silicon diode method, Biometalic device, Change of state of a sensor, etc. Mentionables that, those methods are costly. A cricket's chirping (Cricket Chirps is one type of insect) can be used to approximate the temperature in Fahrenheit or in Degree Celsius. It is a simple and inexpensive way to predict the temperature using Cricket Chirps.

Dataset: Dataset has been made from real-life observation. The dataset is collected from the web and prepare the dataset for this mini-project.

Programming Language: For the implementation of this problem, we will use the Python language. Also, we will use python's sklearn (scikit learn) packages library for using a linear model.

Solution steps:
  1. Pre-process the dataset (using python-pandas package).
  2. Split the dataset for train and test samples (using python-ScikitLearn package).
  3. Apply Linear Regression model (a built-in scikit learn model) on the dataset.
  4. Test the model with a new dataset's sample.

Sample input: 17
Sample output: For 17 chirps/sec, The temperature is: 27.0 degrees celsius

Code:

----------------start------------

#importing python package

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

import sklearn


#Dataset

x=[[20],[16], [19.79999924], [18.39999962],

[17.10000038], [15.5], [14.69999981],

[17.10000038], [15.39999962], [16.20000076],

[15], [17.20000076], [16], [17], [14.39999962]]
y=[[31.444443594444], [21.99999915],

[34.05555725] ,[29.05555725],

[26.99999915], [23.999998305556], [20.94444275],

[27.7778], [20.777778627778], [28.500001694444],

[26.444443594444], [28.111110261111], [26.99999915],

[28.61111], [24.611112805556]]

c1=np.array(x)

c2=np.array(y)

#dC=degree Celsius

dataset = pd.DataFrame({'Chirps/Sec ': c1[:, 0], 

'Temperature(dC)': c2[:, 0]})  

#Table view of the Dataset

print(dataset)

#Split the data into train and test dataset

from sklearn.model_selection import train_test_split

x_train,x_test,y_train,y_test=train_test_split(

x,y,test_size=0.3,train_size=0.7,random_state=2)

#Fitting Simple Linear regression data model to train data set

from sklearn.linear_model import LinearRegression

LR=LinearRegression()

LR.fit(x_train,y_train)

#predict the test set

y_pred_test_data=LR.predict(x_test)

# Visualising the Training set results in a scatter plot

plt.scatter(x_train, y_train, color = 'red')

plt.plot(x_train, LR.predict(x_train), color = 'green')

plt.title('Cricket Chirps vs Temperature (Training set)')

plt.xlabel('Cricket Chirps (chirps/sec 

for the striped ground cricket) ')

plt.ylabel('Temperature (degree Celsius)')

plt.show()

# Visualising the test set results in a scatter plot

plt.scatter(x_test, y_test, color = 'red')

plt.plot(x_train, LR.predict(x_train), color = 'green')

plt.title('Cricket Chirps vs Temperature (Test set)')

plt.xlabel('Cricket Chirps (chirps/sec 

for the striped ground cricket) ')

plt.ylabel('Temperature (degree Celsius)')

plt.show()

#Testing the model with new dataset

x_new=[[17]]

y_pred_new=LR.predict(x_new)

print('For ',x_new[0][0], ' chirps/sec, The 

temperature is : ',round(y_pred_new[0][0]), ' degree celsius')

-----------------end------------


Outputs for the print function:

#Table view of the Dataset

print(dataset)

Chirps/Sec Temperature(dC)

0 20.000000 31.444444

1 16.000000 21.999999

2 19.799999 34.055557

3 18.400000 29.055557

4 17.100000 26.999999

5 15.500000 23.999998

6 14.700000 20.944443

7 17.100000 27.777800

8 15.400000 20.777779

9 16.200001 28.500002

10 15.000000 26.444444

11 17.200001 28.111110

12 16.000000 26.999999

13 17.000000 28.611110

14 14.400000 24.611113


print('For ',x_new[0][0], ' chirps/sec, The 

temperature is : ',round(y_pred_new[0][0]), ' degree celsius')

For 17 chirps/sec, The temperature is : 27 degree celsius



Plotted Graphs for this solution:

Graph of cricket chirps
Graph of cricket chirps





Post a Comment

Previous Post Next Post