Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge | Correlation
Explore the Linear Regression Using Python

book
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).

  1. [Line #19] Find the correlation coefficient between x and y (in any convenient way), the slope, and the intercept of a straight line which shows the trend.
  2. [Lines #22-24] Print these values in the order mentioned above.
  3. [Line #27] Build line using function line() and save in built_line variable using list(map()).
  4. [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
___
___
toggle bottom row
some-alt