diff --git a/src/ml/decision_tree.py b/src/ml/decision_tree.py index 9d402df..aa4f2c8 100644 --- a/src/ml/decision_tree.py +++ b/src/ml/decision_tree.py @@ -225,14 +225,3 @@ def build_tree(self, rows, labels, value=""): for data, label in zip(data_subsets, label_subsets) ] return self.decision_node(question, branches, value) - - -# Sample usage -dt = DecisionTree() -cars = dt.make_cars("car.data") # No value for filename defaults to "car.data" -random.shuffle(cars) -cars = cars[:1000] -car_data = [x[:-1] for x in cars] -car_labels = [x[-1] for x in cars] -tree = dt.build_tree(car_data, car_labels) -dt.print_tree(tree) diff --git a/src/ml/k_means.py b/src/ml/k_means.py index 19ec723..ea904a7 100644 --- a/src/ml/k_means.py +++ b/src/ml/k_means.py @@ -2,6 +2,17 @@ import random class KMeans: + """ + An implementation of the K-Means clustering algorithm. + + # Example usage + data = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]]) + kmeans = KMeans(k=2) + kmeans.fit(data) + print("Cluster centroids:", kmeans.centroids) + print("Predicted clusters:", kmeans.predict(data)) + """ + def __init__(self, k=2, max_iterations=100): self.k = k # Number of clusters self.max_iterations = max_iterations # Maximum number of iterations @@ -50,10 +61,3 @@ def predict(self, data): cluster_idx = np.argmin(distances) predictions.append(cluster_idx) return predictions - -# Example usage -data = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]]) -kmeans = KMeans(k=2) -kmeans.fit(data) -print("Cluster centroids:", kmeans.centroids) -print("Predicted clusters:", kmeans.predict(data)) diff --git a/src/ml/linear_regression.py b/src/ml/linear_regression.py index b9a789f..4bd6108 100644 --- a/src/ml/linear_regression.py +++ b/src/ml/linear_regression.py @@ -1,6 +1,21 @@ import numpy as np class LinearRegression: + """class for utilizing a linear regression algorithm + + # # Example usage + # X = np.array([0, 1, 2, 3, 4, 5]) + # y = np.array([0, 2, 4, 6, 8, 10]) # Example data + + # model = LinearRegression() + # model.fit(X, y) + # predictions = model.predict(X) + + # print("Coefficients:", model.coefficients) + # print("Intercept:", model.intercept) + # print("Predictions:", predictions) + """ + def __init__(self): self.coefficients = None self.intercept = None @@ -20,15 +35,3 @@ def predict(self, X): if len(X.shape) == 1: X = X.reshape(-1, 1) return np.dot(X, self.coefficients) + self.intercept - -# Example usage -X = np.array([0, 1, 2, 3, 4, 5]) -y = np.array([0, 2, 4, 6, 8, 10]) # Example data - -model = LinearRegression() -model.fit(X, y) -predictions = model.predict(X) - -print("Coefficients:", model.coefficients) -print("Intercept:", model.intercept) -print("Predictions:", predictions) diff --git a/src/search/binary_search.py b/src/search/binary_search.py index 7101db5..237058a 100644 --- a/src/search/binary_search.py +++ b/src/search/binary_search.py @@ -1,3 +1,18 @@ +""" +Binary Search algorithm + +# Example usage +arr = [3, 4, 5, 6, 7, 8, 9] +bs = BinarySearch(arr) +target = 5 +result = bs.search(target) + +if result != -1: + print(f"Element {target} is present at index {result}.") +else: + print(f"Element {target} is not present in the array.") +""" + class BinarySearch: def __init__(self, arr): self.arr = sorted(arr) # Ensuring the array is sorted @@ -18,14 +33,3 @@ def search(self, target): right = mid - 1 return -1 # Target not found - -# Example usage -arr = [3, 4, 5, 6, 7, 8, 9] -bs = BinarySearch(arr) -target = 5 -result = bs.search(target) - -if result != -1: - print(f"Element {target} is present at index {result}.") -else: - print(f"Element {target} is not present in the array.")