<jupyter_start><jupyter_text>1. 단순 데이터 (make_wav)를 사용한 단순선형회귀 예 (강의자료 6,7쪽)<jupyter_code>#단순 데이터 (make_wav)를 사용한 단순선형회귀 예 (강의자료 6,7쪽)
import mglearn
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt

X, y = mglearn.datasets.make_wave(n_samples=60)#인위적인 데이터 집합
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)#데이터셋을 훈련용과 테스트용으로 나누기

lr = LinearRegression().fit(X_train, y_train)

w = lr.coef_#가중치
b = lr.intercept_#편향치
print("w (lr.coef_) : ", w)
print("b (lr.intercept) : ", b)

print("훈련 세트 점수 : {:.2f}".format(lr.score(X_train, y_train)))
print("데스트 세트 점수 : {:.2f}".format(lr.score(X_test, y_test)))
line = np.linspace(-3, 3, 100).reshape(-1, 1)
plt.figure(figsize=(8, 8))
plt.plot(line, lr.predict(line))#선형회귀로 추정된 선형함수(모델)로 출력값 생성
plt.plot(X, y, 'o', c='blue')#학습 데이터
plt.ylim(-3, 3)
plt.xlabel('X')
plt.ylabel('Y')
plt.title("Wave Data Plot")
plt.legend(["model", "train data"], loc="best")
plt.grid()
plt.show()<jupyter_output><empty_output><jupyter_text>2. 보스톤 주택가격(1978) 데이터 집합을 사용한 다중선형회귀 예 (강의자료 8쪽)<jupyter_code>import sklearn.datasets as ds

boston = ds.load_boston()

print("데이터의 형태 : ", boston.data.shape)
print("특성 이름 : \n", boston.feature_names)
print("데이터 설명 : \n", boston.DESCR)

X = boston.data
y = boston.target

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
lr = LinearRegression().fit(X_train, y_train)

print("훈련 세트 점수 : {:.2f}".format(lr.score(X_train, y_train)))
print("테스트 세트 점수 : {:.2f}".format(lr.score(X_test, y_test)))<jupyter_output>데이터의 형태 :  (506, 13)
특성 이름 : 
 ['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO'
 'B' 'LSTAT']
데이터 설명 : 
 .. _boston_dataset:

Boston house prices dataset
---------------------------

**Data Set Characteristics:**  

    :Number of Instances: 506 

    :Number of Attributes: 13 numeric/categorical predictive. Median Value (attribute 14) is usually the target.

    :Attribute Information (in order):
        - CRIM     per capita crime rate by town
        - ZN       proportion of residential land zoned for lots over 25,000 sq.ft.
        - INDUS    proportion of non-retail business acres per town
        - CHAS     Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
        - NOX      nitric oxides concentration (parts per 10 million)
        - RM       average number of rooms per dwelling
        - AGE      proportion of owner-occupied units built prior to 1940
        - DIS      weighted distances to five Boston employment centres
        - RAD[...]<jupyter_text>3.  데이터 집합에서 주택가격과 개별특징의 상관도 확인 (강의자료 10쪽)<jupyter_code>import pandas as pd
df = pd.DataFrame(boston.data, columns=boston.feature_names)#데이터 프레임 작성
df['MEDV'] = boston.target#주택가격

df.hist(bins=10, figsize=(9, 7), grid=False)

corr = df.corr(method='pearson')#개별 특증들간의 상관도
display(corr)
corrMEDV = corr['MEDV']#주택가격의 상관도
display(corrMEDV)
sub = corrMEDV[corrMEDV.abs() > 0.45]
display(sub)<jupyter_output><empty_output><jupyter_text>4. 보스톤 주택가격(1978) 데이터 집합에서 주택가격과 높은 상관도를 가지는 특징들로 다중선형회귀 결과 확인 (강의자료 12쪽)<jupyter_code>d=df.loc[:,['INDUS', 'RM', 'TAX', 'PTRATIO', 'LSTAT']]
X = d
y = df['MEDV']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
lrt = LinearRegression().fit(X_train, y_train)

w = lrt.coef_#가중치
b = lrt.intercept_#편향치
print("w (lr.coef_) : ", w)
print("b (lr.intercept) : ", b)

print("훈련 세트 점수 : {:.2f}".format(lrt.score(X_train, y_train)))
print("테스트 세트 점수 : {:.2f}".format(lrt.score(X_test, y_test)))<jupyter_output>w (lr.coef_) :  [ 0.04069519  4.47903682 -0.00562846 -0.99250298 -0.54266618]
b (lr.intercept) :  21.312002509449677
훈련 세트 점수 : 0.72
테스트 세트 점수 : 0.54<jupyter_text>5.  Ridge Regression 예(강의자료 15-16쪽)<jupyter_code>from sklearn.linear_model import Ridge

X = boston.data
y = boston.target
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

ridge = Ridge().fit(X_train, y_train)
w = ridge.coef_
b = ridge.intercept_
print("---------------------------")
print("Ridge(alpha=1)")
print("w (ridge.coef_) : ",w)
print("b (ridge.intercept_) : )", b)

print("훈련 세트 점수 : {:.2f}".format(ridge.score(X_train, y_train)))
print("테스트 세트 점수 : {:.2f}".format(ridge.score(X_test, y_test)))

ridge10 = Ridge(alpha=10).fit(X_train, y_train)
w = ridge10.coef_
b = ridge10.intercept_
print("---------------------------")
print("Ridge(alpha=10)")
print("w (ridge10.coef_) : ",w)
print("b (ridge10.intercept_) : )", b)

print("훈련 세트 점수 : {:.2f}".format(ridge10.score(X_train, y_train)))
print("테스트 세트 점수 : {:.2f}".format(ridge10.score(X_test, y_test)))

ridge01 = Ridge(alpha=0.1).fit(X_train, y_train)
w = ridge01.coef_
b = ridge01.intercept_
print("---------------------------")
print("Ridge(alpha=0.1)")
print("w (ridge10.coef_) : ",w)
print("b (ridge10.intercept_) : )", b)

print("훈련 세트 점수 : {:.2f}".format(ridge01.score(X_train, y_train)))
print("테스트 세트 점수 : {:.2f}".format(ridge01.score(X_test, y_test)))

#alpha 값에 따른 계수 비교
ind = boston.feature_names
dfe = pd.DataFrame({'Ridge alpha=10' : ridge10.coef_,
                   'Ridge alpha=1' : ridge.coef_,
                   'Ridge alpha=0.1' : ridge01.coef_,
                   'LinearRegression' : lr.coef_}, index=ind)
dfe.plot.bar(figsize=(10, 10))
plt.xlabel("features")
plt.ylabel("coef")
plt.ylim(-20, 10)
plt.legend(fontsize=12, loc='buttom right')<jupyter_output>c:\users\wlgh3\venv\tensorflow\lib\site-packages\matplotlib\legend.py:640: UserWarning: Unrecognized location "buttom right". Falling back on "best"; valid locations are
	best
	upper right
	upper left
	lower left
	lower right
	right
	center left
	center right
	lower center
	upper center
	center

  % (loc, '\n\t'.join(self.codes)))
{'fraction_non_alphanumeric': 0.17569444444444443, 'fraction_numerical': 0.0375, 'mean_word_length': 4.035839160839161, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '657935', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 2768, 'n_tokens_neox': 2948, 'n_words': 532, 'path': 'LinearRegression.ipynb', 'repo_name': 'wlgh312/DataMining', 'size': '5760'}