| |
|
| |
|
| | '''
|
| | The slope is defined as how much calorie burnage increases, if average pulse increases by one. It tells us how "steep" the diagonal line is.
|
| | We can find the slope by using the proportional difference of two points from the graph.
|
| | If the average pulse is 80, the calorie burnage is 240
|
| | If the average pulse is 90, the calorie burnage is 260
|
| | We see that if average pulse increases with 10, the calorie burnage increases by 20.
|
| | '''
|
| |
|
| |
|
| | '''
|
| | f(x2) = Second observation of Calorie_Burnage = 260
|
| | f(x1) = First observation of Calorie_Burnage = 240
|
| | x2 = Second observation of Average_Pulse = 90
|
| | x1 = First observation of Average_Pulse = 80
|
| | '''
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | def slope(x1,y1, x2,y2):
|
| | s = (y2-y1)/(x2-x1)
|
| | return s
|
| | print(slope(80,240,90,260))
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | '''
|
| | Does it make sense that average pulse is zero?
|
| | No, you would be dead and you certainly would not burn any calories.
|
| | However, we need to include the intercept in order to complete the mathematical function's ability to predict Calorie_Burnage correctly.
|
| | '''
|
| | '''
|
| | Other examples where the intercept of a mathematical function can have a practical meaning:
|
| | Predicting next years revenue by using marketing expenditure (How much revenue will we have next year, if marketing expenditure is zero?). It is likely to assume that a company will still have some revenue even though if it does not spend money on marketing.
|
| | Fuel usage with speed (How much fuel do we use if speed is equal to 0 mph?). A car that uses gasoline will still use fuel when it is idle.
|
| | '''
|
| |
|
| |
|
| |
|
| | import pandas as pd
|
| | import numpy as np
|
| | health_data = pd.read_csv('data-calculate-slope-and-intercept.csv', header=0, sep=',')
|
| | x = health_data['Average_Pulse']
|
| | y = health_data['Calorie_Burnage']
|
| | slope_intercept = np.polyfit(x,y,1)
|
| | print(slope_intercept)
|
| | '''
|
| | Example Explained:
|
| | Isolate the variables Average_Pulse (x) and Calorie_Burnage (y) from health_data.
|
| | Call the np.polyfit() function.
|
| | The last parameter of the function specifies the degree of the function, which in this case is "1".
|
| | '''
|
| |
|
| | '''
|
| | Tip: linear functions = 1.degree function. In our example, the function is linear, which is in the 1.degree. That means that all coefficients (the numbers) are in the power of one.
|
| | '''
|
| |
|
| | '''
|
| | We have now calculated the slope (2) and the intercept (80). We can write the mathematical function as follow:
|
| | Predict Calorie_Burnage by using a mathematical expression:
|
| | '''
|
| |
|
| | '''
|
| | Task:
|
| | Now, we want to predict calorie burnage if average pulse is 135.
|
| | Remember that the intercept is a constant. A constant is a number that does not change.
|
| | We can now substitute the input x with 135:
|
| | '''
|
| |
|
| |
|
| |
|
| |
|
| | def my_function(x):
|
| | return 2 *x + 80
|
| | print(my_function(135))
|
| |
|
| |
|
| | def my_function(x):
|
| | return 2*x + 80
|
| | print(my_function(140))
|
| |
|
| | def my_function(x):
|
| | return 2*x + 80
|
| | print(my_function(150))
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | import pandas as pd
|
| | import matplotlib.pyplot as plt
|
| | health_data = pd.read_csv('data-linear-functions.csv', header=0, sep=',')
|
| | health_data.plot(x ='Average_Pulse', y='Calorie_Burnage', kind='line')
|
| |
|
| |
|
| | plt.title('Average Pulse vs Calorie Burnage')
|
| | plt.xlabel('Average Pulse')
|
| | plt.ylabel('Calorie Burnage')
|
| |
|
| | plt.xlim(xmin=0)
|
| | plt.ylim(ymin=0)
|
| | plt.show()
|
| |
|
| |
|
| |
|
| | '''
|
| | Example Explained
|
| | Import the pyplot module of the matplotlib library
|
| | Plot the data from Average_Pulse against Calorie_Burnage
|
| | kind='line' tells us which type of plot we want. Here, we want to have a straight line
|
| | plt.ylim() and plt.xlim() tells us what value we want the axis to start and stop on.
|
| | plt.show() shows us the output
|
| | ''' |