Skip to content

Commit

Permalink
Reformat all files that are possible to reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
arnavb committed Feb 23, 2019
1 parent a98be98 commit 308ba81
Show file tree
Hide file tree
Showing 281 changed files with 16,707 additions and 5,105 deletions.
40 changes: 27 additions & 13 deletions code/artificial_intelligence/src/DBSCAN_Clustering/dbscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
UNCLASSIFIED = False
NOISE = None

def _dist(p,q):
return math.sqrt(np.power(p-q,2).sum())

def _eps_neighborhood(p,q,eps):
return _dist(p,q) < eps
def _dist(p, q):
return math.sqrt(np.power(p - q, 2).sum())


def _eps_neighborhood(p, q, eps):
return _dist(p, q) < eps


def _region_query(m, point_id, eps):
n_points = m.shape[1]
seeds = []
for i in range(0, n_points):
if _eps_neighborhood(m[:,point_id], m[:,i], eps):
if _eps_neighborhood(m[:, point_id], m[:, i], eps):
seeds.append(i)
return seeds


def _expand_cluster(m, classifications, point_id, cluster_id, eps, min_points):
seeds = _region_query(m, point_id, eps)
if len(seeds) < min_points:
Expand All @@ -36,14 +40,17 @@ def _expand_cluster(m, classifications, point_id, cluster_id, eps, min_points):
if len(results) >= min_points:
for i in range(0, len(results)):
result_point = results[i]
if classifications[result_point] == UNCLASSIFIED or \
classifications[result_point] == NOISE:
if (
classifications[result_point] == UNCLASSIFIED
or classifications[result_point] == NOISE
):
if classifications[result_point] == UNCLASSIFIED:
seeds.append(result_point)
classifications[result_point] = cluster_id
seeds = seeds[1:]
return True


