<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          NapkinMLNumPy 機(jī)器學(xué)習(xí)模型的袖珍實(shí)現(xiàn)

          聯(lián)合創(chuàng)作 · 2023-09-26 04:32

          NapkinML 是 NumPy 中機(jī)器學(xué)習(xí)模型的袖珍實(shí)現(xiàn)。

          安裝

          $ git clone https://github.com/eriklindernoren/NapkinML
          $ cd NapkinML
          $ sudo python setup.py install
          

          實(shí)現(xiàn)

          K-Nearest 最近鄰

          class KNN():
              def predict(self, k, Xt, X, y):
                  y_pred = np.empty(len(Xt))
                  for i, xt in enumerate(Xt):
                      idx = np.argsort([np.linalg.norm(x-xt) for x in X])[:k]
                      y_pred[i] = np.bincount([y[i] for i in idx]).argmax()
                  return y_pred
          $ python napkin_ml/examples/knn.py

           

          圖:使用K-Nearest最近鄰的Iris數(shù)據(jù)集的分類。

          線性回歸

          class LinearRegression():
              def fit(self, X, y):
                  self.w = np.linalg.lstsq(X, y, rcond=None)[0]
              def predict(self, X):
                  return X.dot(self.w)
          $ python napkin_ml/examples/linear_regression.py

           

          圖:線性回歸

          線性判別分析

          class LDA():
              def fit(self, X, y):
                  cov_sum = sum([np.cov(X[y == val], rowvar=False) for val in [0, 1]])
                  mean_diff = X[y == 0].mean(0) - X[y == 1].mean(0)
                  self.w = np.linalg.inv(cov_sum).dot(mean_diff)
              def predict(self, X):
                  return 1 * (X.dot(self.w) < 0)
          $ python napkin_ml/examples/lda.py

           

          Logistic 回歸

          class LogisticRegression():
              def fit(self, X, y, n_iter=4000, lr=0.01):
                  self.w = np.random.rand(X.shape[1])
                  for _ in range(n_iter):
                      self.w -= lr * (self.predict(X) - y).dot(X)
              def predict(self, X):
                  return sigmoid(X.dot(self.w))
          $ python napkin_ml/examples/logistic_regression.py

           

          圖:Logistic回歸分類

          多層感知器

          class MLP():
              def fit(self, X, y, n_epochs=4000, lr=0.01, n_units=10):
                  self.w = np.random.rand(X.shape[1], n_units)
                  self.v = np.random.rand(n_units, y.shape[1])
                  for _ in range(n_epochs):
                      h_out = sigmoid(X.dot(self.w))
                      out = softmax(h_out.dot(self.v))
                      self.v -= lr * h_out.T.dot(out - y)
                      self.w -= lr * X.T.dot((out - y).dot(self.v.T) * (h_out * (1 - h_out)))
              def predict(self, X):
                  return softmax(sigmoid(X.dot(self.w)).dot(self.v))
          $ python napkin_ml/examples/mlp.py

           

          圖:帶有一個(gè)隱藏層的多層感知器對(duì)虹膜數(shù)據(jù)集的分類。

          主成分分析

          class PCA():
              def transform(self, X, dim):
                  _, S, V = np.linalg.svd(X - X.mean(0), full_matrices=True)
                  idx = S.argsort()[::-1]
                  V = V[idx][:dim]
                  return X.dot(V.T)
          $ python napkin_ml/examples/pca.py

           

          圖:主成分分析降維。

          瀏覽 27
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  色婷婷中文在线观看视频 | 最近中文字幕免费mv第一季歌词大全 | 豆花AV一区二区无码免费看 | 大香蕉夜夜 | 色丁香婷婷五月天 |