mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Remove code with side effects from main (#1577)
* Remove code with side effects from main When running tests withy pytest, some modules execute code in main scope and open plot or browser windows. Moves such code under `if __name__ == "__main__"`. * fixup! Format Python code with psf/black push
This commit is contained in:
parent
5616fa9e62
commit
12f69a86f5
@ -6,97 +6,97 @@ Requirements:
|
||||
Python:
|
||||
- 3.5
|
||||
"""
|
||||
# Create universe of discourse in python using linspace ()
|
||||
import numpy as np
|
||||
|
||||
X = np.linspace(start=0, stop=75, num=75, endpoint=True, retstep=False)
|
||||
|
||||
# Create two fuzzy sets by defining any membership function (trapmf(), gbellmf(),gaussmf(), etc).
|
||||
import skfuzzy as fuzz
|
||||
|
||||
abc1 = [0, 25, 50]
|
||||
abc2 = [25, 50, 75]
|
||||
young = fuzz.membership.trimf(X, abc1)
|
||||
middle_aged = fuzz.membership.trimf(X, abc2)
|
||||
|
||||
# Compute the different operations using inbuilt functions.
|
||||
one = np.ones(75)
|
||||
zero = np.zeros((75,))
|
||||
# 1. Union = max(µA(x), µB(x))
|
||||
union = fuzz.fuzzy_or(X, young, X, middle_aged)[1]
|
||||
# 2. Intersection = min(µA(x), µB(x))
|
||||
intersection = fuzz.fuzzy_and(X, young, X, middle_aged)[1]
|
||||
# 3. Complement (A) = (1- min(µA(x))
|
||||
complement_a = fuzz.fuzzy_not(young)
|
||||
# 4. Difference (A/B) = min(µA(x),(1- µB(x)))
|
||||
difference = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1]
|
||||
# 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))]
|
||||
alg_sum = young + middle_aged - (young * middle_aged)
|
||||
# 6. Algebraic Product = (µA(x) * µB(x))
|
||||
alg_product = young * middle_aged
|
||||
# 7. Bounded Sum = min[1,(µA(x), µB(x))]
|
||||
bdd_sum = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1]
|
||||
# 8. Bounded difference = min[0,(µA(x), µB(x))]
|
||||
bdd_difference = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1]
|
||||
if __name__ == "__main__":
|
||||
# Create universe of discourse in python using linspace ()
|
||||
X = np.linspace(start=0, stop=75, num=75, endpoint=True, retstep=False)
|
||||
|
||||
# max-min composition
|
||||
# max-product composition
|
||||
# Create two fuzzy sets by defining any membership function (trapmf(), gbellmf(),gaussmf(), etc).
|
||||
abc1 = [0, 25, 50]
|
||||
abc2 = [25, 50, 75]
|
||||
young = fuzz.membership.trimf(X, abc1)
|
||||
middle_aged = fuzz.membership.trimf(X, abc2)
|
||||
|
||||
# Compute the different operations using inbuilt functions.
|
||||
one = np.ones(75)
|
||||
zero = np.zeros((75,))
|
||||
# 1. Union = max(µA(x), µB(x))
|
||||
union = fuzz.fuzzy_or(X, young, X, middle_aged)[1]
|
||||
# 2. Intersection = min(µA(x), µB(x))
|
||||
intersection = fuzz.fuzzy_and(X, young, X, middle_aged)[1]
|
||||
# 3. Complement (A) = (1- min(µA(x))
|
||||
complement_a = fuzz.fuzzy_not(young)
|
||||
# 4. Difference (A/B) = min(µA(x),(1- µB(x)))
|
||||
difference = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1]
|
||||
# 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))]
|
||||
alg_sum = young + middle_aged - (young * middle_aged)
|
||||
# 6. Algebraic Product = (µA(x) * µB(x))
|
||||
alg_product = young * middle_aged
|
||||
# 7. Bounded Sum = min[1,(µA(x), µB(x))]
|
||||
bdd_sum = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1]
|
||||
# 8. Bounded difference = min[0,(µA(x), µB(x))]
|
||||
bdd_difference = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1]
|
||||
|
||||
# Plot each set A, set B and each operation result using plot() and subplot().
|
||||
import matplotlib.pyplot as plt
|
||||
# max-min composition
|
||||
# max-product composition
|
||||
|
||||
plt.figure()
|
||||
# Plot each set A, set B and each operation result using plot() and subplot().
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
plt.subplot(4, 3, 1)
|
||||
plt.plot(X, young)
|
||||
plt.title("Young")
|
||||
plt.grid(True)
|
||||
plt.figure()
|
||||
|
||||
plt.subplot(4, 3, 2)
|
||||
plt.plot(X, middle_aged)
|
||||
plt.title("Middle aged")
|
||||
plt.grid(True)
|
||||
plt.subplot(4, 3, 1)
|
||||
plt.plot(X, young)
|
||||
plt.title("Young")
|
||||
plt.grid(True)
|
||||
|
||||
plt.subplot(4, 3, 3)
|
||||
plt.plot(X, union)
|
||||
plt.title("union")
|
||||
plt.grid(True)
|
||||
plt.subplot(4, 3, 2)
|
||||
plt.plot(X, middle_aged)
|
||||
plt.title("Middle aged")
|
||||
plt.grid(True)
|
||||
|
||||
plt.subplot(4, 3, 4)
|
||||
plt.plot(X, intersection)
|
||||
plt.title("intersection")
|
||||
plt.grid(True)
|
||||
plt.subplot(4, 3, 3)
|
||||
plt.plot(X, union)
|
||||
plt.title("union")
|
||||
plt.grid(True)
|
||||
|
||||
plt.subplot(4, 3, 5)
|
||||
plt.plot(X, complement_a)
|
||||
plt.title("complement_a")
|
||||
plt.grid(True)
|
||||
plt.subplot(4, 3, 4)
|
||||
plt.plot(X, intersection)
|
||||
plt.title("intersection")
|
||||
plt.grid(True)
|
||||
|
||||
plt.subplot(4, 3, 6)
|
||||
plt.plot(X, difference)
|
||||
plt.title("difference a/b")
|
||||
plt.grid(True)
|
||||
plt.subplot(4, 3, 5)
|
||||
plt.plot(X, complement_a)
|
||||
plt.title("complement_a")
|
||||
plt.grid(True)
|
||||
|
||||
plt.subplot(4, 3, 7)
|
||||
plt.plot(X, alg_sum)
|
||||
plt.title("alg_sum")
|
||||
plt.grid(True)
|
||||
plt.subplot(4, 3, 6)
|
||||
plt.plot(X, difference)
|
||||
plt.title("difference a/b")
|
||||
plt.grid(True)
|
||||
|
||||
plt.subplot(4, 3, 8)
|
||||
plt.plot(X, alg_product)
|
||||
plt.title("alg_product")
|
||||
plt.grid(True)
|
||||
plt.subplot(4, 3, 7)
|
||||
plt.plot(X, alg_sum)
|
||||
plt.title("alg_sum")
|
||||
plt.grid(True)
|
||||
|
||||
plt.subplot(4, 3, 9)
|
||||
plt.plot(X, bdd_sum)
|
||||
plt.title("bdd_sum")
|
||||
plt.grid(True)
|
||||
plt.subplot(4, 3, 8)
|
||||
plt.plot(X, alg_product)
|
||||
plt.title("alg_product")
|
||||
plt.grid(True)
|
||||
|
||||
plt.subplot(4, 3, 10)
|
||||
plt.plot(X, bdd_difference)
|
||||
plt.title("bdd_difference")
|
||||
plt.grid(True)
|
||||
plt.subplot(4, 3, 9)
|
||||
plt.plot(X, bdd_sum)
|
||||
plt.title("bdd_sum")
|
||||
plt.grid(True)
|
||||
|
||||
plt.subplots_adjust(hspace=0.5)
|
||||
plt.show()
|
||||
plt.subplot(4, 3, 10)
|
||||
plt.plot(X, bdd_difference)
|
||||
plt.title("bdd_difference")
|
||||
plt.grid(True)
|
||||
|
||||
plt.subplots_adjust(hspace=0.5)
|
||||
plt.show()
|
||||
|
@ -36,8 +36,9 @@ def viz_polymonial():
|
||||
return
|
||||
|
||||
|
||||
viz_polymonial()
|
||||
if __name__ == "__main__":
|
||||
viz_polymonial()
|
||||
|
||||
# Predicting a new result with Polymonial Regression
|
||||
pol_reg.predict(poly_reg.fit_transform([[5.5]]))
|
||||
# output should be 132148.43750003
|
||||
# Predicting a new result with Polymonial Regression
|
||||
pol_reg.predict(poly_reg.fit_transform([[5.5]]))
|
||||
# output should be 132148.43750003
|
||||
|
@ -59,125 +59,133 @@ def plot(samples):
|
||||
return fig
|
||||
|
||||
|
||||
# 1. Load Data and declare hyper
|
||||
print("--------- Load Data ----------")
|
||||
mnist = input_data.read_data_sets("MNIST_data", one_hot=False)
|
||||
temp = mnist.test
|
||||
images, labels = temp.images, temp.labels
|
||||
images, labels = shuffle(np.asarray(images), np.asarray(labels))
|
||||
num_epoch = 10
|
||||
learing_rate = 0.00009
|
||||
G_input = 100
|
||||
hidden_input, hidden_input2, hidden_input3 = 128, 256, 346
|
||||
hidden_input4, hidden_input5, hidden_input6 = 480, 560, 686
|
||||
if __name__ == "__main__":
|
||||
# 1. Load Data and declare hyper
|
||||
print("--------- Load Data ----------")
|
||||
mnist = input_data.read_data_sets("MNIST_data", one_hot=False)
|
||||
temp = mnist.test
|
||||
images, labels = temp.images, temp.labels
|
||||
images, labels = shuffle(np.asarray(images), np.asarray(labels))
|
||||
num_epoch = 10
|
||||
learing_rate = 0.00009
|
||||
G_input = 100
|
||||
hidden_input, hidden_input2, hidden_input3 = 128, 256, 346
|
||||
hidden_input4, hidden_input5, hidden_input6 = 480, 560, 686
|
||||
|
||||
|
||||
print("--------- Declare Hyper Parameters ----------")
|
||||
# 2. Declare Weights
|
||||
D_W1 = (
|
||||
np.random.normal(size=(784, hidden_input), scale=(1.0 / np.sqrt(784 / 2.0))) * 0.002
|
||||
)
|
||||
# D_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
D_b1 = np.zeros(hidden_input)
|
||||
|
||||
D_W2 = (
|
||||
np.random.normal(size=(hidden_input, 1), scale=(1.0 / np.sqrt(hidden_input / 2.0)))
|
||||
print("--------- Declare Hyper Parameters ----------")
|
||||
# 2. Declare Weights
|
||||
D_W1 = (
|
||||
np.random.normal(size=(784, hidden_input), scale=(1.0 / np.sqrt(784 / 2.0)))
|
||||
* 0.002
|
||||
)
|
||||
# D_b2 = np.random.normal(size=(1),scale=(1. / np.sqrt(1 / 2.))) *0.002
|
||||
D_b2 = np.zeros(1)
|
||||
)
|
||||
# D_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
D_b1 = np.zeros(hidden_input)
|
||||
|
||||
|
||||
G_W1 = (
|
||||
np.random.normal(size=(G_input, hidden_input), scale=(1.0 / np.sqrt(G_input / 2.0)))
|
||||
* 0.002
|
||||
)
|
||||
# G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
G_b1 = np.zeros(hidden_input)
|
||||
|
||||
G_W2 = (
|
||||
D_W2 = (
|
||||
np.random.normal(
|
||||
size=(hidden_input, hidden_input2), scale=(1.0 / np.sqrt(hidden_input / 2.0))
|
||||
size=(hidden_input, 1), scale=(1.0 / np.sqrt(hidden_input / 2.0))
|
||||
)
|
||||
* 0.002
|
||||
)
|
||||
# G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
G_b2 = np.zeros(hidden_input2)
|
||||
)
|
||||
# D_b2 = np.random.normal(size=(1),scale=(1. / np.sqrt(1 / 2.))) *0.002
|
||||
D_b2 = np.zeros(1)
|
||||
|
||||
G_W3 = (
|
||||
G_W1 = (
|
||||
np.random.normal(
|
||||
size=(hidden_input2, hidden_input3), scale=(1.0 / np.sqrt(hidden_input2 / 2.0))
|
||||
size=(G_input, hidden_input), scale=(1.0 / np.sqrt(G_input / 2.0))
|
||||
)
|
||||
* 0.002
|
||||
)
|
||||
# G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
G_b3 = np.zeros(hidden_input3)
|
||||
)
|
||||
# G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
G_b1 = np.zeros(hidden_input)
|
||||
|
||||
G_W4 = (
|
||||
G_W2 = (
|
||||
np.random.normal(
|
||||
size=(hidden_input3, hidden_input4), scale=(1.0 / np.sqrt(hidden_input3 / 2.0))
|
||||
size=(hidden_input, hidden_input2),
|
||||
scale=(1.0 / np.sqrt(hidden_input / 2.0)),
|
||||
)
|
||||
* 0.002
|
||||
)
|
||||
# G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
G_b4 = np.zeros(hidden_input4)
|
||||
)
|
||||
# G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
G_b2 = np.zeros(hidden_input2)
|
||||
|
||||
G_W5 = (
|
||||
G_W3 = (
|
||||
np.random.normal(
|
||||
size=(hidden_input4, hidden_input5), scale=(1.0 / np.sqrt(hidden_input4 / 2.0))
|
||||
size=(hidden_input2, hidden_input3),
|
||||
scale=(1.0 / np.sqrt(hidden_input2 / 2.0)),
|
||||
)
|
||||
* 0.002
|
||||
)
|
||||
# G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
G_b5 = np.zeros(hidden_input5)
|
||||
)
|
||||
# G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
G_b3 = np.zeros(hidden_input3)
|
||||
|
||||
G_W6 = (
|
||||
G_W4 = (
|
||||
np.random.normal(
|
||||
size=(hidden_input5, hidden_input6), scale=(1.0 / np.sqrt(hidden_input5 / 2.0))
|
||||
size=(hidden_input3, hidden_input4),
|
||||
scale=(1.0 / np.sqrt(hidden_input3 / 2.0)),
|
||||
)
|
||||
* 0.002
|
||||
)
|
||||
# G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
G_b6 = np.zeros(hidden_input6)
|
||||
)
|
||||
# G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
G_b4 = np.zeros(hidden_input4)
|
||||
|
||||
G_W7 = (
|
||||
G_W5 = (
|
||||
np.random.normal(
|
||||
size=(hidden_input4, hidden_input5),
|
||||
scale=(1.0 / np.sqrt(hidden_input4 / 2.0)),
|
||||
)
|
||||
* 0.002
|
||||
)
|
||||
# G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
G_b5 = np.zeros(hidden_input5)
|
||||
|
||||
G_W6 = (
|
||||
np.random.normal(
|
||||
size=(hidden_input5, hidden_input6),
|
||||
scale=(1.0 / np.sqrt(hidden_input5 / 2.0)),
|
||||
)
|
||||
* 0.002
|
||||
)
|
||||
# G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002
|
||||
G_b6 = np.zeros(hidden_input6)
|
||||
|
||||
G_W7 = (
|
||||
np.random.normal(
|
||||
size=(hidden_input6, 784), scale=(1.0 / np.sqrt(hidden_input6 / 2.0))
|
||||
)
|
||||
* 0.002
|
||||
)
|
||||
# G_b2 = np.random.normal(size=(784),scale=(1. / np.sqrt(784 / 2.))) *0.002
|
||||
G_b7 = np.zeros(784)
|
||||
)
|
||||
# G_b2 = np.random.normal(size=(784),scale=(1. / np.sqrt(784 / 2.))) *0.002
|
||||
G_b7 = np.zeros(784)
|
||||
|
||||
# 3. For Adam Optimzier
|
||||
v1, m1 = 0, 0
|
||||
v2, m2 = 0, 0
|
||||
v3, m3 = 0, 0
|
||||
v4, m4 = 0, 0
|
||||
# 3. For Adam Optimzier
|
||||
v1, m1 = 0, 0
|
||||
v2, m2 = 0, 0
|
||||
v3, m3 = 0, 0
|
||||
v4, m4 = 0, 0
|
||||
|
||||
v5, m5 = 0, 0
|
||||
v6, m6 = 0, 0
|
||||
v7, m7 = 0, 0
|
||||
v8, m8 = 0, 0
|
||||
v9, m9 = 0, 0
|
||||
v10, m10 = 0, 0
|
||||
v11, m11 = 0, 0
|
||||
v12, m12 = 0, 0
|
||||
v5, m5 = 0, 0
|
||||
v6, m6 = 0, 0
|
||||
v7, m7 = 0, 0
|
||||
v8, m8 = 0, 0
|
||||
v9, m9 = 0, 0
|
||||
v10, m10 = 0, 0
|
||||
v11, m11 = 0, 0
|
||||
v12, m12 = 0, 0
|
||||
|
||||
v13, m13 = 0, 0
|
||||
v14, m14 = 0, 0
|
||||
v13, m13 = 0, 0
|
||||
v14, m14 = 0, 0
|
||||
|
||||
v15, m15 = 0, 0
|
||||
v16, m16 = 0, 0
|
||||
v15, m15 = 0, 0
|
||||
v16, m16 = 0, 0
|
||||
|
||||
v17, m17 = 0, 0
|
||||
v18, m18 = 0, 0
|
||||
v17, m17 = 0, 0
|
||||
v18, m18 = 0, 0
|
||||
|
||||
beta_1, beta_2, eps = 0.9, 0.999, 0.00000001
|
||||
|
||||
beta_1, beta_2, eps = 0.9, 0.999, 0.00000001
|
||||
|
||||
print("--------- Started Training ----------")
|
||||
for iter in range(num_epoch):
|
||||
print("--------- Started Training ----------")
|
||||
for iter in range(num_epoch):
|
||||
|
||||
random_int = np.random.randint(len(images) - 5)
|
||||
current_image = np.expand_dims(images[random_int], axis=0)
|
||||
@ -494,5 +502,5 @@ for iter in range(num_epoch):
|
||||
),
|
||||
bbox_inches="tight",
|
||||
)
|
||||
# for complete explanation visit https://towardsdatascience.com/only-numpy-implementing-gan-general-adversarial-networks-and-adam-optimizer-using-numpy-with-2a7e4e032021
|
||||
# -- end code --
|
||||
# for complete explanation visit https://towardsdatascience.com/only-numpy-implementing-gan-general-adversarial-networks-and-adam-optimizer-using-numpy-with-2a7e4e032021
|
||||
# -- end code --
|
||||
|
@ -5,16 +5,18 @@ from bs4 import BeautifulSoup
|
||||
from fake_useragent import UserAgent
|
||||
import requests
|
||||
|
||||
print("Googling.....")
|
||||
url = "https://www.google.com/search?q=" + " ".join(sys.argv[1:])
|
||||
res = requests.get(url, headers={"UserAgent": UserAgent().random})
|
||||
# res.raise_for_status()
|
||||
with open("project1a.html", "wb") as out_file: # only for knowing the class
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Googling.....")
|
||||
url = "https://www.google.com/search?q=" + " ".join(sys.argv[1:])
|
||||
res = requests.get(url, headers={"UserAgent": UserAgent().random})
|
||||
# res.raise_for_status()
|
||||
with open("project1a.html", "wb") as out_file: # only for knowing the class
|
||||
for data in res.iter_content(10000):
|
||||
out_file.write(data)
|
||||
soup = BeautifulSoup(res.text, "html.parser")
|
||||
links = list(soup.select(".eZt8xd"))[:5]
|
||||
soup = BeautifulSoup(res.text, "html.parser")
|
||||
links = list(soup.select(".eZt8xd"))[:5]
|
||||
|
||||
print(len(links))
|
||||
for link in links:
|
||||
print(len(links))
|
||||
for link in links:
|
||||
webbrowser.open(f"http://google.com{link.get('href')}")
|
||||
|
Loading…
Reference in New Issue
Block a user