linear regression python sklearn code example

Example 1: scikit learn linear regression

from sklearn.linear_model import LinearRegression
X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
y = np.dot(X, np.array([1, 2])) + 3
reg = LinearRegression().fit(X, y)
reg.score(X, y)
reg.coef_
reg.intercept_
reg.predict(np.array([[3, 5]]))

Example 2: python sklearn knn regression example

KNeighborsRegressor(algorithm='auto', leaf_size=30, metric='minkowski',
          metric_params=None, n_jobs=1, n_neighbors=8, p=2,
          weights='uniform')

Example 3: scikit learn linear regression

from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.score(X, y) #Fit linear model
reg.coef_ #Estimated coefficients for the linear regression problem
reg.predict(y) #Predict using the linear model