Facebook
From Selenay, 9 Months ago, written in Python.
Embed
Download Paste or View Raw
Hits: 425
  1. import random
  2. import math
  3.  
  4. data = [
  5.     [5.1, 3.5, 1.4, 0.2, 'setosa'],
  6.     [4.9, 3.0, 1.4, 0.2, 'setosa'],
  7.     [4.7, 3.2, 1.3, 0.2, 'setosa'],
  8.     [4.6, 3.1, 1.5, 0.2, 'setosa'],
  9.     [5.0, 3.6, 1.4, 0.2, 'setosa'],
  10.     [5.4, 3.9, 1.7, 0.4, 'setosa'],
  11.     [4.6, 3.4, 1.4, 0.3, 'setosa'],
  12.     [5.0, 3.4, 1.5, 0.2, 'setosa'],
  13.     [4.4, 2.9, 1.4, 0.2, 'setosa'],
  14.     [4.9, 3.1, 1.5, 0.1, 'setosa'],
  15.     [5.4, 3.7, 1.5, 0.2, 'setosa'],
  16.     [4.8, 3.4, 1.6, 0.2, 'setosa'],
  17.     [4.8, 3.0, 1.4, 0.1, 'setosa'],
  18.     [4.3, 3.0, 1.1, 0.1, 'setosa'],
  19.     [5.8, 4.0, 1.2, 0.2, 'setosa'],
  20.     [6.7, 3.3, 5.7, 2.5, 'virginica'],
  21.     [6.7, 3.0, 5.2, 2.3, 'virginica'],
  22.     [6.3, 2.5, 5.0, 1.9, 'virginica'],
  23.     [6.5, 3.0, 5.2, 2.0, 'virginica'],
  24.     [6.2, 3.4, 5.4, 2.3, 'virginica'],
  25.     [5.9, 3.0, 5.1, 1.8, 'virginica']
  26. ]
  27.  
  28. features = [row[:-1] for row in data]
  29. target = [1 if row[-1] == 'setosa' else 0 for row in data]
  30.  
  31. print("Özellikler:")
  32. for feature in features:
  33.     print(feature)
  34.  
  35. print("\nTarget:")
  36. for label in target:
  37.     print(label)
  38.  
  39. def mean(data):
  40.     return sum(data) / len(data)
  41.  
  42. def calculate_means(data):
  43.     num_features = len(data[0])
  44.     return [mean([row[i] for row in data]) for i in range(num_features)]
  45.  
  46. means = calculate_means(features)
  47.  
  48. print("\nOrtalama:")
  49. print(means)
  50.  
  51. def std_dev(data):
  52.     m = mean(data)
  53.     return (sum([(x - m)**2 for x in data]) / len(data))**0.5
  54.  
  55. def calculate_stds(data):
  56.     num_features = len(data[0])
  57.     return [std_dev([row[i] for row in data]) for i in range(num_features)]
  58.  
  59. stds = calculate_stds(features)
  60.  
  61. print("\nStandart Sapma:")
  62. print(stds)
  63.  
  64. def plot_histogram(feature_index, data):
  65.     values = [row[feature_index] for row in data]
  66.     import matplotlib.pyplot as plt
  67.     plt.figure(figsize=(8, 6))
  68.     plt.hist(values, bins=20)
  69.     plt.title(f"Feature {feature_index + 1}")
  70.     plt.show()
  71.  
  72. for i in range(len(features[0])):
  73.     plot_histogram(i, features)
  74.  
  75. def train_test_split(features, target, test_size=0.2, random_state=None):
  76.     random.seed(random_state)
  77.     data = list(zip(features, target))
  78.     random.shuffle(data)
  79.     split_idx = int(len(data) * (1 - test_size))
  80.     train_data, test_data = data[:split_idx], data[split_idx:]
  81.     X_train, y_train = zip(*train_data)
  82.     X_test, y_test = zip(*test_data)
  83.     return X_train, X_test, y_train, y_test
  84.  
  85. def standardize_data(features):
  86.     num_features = len(features[0])
  87.     means = calculate_means(features)
  88.     stds = calculate_stds(features)
  89.    
  90.     for i in range(len(stds)):
  91.         if stds[i] == 0:
  92.             stds[i] = 1
  93.    
  94.     return [[(row[i] - means[i]) / stds[i] for i in range(num_features)] for row in features]
  95.  
  96. def logistic_regression(X_train, y_train):
  97.     weights = [0] * len(X_train[0])
  98.     lr = 0.01
  99.     epochs = 100
  100.     for _ in range(epochs):
  101.         for i in range(len(X_train)):
  102.             prediction = sum([X_train[i][j] * weights[j] for j in range(len(X_train[i]))])
  103.             error = y_train[i] - (1 / (1 + math.exp(-prediction)))
  104.             for j in range(len(weights)):
  105.                 weights[j] += lr * error * X_train[i][j]
  106.     return weights
  107.  
  108. def predict(X_test, weights):
  109.     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))]
  110.  
  111. def confusion_matrix(y_test, predictions):
  112.     tp = sum([1 for i in range(len(y_test)) if y_test[i] == 1 and predictions[i] >= 0.5])
  113.     tn = sum([1 for i in range(len(y_test)) if y_test[i] == 0 and predictions[i] < 0.5])
  114.     fp = sum([1 for i in range(len(y_test)) if y_test[i] == 0 and predictions[i] >= 0.5])
  115.     fn = sum([1 for i in range(len(y_test)) if y_test[i] == 1 and predictions[i] < 0.5])
  116.     return [[tp, fp], [fn, tn]]
  117.  
  118. def classification_report(conf_matrix):
  119.     tp, fp, fn, tn = conf_matrix[0][0], conf_matrix[0][1], conf_matrix[1][0], conf_matrix[1][1]
  120.      precisi / (tp + fp) if (tp + fp) != 0 else 0.0
  121.     recall = tp / (tp + fn) if (tp + fn) != 0 else 0.0
  122.     f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) != 0 else 0.0
  123.     return {"Precision": precision, "Recall": recall, "F1 Score": f1_score}
  124.  
  125.  
  126. X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)
  127. X_train = standardize_data(X_train)
  128. X_test = standardize_data(X_test)
  129. weights = logistic_regression(X_train, y_train)
  130.  predicti weights)
  131.  c predictions)
  132. report = classification_report(conf_matrix)
  133.  
  134. print("\nKarışıklık Matrisi:")
  135. print(conf_matrix)
  136. print("\nSınıflandırma Raporu:")
  137. print(report)