반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

sklearn.non-linear regression(비선형회귀) 본문

[AI]/python.sklearn

sklearn.non-linear regression(비선형회귀)

givemebro 2020. 4. 19. 21:56
반응형

linear_model.LinearRegression.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

from sklearn.datasets import load_breast_cancer
# data 준비
cancer =load_breast_cancer()

# column 컨트롤러
col1=0
col2=6

# visualization
plt.scatter(cancer.data[:,col1],cancer.data[:,col2],c=cancer.target,alpha=0.3)

 

# 선형 회귀선
from sklearn.linear_model import LinearRegression

model=LinearRegression()
model.fit(cancer.data[:,[col1]],cancer.data[:,col2])

xx=np.arange(5,30,0.1)
yy=xx*model.coef_+model.intercept_

plt.scatter(cancer.data[:,col1],cancer.data[:,col2],c=cancer.target,alpha=0.3)
plt.plot(xx,yy,'r-',lw=3)

# 비선형회귀선(2차식)

# 기존 X에 X값의 제곱값을 열로 추가
X=np.c_[cancer.data[:,col1],(cancer.data[:,col1]**2)]
y=cancer.data[:,col2]

model=LinearRegression()
model.fit(X,y)

xx=np.arange(0,30,0.1)
yy=xx*model.coef_[0]+(xx**2)*model.coef_[1]+model.intercept_

plt.scatter(cancer.data[:,col1],cancer.data[:,col2],c=cancer.target,alpha=0.3)
plt.plot(xx,yy,'r-',lw=3)

# 비선형회귀선(3차식)

# 기존 X에 X값의 제곱값과 세제곱값을 열로 추가

X=np.c_[cancer.data[:,col1],(cancer.data[:,col1])**2,(cancer.data[:,col1])**3]
y=cancer.data[:,col2]

model=LinearRegression()
model.fit(X,y)

xx=np.arange(0,40,0.1)
yy=xx*model.coef_[0]+(xx**2)*model.coef_[1]+(xx**3)*model.coef_[2]+model.intercept_

plt.scatter(cancer.data[:,col1],cancer.data[:,col2],c=cancer.target,alpha=0.3)
plt.plot(xx,yy,'r-',lw=3)

반응형
Comments