<jupyter_start><jupyter_text><jupyter_code>!sudo apt-get install -y fonts-nanum
!sudo fc-cache -fv
!rm ~/.cache/matplotlib -rf
import numpy as np
import matplotlib.pyplot as plt

X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.rand(100,1)
plt.scatter(X,y)
X_b = np.c_[np.ones((100,1)), X]
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)
theta_best
X_new = np.array([[0],[2]])
X_new_b = np.c_[np.ones((2,1)), X_new]
y_predict = X_new_b.dot(theta_best)
y_predict
plt.plot(X_new, y_predict, "r--")
plt.plot(X, y, "b.")
plt.axis([0, 2, 0, 15])
plt.show()
from sklearn.linear_model import LinearRegression

lin_reg = LinearRegression()
lin_reg.fit(X, y)
lin_reg.intercept_, lin_reg.coef_
theta_best_svd, residuals, rank, s = np.linalg.lstsq(X_b, y, rcond = 1e-6)
theta_best_svd
np.linalg.pinv(X_b).dot(y)
eta = 0.1 
n_iterations = 1000
m = 100
theta = np.random.randn(2,1)

for iteration in range(n_iterations):
  gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y)
  theta = theta - eta * gradients

theta
n_epochs = 50
t0, t1 = 5, 50 # 학습 스케줄 파라미터

def learning_schedule(t):
  return t0 / (t + t1)

theta = np.random.randn(2,1) # 무작위 초기화

for epoch in range(n_epochs):
  for i in range(m):
    random_index = np.random.randint(m)
    xi = X_b[random_index:random_index+1]
    yi = y[random_index:random_index+1]
    gradients = 2 * xi.T.dot(xi.dot(theta) - yi)
    eta = learning_schedule(epoch * m + i)
    theta = theta - eta * gradients
theta
from sklearn.linear_model import SGDRegressor
sgd_reg = SGDRegressor(max_iter = 1000, tol = 1e-3, penalty = None, eta0 = 0.1)
sgd_reg.fit(X, y.ravel())
sgd_reg.intercept_, sgd_reg.coef_<jupyter_output><empty_output><jupyter_text>4.3 다항 회귀<jupyter_code>m = 100
X = 6 * np.random.rand(m, 1) - 3
y = 0.5 * X**2 + X + 2 + np.random.randn(m,1)
plt.scatter(X,y)
from sklearn.preprocessing import PolynomialFeatures
poly_features = PolynomialFeatures(degree = 2, include_bias = False)
X_poly = poly_features.fit_transform(X)
display(X[0])
display(X_poly[0])
lin_reg = LinearRegression()
lin_reg.fit(X_poly, y)
lin_reg.intercept_, lin_reg.coef_<jupyter_output><empty_output><jupyter_text>4.4 학습  곡선<jupyter_code>from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split

def plot_learning_curves(model, X, y):
  X_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.2)
  train_errors, val_errors = [], []
  for m in range(1, len(X_train)):
    model.fit(X_train[:m], y_train[:m])
    y_train_predict = model.predict(X_train[:m])
    y_val_predict = model.predict(X_val)
    train_errors.append(mean_squared_error(y_train[:m], y_train_predict))
    val_errors.append(mean_squared_error(y_val, y_val_predict))
  plt.rc('font', family='NanumBarunGothic') 
  plt.plot(np.sqrt(train_errors), 'r-+', linewidth = 2, label = '훈련 세트')
  plt.plot(np.sqrt(val_errors), 'b-', linewidth = 3, label = '검증 세트')
  plt.legend()
  plt.xlabel('훈련 세트 크기')
  plt.ylabel('RMSE')
  plt.show()
lin_reg = LinearRegression()
plot_learning_curves(lin_reg, X, y)
from sklearn.pipeline import Pipeline

