शुरू करेंमुफ़्त में शुरू करें

Linear regression एल्गोरिदम

Linear regression को सच में समझने के लिए यह जानना उपयोगी है कि एल्गोरिदम कैसे काम करता है। ols() का कोड सैकड़ों लाइनों का है क्योंकि उसे किसी भी फॉर्मूला और किसी भी डेटासेट के साथ काम करना होता है। लेकिन, एक ही डेटासेट पर simple linear regression के मामले में, आप कुछ ही लाइनों के कोड में linear regression एल्गोरिदम बना सकते हैं।

वर्कफ़्लो यह है:

  • सबसे पहले, sum of squares निकालने के लिए इस सामान्य सिंटैक्स का उपयोग करके एक फंक्शन लिखिए:
def function_name(args):
  # some calculations with the args
  return outcome
  • दूसरे, scipy के minimize फंक्शन का उपयोग करके उन coefficients को खोजिए जो इस फंक्शन को न्यूनतम करें।

Explanatory values (taiwan_real_estate की n_convenience कॉलम) x_actual के रूप में उपलब्ध हैं। Response values (taiwan_real_estate की price_twd_msq कॉलम) y_actual के रूप में उपलब्ध हैं।

minimize() भी लोड किया गया है।

यह अभ्यास पाठ्यक्रम का हिस्सा है

इंटरमीडिएट Regression with statsmodels in Python

पाठ्यक्रम देखें

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

# Complete the function
def calc_sum_of_squares(coeffs):
    # Unpack coeffs
    ____, ____ = ____
    # Calculate predicted y-values
    y_pred = ____ + ____ * ____
    # Calculate differences between y_pred and y_actual
    y_diff = ____ - ____
    # Calculate sum of squares
    sum_sq = ____
    # Return sum of squares
    return sum_sq
  
# Test the function with intercept 10 and slope 1
print(calc_sum_of_squares([10, 1]))
कोड संपादित करें और चलाएँ