def dbscan(m, eps, min_points):
"""Implementation of Density Based Spatial Clustering of Applications with Noise
See https://en.wikipedia.org/wiki/DBSCAN
Expand All @@ -65,22 +72,29 @@ def dbscan(m, eps, min_points):
n_points = m.shape[1]
classifications = [UNCLASSIFIED] * n_points
for point_id in range(0, n_points):
point = m[:,point_id]
point = m[:, point_id]
if classifications[point_id] == UNCLASSIFIED:
if _expand_cluster(m, classifications, point_id, cluster_id, eps, min_points):
if _expand_cluster(
m, classifications, point_id, cluster_id, eps, min_points
):
cluster_id = cluster_id + 1
return classifications


# def test_dbscan():
# m = np.matrix('1 1.2 0.8 3.7 3.9 3.6 10; 1.1 0.8 1 4 3.9 4.1 10')
# eps = 0.5
# min_points = 2
# assert dbscan(m, eps, min_points) == [1, 1, 1, 2, 2, 2, None]


def main():
m = np.matrix('-0.99 -0.98 -0.97 -0.96 -0.95 0.95 0.96 0.97 0.98 0.99; 1.1 1.09 1.08 1.07 1.06 1.06 1.07 1.08 1.09 1.1')
eps = 1
min_points = 3
print(dbscan(m, eps, min_points))
m = np.matrix(
"-0.99 -0.98 -0.97 -0.96 -0.95 0.95 0.96 0.97 0.98 0.99; 1.1 1.09 1.08 1.07 1.06 1.06 1.07 1.08 1.09 1.1"
)
eps = 1
min_points = 3
print(dbscan(m, eps, min_points))


main()
38 changes: 23 additions & 15 deletions code/artificial_intelligence/src/ISODATA_Clustering/ISODATA.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def volume_estimation(cluster, center):
num_of_points = len(cluster)
distance = []
for i in range(num_of_points):
distance.append(distance_2point(center[0], center[1], cluster[i][0], cluster[i][1]))
distance.append(
distance_2point(center[0], center[1], cluster[i][0], cluster[i][1])
)

return sum(distance) / num_of_points

Expand All @@ -33,7 +35,9 @@ def center_distance(centers):
if i == j:
pass
else:
D_ij[(i, j)] = distance_2point(centers[i][0], centers[i][1], centers[j][0], centers[j][1])
D_ij[(i, j)] = distance_2point(
centers[i][0], centers[i][1], centers[j][0], centers[j][1]
)
k += 1
return D_ij

Expand Down Expand Up @@ -66,7 +70,11 @@ def cluster_points_distribution(centers, points):
for i in range(points_len):
# iteration throught all centers
for j in range(centers_len):
distance.append(distance_2point(centers[j][0], centers[j][1], points[i][0], points[i][1]))
distance.append(
distance_2point(
centers[j][0], centers[j][1], points[i][0], points[i][1]
)
)
distances.append(distance)
distance = []

Expand Down Expand Up @@ -210,8 +218,12 @@ def clusterize():
length = len(clusters[i])
coef = 2 * (THETA_N + 1)

if (max_s[i] > THETA_S) and ((D_vol[i] > D and length > coef) or N_c < float(K) / 2):
center1, center2 = cluster_division(clusters[i], centers[i], vectors[i])
if (max_s[i] > THETA_S) and (
(D_vol[i] > D and length > coef) or N_c < float(K) / 2
):
center1, center2 = cluster_division(
clusters[i], centers[i], vectors[i]
)
del centers[i]
centers.append(center1)
centers.append(center2)
Expand All @@ -221,14 +233,14 @@ def clusterize():
pass

# for i in clusters:
# print(i)
# print(i)

# step 11
D_ij = center_distance(centers)
rang = {}
for coord in D_ij:
if D_ij[coord] < THETA_C:
rang[coord] = (D_ij[coord])
rang[coord] = D_ij[coord]
else:
pass

Expand All @@ -245,7 +257,7 @@ def clusterize():
return clusters


if __name__ == '__main__':
if __name__ == "__main__":
# if file called as a script
# cluster points
points1 = [
Expand All @@ -261,7 +273,7 @@ def clusterize():
(5, -6),
(6, -5),
(44, 49),
(45, 50)
(45, 50),
]
points2 = [
(-0.99, 1.09),
Expand All @@ -276,7 +288,7 @@ def clusterize():
(0.96, 1.06),
(0.97, 1.07),
(0.98, 1.08),
(0.99, 1.09)
(0.99, 1.09),
]
points3 = [
(-99, 109),
Expand All @@ -291,13 +303,9 @@ def clusterize():
(96, 106),
(97, 107),
(98, 108),
(99, 109)
(99, 109),
]
points = points3
cl = clusterize()
for i in cl:
print("Cl", i)




Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def plot_regression_line(x, y, b):
plt.plot(x, y_pred, color="r")

# putting labels
plt.xlabel('x')
plt.ylabel('y')
plt.xlabel("x")
plt.ylabel("y")

# function to show plot
plt.show()
Expand All @@ -47,8 +47,12 @@ def main():

# estimating coefficients
b = estimate_coef(x, y)
print("Estimated coefficients are:\nb_0 = {} \
\nb_1 = {}".format(b[0], b[1]))
print(
"Estimated coefficients are:\nb_0 = {} \
\nb_1 = {}".format(
b[0], b[1]
)
)

# plotting regression line
plot_regression_line(x, y, b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ def log_likelihood(features, target, weights):
return ll


def logistic_regression(features,
target,
num_steps,
learning_rate,
add_intercept=False):
def logistic_regression(
features, target, num_steps, learning_rate, add_intercept=False
):
if add_intercept:
intercept = np.ones((features.shape[0], 1))
features = np.hstack((intercept, features))
Expand All @@ -44,21 +42,19 @@ def logistic_regression(features,
np.random.seed(12)
num_observations = 5000

x1 = np.random.multivariate_normal([0, 0], [[1, .75], [.75, 1]],
num_observations)
x2 = np.random.multivariate_normal([1, 4], [[1, .75], [.75, 1]],
num_observations)
x1 = np.random.multivariate_normal([0, 0], [[1, 0.75], [0.75, 1]], num_observations)
x2 = np.random.multivariate_normal([1, 4], [[1, 0.75], [0.75, 1]], num_observations)

simulated_separableish_features = np.vstack((x1, x2)).astype(np.float32)
simulated_labels = np.hstack((np.zeros(num_observations),
np.ones(num_observations)))
simulated_labels = np.hstack((np.zeros(num_observations), np.ones(num_observations)))

plt.figure(figsize=(12, 8))
plt.scatter(
simulated_separableish_features[:, 0],
simulated_separableish_features[:, 1],
c=simulated_labels,
alpha=.4)
alpha=0.4,
)

plt.show()

Expand All @@ -69,6 +65,7 @@ def logistic_regression(features,
simulated_labels,
num_steps=300000,
learning_rate=5e-5,
add_intercept=True)
add_intercept=True,
)

print(weights)
30 changes: 11 additions & 19 deletions code/artificial_intelligence/src/artificial_neural_network/ann.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('dataset.csv')
dataset = pd.read_csv("dataset.csv")
X = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values

from sklearn.preprocessing import LabelEncoder, OneHotEncoder

labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
labelencoder_X_2 = LabelEncoder()
Expand All @@ -16,10 +17,11 @@
X = X[:, 1:]

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

from sklearn.preprocessing import StandardScaler

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
Expand All @@ -31,26 +33,16 @@
classifier = Sequential()

classifier.add(
Dense(
units=6,
kernel_initializer='uniform',
activation='relu',
input_dim=11))
Dense(units=6, kernel_initializer="uniform", activation="relu", input_dim=11)
)

classifier.add(Dense(units=6, kernel_initializer='uniform', activation='relu'))
classifier.add(Dense(units=6, kernel_initializer="uniform", activation="relu"))

classifier.add(
Dense(
units=1,
kernel_initializer='uniform',
activation='sigmoid'))
classifier.add(Dense(units=1, kernel_initializer="uniform", activation="sigmoid"))

classifier.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
classifier.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

classifier.fit(X_train, y_train, batch_size=10, epochs=100)

y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)
y_pred = y_pred > 0.5
Loading

0 comments on commit 308ba81

Please sign in to comment.