티스토리 뷰
In [2]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
In [3]:
from sklearn.datasets import load_breast_cancer
cancer = load_breast_cancer()
In [4]:
col1 = 0
col2 = 6
plt.scatter(cancer.data[:,col1], cancer.data[:,col2], c= cancer.target, alpha=0.3)
Out[4]:
선형회귀¶
In [5]:
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(cancer.data[:,[col1]], cancer.data[:,col2])
model.coef_, model.intercept_
Out[5]:
In [6]:
xs = np.arange(5,30,0.1)
ys = xs*model.coef_[0] + model.intercept_
plt.scatter(cancer.data[:,col1], cancer.data[:,col2], c= cancer.target, alpha=0.3)
plt.plot(xs,ys,'r-',lw=3)
Out[6]:
2차식 회귀¶
In [11]:
X = np.c_[cancer.data[:,col1], (cancer.data[:,col1])**2]
y = cancer.data[:,col2]
model = LinearRegression()
model.fit(X, y)
model.coef_, model.intercept_
Out[11]:
In [14]:
xs = np.arange(0,30,0.1)
ys = xs*model.coef_[0] + (xs**2)*model.coef_[1] + model.intercept_
plt.scatter(cancer.data[:,col1], cancer.data[:,col2], c= cancer.target, alpha=0.3)
plt.plot(xs,ys,'r-',lw=3)
Out[14]:
3차식 회귀¶
In [6]:
X = np.c_[cancer.data[:,col1], (cancer.data[:,col1])**2, (cancer.data[:,col1])**3]
y = cancer.data[:,col2]
model = LinearRegression()
model.fit(X, y)
model.coef_, model.intercept_
Out[6]:
In [8]:
xs = np.arange(-10,50,0.1)
ys = xs*model.coef_[0] + (xs**2)*model.coef_[1] + (xs**3)*model.coef_[2] + model.intercept_
plt.scatter(cancer.data[:,col1], cancer.data[:,col2], c= cancer.target, alpha=0.3)
plt.plot(xs,ys,'r-',lw=3)
Out[8]:
지수함수 회귀 (Exponential Regression)¶
$$ y = exp(ax + b) $$ $$ log(y) = ax + b $$
In [17]:
X = cancer.data[:,[col1]]
y = np.log(cancer.data[:,col2]+0.01) # 값이 0인 데이터가 있어 0.01 을 더해줌
model = LinearRegression()
model.fit(X, y)
model.coef_, model.intercept_
Out[17]:
In [18]:
xs = np.arange(0,30,0.1)
ys = np.exp(xs*model.coef_[0] + model.intercept_) - 0.01
plt.scatter(cancer.data[:,col1], cancer.data[:,col2], c= cancer.target, alpha=0.3)
plt.plot(xs,ys,'r-',lw=3)
Out[18]:
멱함수 회귀 (Power Law Regression)¶
$$ y = a \cdot x^n $$ $$ log(y) = n \cdot log(x) + log(a) $$
In [20]:
X = np.log(cancer.data[:,[col1]])
y = np.log(cancer.data[:,col2]+0.01) # 값이 0인 데이터가 있어 0.01 을 더해줌
model = LinearRegression()
model.fit(X, y)
model.coef_, model.intercept_
Out[20]:
In [21]:
xs = np.arange(0,30,0.1)
ys = np.exp(model.intercept_) * (xs**model.coef_[0]) - 0.01
plt.scatter(cancer.data[:,col1], cancer.data[:,col2], c= cancer.target, alpha=0.3)
plt.plot(xs,ys,'r-',lw=3)
Out[21]:
멱함수와 복잡계¶
- 샘플을 만들어 적용해 보자 (n = -3/4 인 경우)
- 지진강도 대비 어떤 지역의 지진이 일어나는 횟수로 생각해 보자
In [10]:
xs = np.random.uniform(0.1, 2, size=100)
ys = 10 * (xs**(-3/4)) + np.random.normal(0, 0.5, size=len(xs)) # n=-3/4, a = 10
In [11]:
plt.scatter(xs, ys, alpha=0.3)
plt.title('Earthquake report', fontsize=30)
plt.xlabel('Power')
plt.ylabel('Count')
Out[11]:
In [13]:
plt.scatter(np.log(xs), np.log(ys))
plt.axis('equal')
Out[13]:
In [14]:
X = np.log(xs).reshape(-1,1)
y = np.log(ys)
model = LinearRegression()
model.fit(X, y)
model.coef_, model.intercept_ # n = model.coef_[0], a = exp(model.intercept_)
Out[14]:
In [68]:
input_x = np.arange(0.1,2.2,0.05)
pred_y = np.exp(model.intercept_) * (input_x**model.coef_[0])
plt.scatter(xs, ys, alpha=0.3)
plt.plot(input_x,pred_y,'r-')
Out[68]:
'beginner > 파이썬 머신러닝 기초' 카테고리의 다른 글
비지도학습-군집 (0) | 2019.04.08 |
---|---|
비지도학습-스케일 (0) | 2019.04.08 |
지도학습-릿지와 라쏘 (0) | 2019.04.02 |
지도학습-경사하강법 (0) | 2019.04.01 |
지도학습-나이브베이즈 (0) | 2019.03.30 |