polynomial_regression = Pipeline([
                                  ("poly_features", PolynomialFeatures(degree = 10, include_bias= False)),
                                  ("lin_reg", LinearRegression())
])
plot_learning_curves(polynomial_regression, X, y)<jupyter_output><empty_output><jupyter_text>4.5 규제가 있는 선형 모델  4.5.1 릿지 회귀<jupyter_code>from sklearn.linear_model import Ridge
ridge_reg = Ridge(alpha = 1, solver = 'cholesky')
ridge_reg.fit(X, y)
ridge_reg.predict([[1.5]])
sgd_reg = SGDRegressor(penalty = 'l2')
sgd_reg.fit(X, y.ravel())
sgd_reg.predict([[1.5]])<jupyter_output><empty_output><jupyter_text>4.5.2 라쏘 회귀<jupyter_code>from sklearn.linear_model import Lasso
lasso_reg = Lasso(alpha = 0.1)
lasso_reg.fit(X, y)
lasso_reg.predict([[1.5]])<jupyter_output><empty_output><jupyter_text>4.5.3 엘라스틱넷<jupyter_code>from sklearn.linear_model import ElasticNet
elastic_net = ElasticNet(alpha = 0.1, l1_ratio = 0.5)
elastic_net.fit(X, y)
elastic_net.predict([[1.5]])<jupyter_output><empty_output><jupyter_text>4.5.4 조기 종료<jupyter_code>from copy import deepcopy
from sklearn.preprocessing import StandardScaler

X_train, X_val, y_train, y_val = train_test_split(X, y.ravel(), test_size = 0.2)

poly_scaler = Pipeline([
                        ('poly_features', PolynomialFeatures(degree = 90, include_bias= False)),
                        ('std_scaler', StandardScaler())
])
X_train_poly_scaled = poly_scaler.fit_transform(X_train)
X_val_poly_scaled = poly_scaler.transform(X_val)

sgd_reg = SGDRegressor(max_iter = 1, tol = -np.infty, warm_start=True,
                       penalty = None, learning_rate = 'constant', eta0 = 0.0005)

minimum_val_error = float("inf")
best_epoch = None
best_model = None
for epoch in range(1000):
  sgd_reg.fit(X_train_poly_scaled, y_train)
  y_val_predict = sgd_reg.predict(X_val_poly_scaled)
  val_error = mean_squared_error(y_val, y_val_predict)
  if val_error < minimum_val_error:
    minimum_val_error = val_error
    best_epoch = epoch
    best_model = deepcopy(sgd_reg)
best_epoch
best_model<jupyter_output><empty_output><jupyter_text>4.6 로지스틱 회귀  4.6.1 확률 추정  4.6.2 훈련과 비용 함수  4.6.3 결정 경계<jupyter_code>from sklearn import datasets
iris = datasets.load_iris()
list(iris.keys())
X = iris['data'][:, 3:]
y = (iris['target'] == 2).astype(np.int)
from sklearn.linear_model import LogisticRegression

log_reg = LogisticRegression()
log_reg.fit(X, y)
X_new = np.linspace(0, 3, 1000).reshape(-1, 1)
y_proba = log_reg.predict_proba(X_new)
plt.plot(X_new, y_proba[:,1], 'g-', label = 'iris_virginica')
plt.plot(X_new, y_proba[:,0], 'b--', label = 'Not iris_virgincica')
plt.legend()
plt.xlabel('꽃잎 너비(cm)')
plt.ylabel('확률')
log_reg.predict([[1.7],[1.5]])<jupyter_output><empty_output><jupyter_text>4.6.4 소프트맥스 회귀<jupyter_code>X = iris['data'][:, (2,3)]
y = iris['target']

sofrtmax_reg = LogisticRegression(multi_class='multinomial', solver='lbfgs', C = 10)
sofrtmax_reg.fit(X, y)
sofrtmax_reg.predict([[5,2]])
sofrtmax_reg.predict_proba([[5,2]])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14735289296836585, 'fraction_numerical': 0.027864284543517456, 'mean_word_length': 5.035608308605341, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '555720', 'license': 'MIT', 'n_tokens_mistral': 2784, 'n_tokens_neox': 2737, 'n_words': 460, 'path': '_ipynb/Chapter4.ipynb', 'repo_name': 'Yeongjin-Jo/minimal-mistakes', 'size': '6101'}