Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python linear regression


import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset('tips')
sb.regplot(x = "total_bill", y = "tip", data = df)
plt.show()
Comment

python linear regression

>>> from scipy import stats
>>> import numpy as np
>>> x = np.random.random(10)
>>> y = np.random.random(10)
>>> slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
Comment

linear regression python code

import numpy as np
import matplotlib.pyplot as plt
 
def estimate_coef(x, y):
    # number of observations/points
    n = np.size(x)
 
    # mean of x and y vector
    m_x = np.mean(x)
    m_y = np.mean(y)
 
    # calculating cross-deviation and deviation about x
    SS_xy = np.sum(y*x) - n*m_y*m_x
    SS_xx = np.sum(x*x) - n*m_x*m_x
 
    # calculating regression coefficients
    b_1 = SS_xy / SS_xx
    b_0 = m_y - b_1*m_x
 
    return (b_0, b_1)
 
def plot_regression_line(x, y, b):
    # plotting the actual points as scatter plot
    plt.scatter(x, y, color = "m",
               marker = "o", s = 30)
 
    # predicted response vector
    y_pred = b[0] + b[1]*x
 
    # plotting the regression line
    plt.plot(x, y_pred, color = "g")
 
    # putting labels
    plt.xlabel('x')
    plt.ylabel('y')
 
    # function to show plot
    plt.show()
 
def main():
    # observations / data
    x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
 
    # estimating coefficients
    b = estimate_coef(x, y)
    print("Estimated coefficients:
b_0 = {}  
          
b_1 = {}".format(b[0], b[1]))
 
    # plotting regression line
    plot_regression_line(x, y, b)
 
if __name__ == "__main__":
    main()
Comment

PREVIOUS NEXT
Code Example
Python :: change tuple python 
Python :: how to add to a list python 
Python :: class attributes in python 
Python :: how print array in python with clean dublication 
Python :: drf not getting form 
Python :: streamlit - Warning: NumberInput value below has type int so is displayed as int despite format string %.1f. 
Python :: API curl python pandas 
Python :: Chudnovsky algorithm in python codes 
Python :: Python Print Variable Using the string formatting with positional arguments {} 
Python :: how to check if object is of file type in python3 
Python :: python calculated row in dataframe subtract 
Python :: import pycocotools._mask as _mask error Expected 80, got 88 
Python :: pytonh leer txt y quitar tildes acentos 
Python :: how to show rosbag file python 
Python :: put cropped image in original image name folder python 
Python :: time python 
Python :: gtts python 
Python :: Making a delete request using python 
Python :: cv2 and PIL BRG to RGB 
Python :: any python type hint 
Python :: python not showing in control panel but showing not installed 
Python :: where is a package stored python 
Python :: 9x9 grid tkinter 
Python :: dobj in spacy 
Python :: pandas recognize type from strings 
Python :: python ternary statement 
Python :: WSGIPassAuthorization on 
Python :: Python NumPy ascontiguousarray Function Example Scalar to an array 
Python :: python if column is null then 
Python :: numpy distance of consecutive elements 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =