反比例编写Javascript

时间:2018-09-16 21:25:07

标签: javascript math

花一些时间尝试编写一些JavaScript来使这种反比例起作用。基本上,一个元素将具有基于其大小的点值。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv("data.csv")
data = data.drop(['id'], axis = 1)
data = data.drop(data.columns[31], axis = 1)
data = data.replace({'M': 1, 'B': 0})

X = data
X = X.drop(['diagnosis'], axis = 1)
X = np.array(X)

X_mean = np.mean(X, axis = 1, keepdims = True)
X_std = np.std(X, axis = 1, keepdims = True)
X_n = (X - X_mean) / X_std
y = np.array(data['diagnosis'])
y = y.reshape(569, 1)
m = 378
y_train = y[:m, :]
y_test = y[m:, :]

X_train = X_n[:m, :]
X_test = X_n[m:, :]

def sigmoid(z):
  return 1 / (1 + np.exp(-z))

def dsigmoid(z):
  return np.multiply(z, (1 - z))

def tanh(z):
  return (np.exp(z) - np.exp(-z)) / (np.exp(z) + np.exp(-z))

def dtanh(z):
  return 1 - np.square(tanh(z))

def cost(A, Y):
  m = Y.shape[0]
  return -(1.0/m) *np.sum( np.dot(Y.T, np.log(A)) + np.dot((1 - Y).T, np.log(1-A)))

def train(X, y ,model, epocs, a):
  W1 = model['W1']
  W2 = model['W2']
  W3 = model['W3']

  b1 = model['b1']
  b2 = model['b2']
  b3 = model['b3']

  costs = []

  for i in range(epocs):

    #forward propagation

    z1 = np.dot(X, W1) + b1
    a1 = tanh(z1)

    z2 = np.dot(a1, W2) + b2
    a2 = tanh(z2)

    z3 = np.dot(a2, W3) + b3
    a3 = sigmoid(z3)

    costs.append(cost(a3, y))

    #back propagation

    dz3 = z3 - y
    d3 = np.multiply(dz3, dsigmoid(z3))
    dW3 = np.dot(a2.T, d3)
    db3 = np.sum(d3, axis = 0, keepdims=True)

    d2 = np.multiply(np.dot(d3, W3.T),  dtanh(z2))
    dW2 = np.dot(a1.T, d2)
    db2 = np.sum(d2, axis = 0, keepdims=True)

    d1 = np.multiply(np.dot(d2, W2.T), dtanh(z1))
    dW1 = np.dot(X.T, d1)
    db1 = np.sum(d1, axis = 0, keepdims=True)

    W1 -= (a / m) * dW1
    W2 -= (a / m) * dW2
    W3 -= (a / m) * dW3

    b1 -= (a / m) * db1
    b2 -= (a / m) * db2
    b3 -= (a / m) * db3

  cache = {'W1': W1, 'W2': W2, 'W3': W3, 'b1': b1, 'b2': b2, 'b3': b3}
  return cache, costs

np.random.seed(0)

model = {'W1': np.random.rand(30, 6) * 0.01, 'W2': np.random.rand(6, 6) * 0.01, 'W3': np.random.rand(6, 1) * 0.01, 'b1': np.random.rand(1, 6), 'b2': np.random.rand(1, 6), 'b3': np.random.rand(1, 1)}

model, costss = train(X_train, y_train, model, 1000, 0.1)

plt.plot([i for i in range(1000)], costss)
print(costss[999])
plt.show()



def predict(X,y ,model):
  W1 = model['W1']
  W2 = model['W2']
  W3 = model['W3']

  b1 = model['b1']
  b2 = model['b2']
  b3 = model['b3']

  z1 = np.dot(X, W1) + b1
  a1 = tanh(z1)

  z2 = np.dot(a1, W2) + b2
  a2 = tanh(z2)

  z3 = np.dot(a2, W3) + b3
  a3 = sigmoid(z3)

  m = a3.shape[0]
  y_predict = np.zeros((m, 1))

  for i in range(m):
    y_predict = 1 if a3[i, 0] > 0.5 else 0
  return y_predict

任何建议都会很棒。谢谢

1 个答案:

答案 0 :(得分:1)

您可以采用数字11的差值和十分之一的数字。

function fn(n) {
    return 11 - n / 10;
}

var i;

for (i = 10; i <= 100; i += 10) {
    console.log(i, fn(i));
}
.as-console-wrapper { max-height: 100% !important; top: 0; }