<jupyter_start><jupyter_text>Aşcı Test Ortamı<jupyter_code>import numpy as np
import numpy.linalg as LA
import sklearn
from sklearn.neighbors import NearestNeighbors
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import random
class Smote(object):

    def __init__(self, minority_samples):
        self.samples = minority_samples
        pass

    def oversample(self, N: int = 500, k: int = 5):
        N = N//100
        numsamples, numattrs = self.samples.shape
        synthetic = np.zeros( (N*numsamples, numattrs))
        
        matrix=[]
        indices = k_NN(self.samples)
        newindex = 0
        for i in range(numsamples):
           while N > 0:
               nn = random.randint(0, k)
               for attr in range(numattrs):
                   dif = self.samples[indices[i, nn], attr] -self.samples[i, attr]
                   gap = random.uniform(0,1)
                   synthetic[newindex,attr] = self.samples[i, attr] + gap*dif
               newindex += 1
               N -= 1
        return synthetic
        #for m in range(len(indices)):
        #    t=self.samples[indices[m]]
        #    newt=pd.DataFrame(t)
        #    matrix.append([])
        #    for j in range(len(newt.columns)):
        #        matrix[m].append(random.choice(newt[j]))
        return matrix

def k_NN(X: np.ndarray, k: int = 5):
    indices_matrix = np.zeros((X.shape[0], k))
    for idx, row in enumerate(X):
        dist = LA.norm(X-row, axis=1)
        indices = np.argsort(dist)[:k]
        indices_matrix[idx,:] = indices 
    return indices_matrix.astype(int)  

def nearest_neighbour(X, k=2):
  nbs=NearestNeighbors(n_neighbors=k, metric='euclidean', algorithm='kd_tree').fit(X)
  euclidean,indices= nbs.kneighbors(X)
  return indices


raw_data = pd.read_csv("data.csv")
bankrupt_pd = raw_data["Bankrupt?"]
features_pd = raw_data.drop(["Bankrupt?"], axis=1)
bankrupt = bankrupt_pd.to_numpy()
asd = np.array([[5, 5, 5], [10, 10, 10], [1, 2, 3]])
asd2 = asd - np.array([1, 2, 3])
asd3 = np.array([1, 3, 2, 5, 8, 4, 0])
print(np.argsort(asd3))
np.argsort(asd3)[-3:]
df = pd.DataFrame(np.arange(12).reshape(3, 4),
                  columns=['A', 'B', 'C', 'D'])
minority = features_pd[bankrupt_pd == 1]
smote = Smote( minority.to_numpy() )
oversamples = smote.oversample()

oversamples.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14068441064638784, 'fraction_numerical': 0.021546261089987327, 'mean_word_length': 2.654320987654321, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '554436', 'license': 'Apache-2.0', 'n_tokens_mistral': 891, 'n_tokens_neox': 839, 'n_words': 189, 'path': 'smote.ipynb', 'repo_name': 'ArdaAsci/EEE485_Project', 'size': '2367'}