Skip to content

Commit bd65c01

Browse files
authored
Merge branch 'master' into patch-11
2 parents f19d060 + 2b7ea53 commit bd65c01

File tree

329 files changed

+100514
-5260
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

329 files changed

+100514
-5260
lines changed

Diff for: .travis.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ matrix:
5454
env:
5555
- MATRIX_EVAL="CXX=g++-7"
5656

57+
# Autoformat Python code
58+
- os: linux
59+
language: python
60+
python: 3.6
61+
script:
62+
- pip install black
63+
- ./scripts/python_code_style_checker.sh
64+
5765
before_install:
5866
- eval "${MATRIX_EVAL}"
59-

Diff for: code/artificial_intelligence/src/DBSCAN_Clustering/dbscan.py

+27-13
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@
66
UNCLASSIFIED = False
77
NOISE = None
88

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

12-
def _eps_neighborhood(p,q,eps):
13-
return _dist(p,q) < eps
10+
def _dist(p, q):
11+
return math.sqrt(np.power(p - q, 2).sum())
12+
13+
14+
def _eps_neighborhood(p, q, eps):
15+
return _dist(p, q) < eps
16+
1417

1518
def _region_query(m, point_id, eps):
1619
n_points = m.shape[1]
1720
seeds = []
1821
for i in range(0, n_points):
19-
if _eps_neighborhood(m[:,point_id], m[:,i], eps):
22+
if _eps_neighborhood(m[:, point_id], m[:, i], eps):
2023
seeds.append(i)
2124
return seeds
2225

26+
2327
def _expand_cluster(m, classifications, point_id, cluster_id, eps, min_points):
2428
seeds = _region_query(m, point_id, eps)
2529
if len(seeds) < min_points:
@@ -36,14 +40,17 @@ def _expand_cluster(m, classifications, point_id, cluster_id, eps, min_points):
3640
if len(results) >= min_points:
3741
for i in range(0, len(results)):
3842
result_point = results[i]
39-
if classifications[result_point] == UNCLASSIFIED or \
40-
classifications[result_point] == NOISE:
43+
if (
44+
classifications[result_point] == UNCLASSIFIED
45+
or classifications[result_point] == NOISE
46+
):
4147
if classifications[result_point] == UNCLASSIFIED:
4248
seeds.append(result_point)
4349
classifications[result_point] = cluster_id
4450
seeds = seeds[1:]
4551
return True
4652

