import random import math data = [ [5.1, 3.5, 1.4, 0.2, 'setosa'], [4.9, 3.0, 1.4, 0.2, 'setosa'], [4.7, 3.2, 1.3, 0.2, 'setosa'], [4.6, 3.1, 1.5, 0.2, 'setosa'], [5.0, 3.6, 1.4, 0.2, 'setosa'], [5.4, 3.9, 1.7, 0.4, 'setosa'], [4.6, 3.4, 1.4, 0.3, 'setosa'], [5.0, 3.4, 1.5, 0.2, 'setosa'], [4.4, 2.9, 1.4, 0.2, 'setosa'], [4.9, 3.1, 1.5, 0.1, 'setosa'], [5.4, 3.7, 1.5, 0.2, 'setosa'], [4.8, 3.4, 1.6, 0.2, 'setosa'], [4.8, 3.0, 1.4, 0.1, 'setosa'], [4.3, 3.0, 1.1, 0.1, 'setosa'], [5.8, 4.0, 1.2, 0.2, 'setosa'], [6.7, 3.3, 5.7, 2.5, 'virginica'], [6.7, 3.0, 5.2, 2.3, 'virginica'], [6.3, 2.5, 5.0, 1.9, 'virginica'], [6.5, 3.0, 5.2, 2.0, 'virginica'], [6.2, 3.4, 5.4, 2.3, 'virginica'], [5.9, 3.0, 5.1, 1.8, 'virginica'] ] features = [row[:-1] for row in data] target = [1 if row[-1] == 'setosa' else 0 for row in data] print("Özellikler:") for feature in features: print(feature) print("\nTarget:") for label in target: print(label) def mean(data): return sum(data) / len(data) def calculate_means(data): num_features = len(data[0]) return [mean([row[i] for row in data]) for i in range(num_features)] means = calculate_means(features) print("\nOrtalama:") print(means) def std_dev(data): m = mean(data) return (sum([(x - m)**2 for x in data]) / len(data))**0.5 def calculate_stds(data): num_features = len(data[0]) return [std_dev([row[i] for row in data]) for i in range(num_features)] stds = calculate_stds(features) print("\nStandart Sapma:") print(stds) def plot_histogram(feature_index, data): values = [row[feature_index] for row in data] import matplotlib.pyplot as plt plt.figure(figsize=(8, 6)) plt.hist(values, bins=20) plt.title(f"Feature {feature_index + 1}") plt.show() for i in range(len(features[0])): plot_histogram(i, features) def train_test_split(features, target, test_size=0.2, random_state=None): random.seed(random_state) data = list(zip(features, target)) random.shuffle(data) split_idx = int(len(data) * (1 - test_size)) train_data, test_data = data[:split_idx], data[split_idx:] X_train, y_train = zip(*train_data) X_test, y_test = zip(*test_data) return X_train, X_test, y_train, y_test def standardize_data(features): num_features = len(features[0]) means = calculate_means(features) stds = calculate_stds(features) for i in range(len(stds)): if stds[i] == 0: stds[i] = 1 return [[(row[i] - means[i]) / stds[i] for i in range(num_features)] for row in features] def logistic_regression(X_train, y_train): weights = [0] * len(X_train[0]) lr = 0.01 epochs = 100 for _ in range(epochs): for i in range(len(X_train)): prediction = sum([X_train[i][j] * weights[j] for j in range(len(X_train[i]))]) error = y_train[i] - (1 / (1 + math.exp(-prediction))) for j in range(len(weights)): weights[j] += lr * error * X_train[i][j] return weights def predict(X_test, weights): return [(1 / (1 + math.exp(-sum([X_test[i][j] * weights[j] for j in range(len(X_test[i]))])))) for i in range(len(X_test))] def confusion_matrix(y_test, predictions): tp = sum([1 for i in range(len(y_test)) if y_test[i] == 1 and predictions[i] >= 0.5]) tn = sum([1 for i in range(len(y_test)) if y_test[i] == 0 and predictions[i] < 0.5]) fp = sum([1 for i in range(len(y_test)) if y_test[i] == 0 and predictions[i] >= 0.5]) fn = sum([1 for i in range(len(y_test)) if y_test[i] == 1 and predictions[i] < 0.5]) return [[tp, fp], [fn, tn]] def classification_report(conf_matrix): tp, fp, fn, tn = conf_matrix[0][0], conf_matrix[0][1], conf_matrix[1][0], conf_matrix[1][1] precisi / (tp + fp) if (tp + fp) != 0 else 0.0 recall = tp / (tp + fn) if (tp + fn) != 0 else 0.0 f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) != 0 else 0.0 return {"Precision": precision, "Recall": recall, "F1 Score": f1_score} X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42) X_train = standardize_data(X_train) X_test = standardize_data(X_test) weights = logistic_regression(X_train, y_train) predicti weights) c predictions) report = classification_report(conf_matrix) print("\nKarışıklık Matrisi:") print(conf_matrix) print("\nSınıflandırma Raporu:") print(report)