Challenge
Let's do one more task as practice!
Завдання
Swipe to start coding
You have the dataset about Abbisian cats where the number of calories the cat eats every day of a certain weight is indicated (array x
- weight, y
- number of calories).
- [Line #19] Find the correlation coefficient between
x
andy
(in any convenient way), the slope, and the intercept of a straight line which shows the trend. - [Lines #22-24] Print these values in the order mentioned above.
- [Line #27] Build line using function
line()
and save inbuilt_line
variable usinglist(map())
. - [Lines #30-31] Build the line using
matplotlib.pyplot
functions.
Рішення
# Import the libraries
import matplotlib.pyplot as plt
from scipy import stats
# Initialize the data
x = [1.8, 2.24, 11.2, 7, 4.2, 9.3, 10.3, 6.3, 1.4, 5.8, 1.1, 4.3]
y = [111, 130, 450, 320, 195, 360, 402, 272, 85, 263, 105, 237]
# Add titles to axes
ax = plt.gca()
ax.set_xlabel('Cat weight (kg)')
ax.set_ylabel('Cat ration (calories)')
# The line shows the dependence between x and y
def line(x):
return slope * x + intercept
# Your code
# Get the linear regression parameters
slope, intercept, r, p, std_err = stats.linregress(x, y)
# Print results
print(r)
print(slope)
print(intercept)
# Find the line
built_line = list(map(line, x))
# Visualize the data
plt.plot(x, built_line)
plt.show()
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 2. Розділ 4
# Import the libraries
import matplotlib.pyplot as plt
from scipy import stats
# Initialize the data
x = [1.8, 2.24, 11.2, 7, 4.2, 9.3, 10.3, 6.3, 1.4, 5.8, 1.1, 4.3]
y = [111, 130, 450, 320, 195, 360, 402, 272, 85, 263, 105, 237]
# Add titles to axes
ax = plt.gca()
ax.set_xlabel('Cat weight (kg)')
ax.set_ylabel('Cat ration (calories)')
# The line shows the dependence between x and y
def line(x):
return slope * x + intercept
# Get the linear regression parameters
___
# Print results
___
___
___
# Find the line
built_line = ___
# Visualize the data
___
___