53+
4754
def dbscan(m, eps, min_points):
4855
"""Implementation of Density Based Spatial Clustering of Applications with Noise
4956
See https://en.wikipedia.org/wiki/DBSCAN
@@ -65,22 +72,29 @@ def dbscan(m, eps, min_points):
6572
n_points = m.shape[1]
6673
classifications = [UNCLASSIFIED] * n_points
6774
for point_id in range(0, n_points):
68-
point = m[:,point_id]
75+
point = m[:, point_id]
6976
if classifications[point_id] == UNCLASSIFIED:
70-
if _expand_cluster(m, classifications, point_id, cluster_id, eps, min_points):
77+
if _expand_cluster(
78+
m, classifications, point_id, cluster_id, eps, min_points
79+
):
7180
cluster_id = cluster_id + 1
7281
return classifications
7382

83+
7484
# def test_dbscan():
7585
# 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')
7686
# eps = 0.5
7787
# min_points = 2
7888
# assert dbscan(m, eps, min_points) == [1, 1, 1, 2, 2, 2, None]
7989

90+
8091
def main():
81-
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')
82-
eps = 1
83-
min_points = 3
84-
print(dbscan(m, eps, min_points))
92+
m = np.matrix(
93+
"-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"
94+
)
95+
eps = 1
96+
min_points = 3
97+
print(dbscan(m, eps, min_points))
98+
8599

86100
main()

Diff for: code/artificial_intelligence/src/ISODATA_Clustering/ISODATA.py

+23-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ def volume_estimation(cluster, center):
1111
num_of_points = len(cluster)
1212
distance = []
1313
for i in range(num_of_points):
14-
distance.append(distance_2point(center[0], center[1], cluster[i][0], cluster[i][1]))
14+
distance.append(
15+
distance_2point(center[0], center[1], cluster[i][0], cluster[i][1])
16+
)
1517

1618
return sum(distance) / num_of_points
1719

@@ -33,7 +35,9 @@ def center_distance(centers):
3335
if i == j:
3436
pass
3537
else:
36-
D_ij[(i, j)] = distance_2point(centers[i][0], centers[i][1], centers[j][0], centers[j][1])
38+
D_ij[(i, j)] = distance_2point(
39+
centers[i][0], centers[i][1], centers[j][0], centers[j][1]
40+
)
3741
k += 1
3842
return D_ij
3943

@@ -66,7 +70,11 @@ def cluster_points_distribution(centers, points):
6670
for i in range(points_len):
6771
# iteration throught all centers
6872
for j in range(centers_len):
69-
distance.append(distance_2point(centers[j][0], centers[j][1], points[i][0], points[i][1]))
73+
distance.append(
74+
distance_2point(
75+
centers[j][0], centers[j][1], points[i][0], points[i][1]
76+
)
77+
)
7078
distances.append(distance)
7179
distance = []
7280

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

213-
if (max_s[i] > THETA_S) and ((D_vol[i] > D and length > coef) or N_c < float(K) / 2):
214-
center1, center2 = cluster_division(clusters[i], centers[i], vectors[i])
221+
if (max_s[i] > THETA_S) and (
222+
(D_vol[i] > D and length > coef) or N_c < float(K) / 2
223+
):
224+
center1, center2 = cluster_division(
225+
clusters[i], centers[i], vectors[i]
226+
)
215227
del centers[i]
216228
centers.append(center1)
217229
centers.append(center2)
@@ -221,14 +233,14 @@ def clusterize():
221233
pass
222234

223235
# for i in clusters:
224-
# print(i)
236+
# print(i)
225237

226238
# step 11
227239
D_ij = center_distance(centers)
228240
rang = {}
229241
for coord in D_ij:
230242
if D_ij[coord] < THETA_C:
231-
rang[coord] = (D_ij[coord])
243+
rang[coord] = D_ij[coord]
232244
else:
233245
pass
234246

@@ -245,7 +257,7 @@ def clusterize():
245257
return clusters
246258

247259

248-
if __name__ == '__main__':
260+
if __name__ == "__main__":
249261
# if file called as a script
250262
# cluster points
251263
points1 = [
@@ -261,7 +273,7 @@ def clusterize():
261273
(5, -6),
262274
(6, -5),
263275
(44, 49),
264-
(45, 50)
276+
(45, 50),
265277
]
266278
points2 = [
267279
(-0.99, 1.09),
@@ -276,7 +288,7 @@ def clusterize():
276288
(0.96, 1.06),
277289
(0.97, 1.07),
278290
(0.98, 1.08),
279-
(0.99, 1.09)
291+
(0.99, 1.09),
280292
]
281293
points3 = [
282294
(-99, 109),
@@ -291,13 +303,9 @@ def clusterize():
291303
(96, 106),
292304
(97, 107),
293305
(98, 108),
294-
(99, 109)
306+
(99, 109),
295307
]
296308
points = points3
297309
cl = clusterize()
298310
for i in cl:
299311
print("Cl", i)
300-
301-
302-
303-

Diff for: code/artificial_intelligence/src/Linear_Regression/linear_regression.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def plot_regression_line(x, y, b):
3333
plt.plot(x, y_pred, color="r")
3434

3535
# putting labels
36-
plt.xlabel('x')
37-
plt.ylabel('y')
36+
plt.xlabel("x")
37+
plt.ylabel("y")
3838

3939
# function to show plot
4040
plt.show()
@@ -47,8 +47,12 @@ def main():
4747

4848
# estimating coefficients
4949
b = estimate_coef(x, y)
50-
print("Estimated coefficients are:\nb_0 = {} \
51-
\nb_1 = {}".format(b[0], b[1]))
50+
print(
51+
"Estimated coefficients are:\nb_0 = {} \
52+
\nb_1 = {}".format(
53+
b[0], b[1]
54+
)
55+
)
5256

5357
# plotting regression line
5458
plot_regression_line(x, y, b)

Diff for: code/artificial_intelligence/src/Logistic_Regression/Logistic_Regression.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ def log_likelihood(features, target, weights):
1414
return ll
1515

1616

17-
def logistic_regression(features,
18-
target,
19-
num_steps,
20-
learning_rate,
21-
add_intercept=False):
17+
def logistic_regression(
18+
features, target, num_steps, learning_rate, add_intercept=False
19+
):
2220
if add_intercept:
2321
intercept = np.ones((features.shape[0], 1))
2422
features = np.hstack((intercept, features))
@@ -44,21 +42,19 @@ def logistic_regression(features,
4442
np.random.seed(12)
4543
num_observations = 5000
4644

47-
x1 = np.random.multivariate_normal([0, 0], [[1, .75], [.75, 1]],
48-
num_observations)
49-
x2 = np.random.multivariate_normal([1, 4], [[1, .75], [.75, 1]],
50-
num_observations)
45+
x1 = np.random.multivariate_normal([0, 0], [[1, 0.75], [0.75, 1]], num_observations)
46+
x2 = np.random.multivariate_normal([1, 4], [[1, 0.75], [0.75, 1]], num_observations)
5147

5248
simulated_separableish_features = np.vstack((x1, x2)).astype(np.float32)
53-
simulated_labels = np.hstack((np.zeros(num_observations),
54-
np.ones(num_observations)))
49+
simulated_labels = np.hstack((np.zeros(num_observations), np.ones(num_observations)))
5550

5651
plt.figure(figsize=(12, 8))
5752
plt.scatter(
5853
simulated_separableish_features[:, 0],
5954
simulated_separableish_features[:, 1],
6055
c=simulated_labels,
61-
alpha=.4)
56+
alpha=0.4,
57+
)
6258

6359
plt.show()
6460

@@ -69,6 +65,7 @@ def logistic_regression(features,
6965
simulated_labels,
7066
num_steps=300000,
7167
learning_rate=5e-5,
72-
add_intercept=True)
68+
add_intercept=True,
69+
)
7370

7471
print(weights)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import pandas as pd
4+
5+
dataset = pd.read_csv("dataset.csv")
6+
X = dataset.iloc[:, 3:13].values
7+
y = dataset.iloc[:, 13].values
8+
9+
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
10+
11+
labelencoder_X_1 = LabelEncoder()
12+
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
13+
labelencoder_X_2 = LabelEncoder()
14+
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])
15+
onehotencoder = OneHotEncoder(categorical_features=[1])
16+
X = onehotencoder.fit_transform(X).toarray()
17+
X = X[:, 1:]
18+
19+
from sklearn.model_selection import train_test_split
20+
21+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
22+
23+
from sklearn.preprocessing import StandardScaler
24+
25+
sc = StandardScaler()
26+
X_train = sc.fit_transform(X_train)
27+
X_test = sc.transform(X_test)
28+
29+
import keras
30+
from keras.models import Sequential
31+
from keras.layers import Dense
32+
33+
classifier = Sequential()
34+
35+
classifier.add(
36+
Dense(units=6, kernel_initializer="uniform", activation="relu", input_dim=11)
37+
)
38+
39+
classifier.add(Dense(units=6, kernel_initializer="uniform", activation="relu"))
40+
41+
classifier.add(Dense(units=1, kernel_initializer="uniform", activation="sigmoid"))
42+
43+
classifier.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
44+
45+
classifier.fit(X_train, y_train, batch_size=10, epochs=100)
46+
47+
y_pred = classifier.predict(X_test)
48+
y_pred = y_pred > 0.5

0 commit comments

Comments
 (0)