<jupyter_start><jupyter_text>Sbagliato<jupyter_code>import logging
import sklearn.datasets as ds
import pandas as pd
import numpy as np
import math as mt

import time

from itertools import product
from sklearn.metrics import mean_squared_error
from mulearn import FuzzyInductor
from mulearn.optimization import TensorFlowSolver
from mulearn.kernel import GaussianKernel


logger = logging.getLogger()
logger.setLevel(logging.INFO)
def create_dataset(name):

    #load dataset, in iris_X values, in iris_y labels 0 1 2
    iris_X, iris_y = ds.load_iris(return_X_y=True)  

    labels = ("Setosa", "Versicolor", "Virginica") 

    #dataframe with correct labels for respective values
    df = pd.DataFrame(iris_X, columns=["Sepal length", "Sepal width",
                                       "Petal length", "Petal width"])

    #associating 
    df['Class'] = iris_y
    df['Class'] = df['Class'].map(lambda c: labels[c])

    #dataset copy for labels 0 1
    selected_iris_dataset = iris_y.copy()

    #dataset selected with labels
    if(name == "Setosa"):        
        selected_iris_dataset[selected_iris_dataset != 0] = 2
        selected_iris_dataset[selected_iris_dataset == 0] = 1
        selected_iris_dataset[selected_iris_dataset == 2] = 0
    elif(name == "Versicolor"):
        selected_iris_dataset[selected_iris_dataset==2] = 0    
    elif(name == "Virginica"):
        selected_iris_dataset[selected_iris_dataset != 2] = 0
        selected_iris_dataset[selected_iris_dataset == 2] = 1


    return iris_X, selected_iris_dataset
def create_handler(path):   
    
    
    fhandler = logging.FileHandler(filename = path)
    
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fhandler.setFormatter(formatter)
    
    logger.addHandler(fhandler)  
    
    return fhandler
def main(nome, c, sigma, penalization, learning_rate):  
    
    handler = create_handler(f"../log/Learning-Rate/{nome}/c{str(c).replace('.','')}"
                             f"_sigma{str(sigma).replace('.','')}"
                             f"_penalization{str(penalization).replace('.','')}"
                             f"_{str(learning_rate)}.log")

    iris_X, selected_iris_dataset = create_dataset(nome)

    #parametri     
    #c = 0.05
    #sigma = 0.1
    #penalization = 0.1 

    n_iter = 100

    # Gurobi solver & fit
    fi = FuzzyInductor(c=c, k=GaussianKernel(sigma=sigma))
    fi.fit(iris_X, selected_iris_dataset)

    # rmse gurobi
    rmse_gurobi = mean_squared_error(selected_iris_dataset, fi.predict(iris_X), squared=False)
    logger.info("RMSE GUROBI: " + str(rmse_gurobi))

    # TensorFlow solver
    fi = FuzzyInductor(solver=TensorFlowSolver(n_iter=n_iter, optimizer=Adam(learning_rate=learning_rate),
                                               penalization=penalization),c=c,k=GaussianKernel(sigma=sigma))
    try:
        fi.fit(iris_X, selected_iris_dataset)
    except (ModuleNotFoundError, ValueError):
        print('Tensorflow not available')


    # rmse TensorFlow
    rmse_tensorflow = mean_squared_error(selected_iris_dataset, fi.predict(iris_X), squared=False)
    logger.info("RMSE TENSORFLOW: " + str(rmse_tensorflow))

    # calcolo distanza
    distance = abs(rmse_tensorflow - rmse_gurobi)
    logger.info("DISTANCE RMSE: " + str(distance))

    #coppia n_iter, distance
    couple = [(n_iter, distance)]
    logger.info("COUPLE(N_ITER,DISTANCE RMSE): " + str(couple))

    # salvo i chi
    chi_ = fi.chis_
    logger.info("CHI: " + str(chi_))

    #incremento n
    n = 200

    # faccio ciclo fino a 10000
    while n <= 1500:

        # TensorFlow solver
        fi = FuzzyInductor(solver=TensorFlowSolver(initial_values=chi_, n_iter=n_iter, 
                                                   optimizer=Adam(learning_rate=learning_rate),
                                                   penalization=penalization),c=c,k=GaussianKernel(sigma=sigma))
        try:
            fi.fit(iris_X, selected_iris_dataset)
        except (ModuleNotFoundError, ValueError):
            print('Tensorflow not available')


        # rmse TensorFlow
        rmse_tensorflow = mean_squared_error(selected_iris_dataset, fi.predict(iris_X), squared=False)
        logger.info("RMSE TENSORFLOW: " + str(rmse_tensorflow))

        # calcolo distanza
        distance = abs(rmse_tensorflow - rmse_gurobi)
        logger.info("DISTANCE RMSE: " + str(distance))

        #coppia n_iter, distance
        couple = [(n, distance)]
        logger.info("COUPLE(N_ITER,DISTANCE RMSE): " + str(couple))

        # salvo i chi
        chi_ = fi.chis_
        logger.info("CHI: " + str(chi_))

        #incremento n_iter
        n += 100

    logger.removeHandler(handler)
main("Setosa",1,1,1,1)
for i in list(product(["Virginica"],[1,75,200],[0.1,0.25,0.5],[0.1,10,100], repeat=1)):
    #if i not in [('Virginica', 0.05, 0.1, 0.1)]:
    main(*i)<jupyter_output>100%|██████████| 100/100 [00:19<00:00,  5.06it/s]
100%|██████████| 100/100 [00:19<00:00,  5.16it/s]
100%|██████████| 100/100 [00:19<00:00,  5.11it/s]
100%|██████████| 100/100 [00:20<00:00,  4.93it/s]
100%|██████████| 100/100 [00:19<00:00,  5.22it/s]
100%|██████████| 100/100 [00:21<00:00,  4.59it/s]
100%|██████████| 100/100 [00:18<00:00,  5.49it/s]
100%|██████████| 100/100 [00:20<00:00,  4.89it/s]
100%|██████████| 100/100 [00:19<00:00,  5.14it/s]
100%|██████████| 100/100 [00:25<00:00,  3.89it/s]
100%|██████████| 100/100 [00:22<00:00,  4.51it/s]
100%|██████████| 100/100 [00:18<00:00,  5.31it/s]
100%|██████████| 100/100 [00:20<00:00,  4.96it/s]
100%|██████████| 100/100 [00:20<00:00,  4.81it/s]
100%|██████████| 100/100 [00:22<00:00,  4.50it/s]
100%|██████████| 100/100 [00:20<00:00,  4.92it/s]
100%|██████████| 100/100 [00:18<00:00,  5.37it/s]
100%|██████████| 100/100 [00:17<00:00,  5.74it/s]
100%|██████████| 100/100 [00:16<00:00,  5.90it/s]
100%|██████████| 100/100 [00:17<00:00,  5.79it/s]
[...]
{'fraction_non_alphanumeric': 0.17043155765340526, 'fraction_numerical': 0.0799055967633176, 'mean_word_length': 3.128740431454419, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '136425', 'license': 'Apache-2.0', 'n_tokens_mistral': 2689, 'n_tokens_neox': 2219, 'n_words': 387, 'path': 'notebook/Prove/Prove-2/Prove-Chi.ipynb', 'repo_name': 'GiacomoInt/Tesi', 'size': '5932'}