text_html_output_1.png
from sklearn.utils import shuffle

import matplotlib.pyplot as plt

import pandas as pd

import random

import re

df = pd.read_csv('/kaggle/input/kullanlcaklar/Womens_Clothing_E-Commerce_Reviews_1.csv')

df = shuffle(df)
df = df.reset_index(drop=True)

N = 10
population_size = 100
mutation_rate = 0.3
generations = 200

text_column_name = 'Title'
rating_column_name = 'Rating'

def clean_text(text):
    text = text.lower()
    text = re.sub('[^\\w\\s]', '', text)
    return text
def tokenize(text):
    return text.split()

def prepare_population(df, text_column_name):
    all_words = []
    for text in df[text_column_name]:
        clean_review = clean_text(text)
        tokens = tokenize(clean_review)
        all_words.extend(tokens)
    random.shuffle(all_words)
    sublists = [all_words[i:i + N] for i in range(0, len(all_words), N)]
    population = sublists
    return (population, all_words)

def count_words(text, word_list):
    count = 0
    for word in word_list:
        count += text.count(word)
    return count

def fitness(individual):
    correct_classification = 0
    for i in range(len(df)):
        row = df.iloc[i]
        text, rating = (row[text_column_name], row[rating_column_name])
        pozitif = count_words(text, individual[:N // 2])
        negatif = count_words(text, individual[N // 2:])
        if pozitif == negatif:
            if random.random() < 0.5:
                classification = 1
            else:
                classification = 5
        elif pozitif > negatif:
            classification = 5
        else:
            classification = 1
        if classification == rating:
            correct_classification += 1
    return correct_classification / len(df)

def fitness2(individual):
    correct_classification = 0
    for i in range(len(df)):
        row = df.iloc[i]
        text, rating = (row[text_column_name], row[rating_column_name])
        pozitif = count_words(text, individual[:N // 2])
        negatif = count_words(text, individual[N // 2:])
        if pozitif == negatif:
            if random.random() < 0.5:
                classification = 'negative'
            else:
                classification = 'positive'
        elif pozitif > negatif:
            classification = 'positive'
        else:
            classification = 'negative'
        if classification == rating:
            correct_classification += 1
    return correct_classification / len(df)

def crossover(parent1, parent2):
    n = N // 2
    child1 = parent1[:n] + parent2[n:]
    child2 = parent2[:n] + parent1[n:]
    return (child1, child2)

def mutate(individual, all_words):
    if random.random() < mutation_rate:
        i = random.randint(0, len(individual) - 1)
        new_word = random.choice(all_words)
        individual[i] = new_word
    return individual

def display_results(population, success_rates, average_success_rates):
    best_individual = max(population, key=fitness)
    print("En iyi birey:", best_individual)
    print("Başarı oranı:", fitness(best_individual))

    fig, ax = plt.subplots()
    ax.plot(success_rates, label="En iyi birey")
    ax.plot(average_success_rates, label="Ortalama başarı")
    ax.set_xlabel('Generation')
    ax.set_ylabel('Success Rate')
    ax.set_title('Success Rate per Generation')
    ax.legend()

    plt.show()

def display_results2(population, success_rates, average_success_rates):
    best_individual = max(population, key=fitness2)
    print("En iyi birey:", best_individual)
    print("Başarı oranı:", fitness2(best_individual))

    fig, ax = plt.subplots()
    ax.plot(success_rates, label="En iyi birey")
    ax.plot(average_success_rates, label="Ortalama başarı")
    ax.set_xlabel('Generation')
    ax.set_ylabel('Success Rate')
    ax.set_title('Success Rate per Generation')
    ax.legend()

    plt.show()

success_rates = []
average_success_rates = []
for generation in range(generations):
    fitness_values = []
    for individual in population:
        fitness_values.append(fitness(individual))
    average_success_rate = sum(fitness_values) / population_size
    average_success_rates.append(average_success_rate)
    children = []
    for i in range(population_size // 2):
        parents = random.choices(population, weights=fitness_values, k=2)
        child1, child2 = crossover(parents[0], parents[1])
        children.append(child1)
        children.append(child2)
    mutations = []
    for child in children:
        mutations.append(mutate(child, all_words))
    population = mutations
    best_individual = max(population, key=fitness)
    success_rate = fitness(best_individual)
    success_rates.append(success_rate)

display_results(population, success_rates, average_success_rates)