Python XY-Scatter Plot with Linear Regression: Demos the numpy, matplotlib, and csv modules

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ก.ย. 2024
  • In this video we look at a Python program the reads some x-y data from a csv file. The x's are the speed (in kilometers per hour) at which someone performs an exercise, and the y's are the heart rate (in beats per minute). The program uses the x and y lists and matplotlib's scatter method to make an xy-scatter plot. Next it uses numpy's polyfit method to perform a linear regression (aka create a linear model, aka perform a least-squares fit) of the data. The method yields the best-fit slope and intercept. The program uses numpy's linspace to generate a sequence of x values, and those are in turn used to plot the fit line. Finally the program creates a display version of the best fit line and uses the matplotlib's text() method to add it to the displayed data and fit line.

ความคิดเห็น • 2

  • @thomasblum9803
    @thomasblum9803  ปีที่แล้ว

    I read that polyfit is old-fashioned and the fit within polynomial is now preferred, so here's a possible replacement
    #slope, intercept = np.polyfit(speeds, heart_beats, deg=1)
    the_fit = np.polynomial.Polynomial.fit(speeds, heart_beats, deg=1)
    intercept = the_fit(0)
    slope = the_fit(1)-the_fit(0)
    print(the_fit) # weirdly "scaled"(?) result
    print(the_fit.convert()) # more what one expects

  • @thomasblum9803
    @thomasblum9803  ปีที่แล้ว

    www1.lasalle.edu/~blum/c341wks/LinearRegression_heart.zip