Best Python code snippet using autotest_python
main.py
Source:main.py
1import yaml2import time3import json4import pandas as pd5from sklearn.neural_network import MLPClassifier6from sklearn.neighbors import KNeighborsClassifier7from sklearn.svm import SVC8from sklearn.tree import DecisionTreeClassifier9from sklearn.naive_bayes import GaussianNB10from sklearn.discriminant_analysis import LinearDiscriminantAnalysis11from sklearn.linear_model import LogisticRegression12from src.preprocessor.preprocessor import DataPreProcessor13from src.feature_extractor.feature_extractor import FeatureExtractor14from src.model.model import MLModel15DATA_FOLDER = "./data/"16with open('config.yaml') as f:17 CONFIG_FILE = yaml.load(f, Loader=yaml.FullLoader)18TARGET = CONFIG_FILE["TARGET"]19TARGET_MIN = CONFIG_FILE["TARGET_MIN"]20TARGET_MAX = CONFIG_FILE["TARGET_MAX"]21with open("config/model_parameters.json") as MODEL_PARAM_FILE:22 MODEL_PARAMS = json.load(MODEL_PARAM_FILE)23if __name__ == "__main__":24 print("===== Program Start =====")25 try:26 print("Importing masterdata.")27 master_data = pd.read_csv(f"{DATA_FOLDER}/masterdata.csv")28 except FileNotFoundError:29 print("masterdata file is missing. Please upload the data first.")30 else:31 null_methods = ["interpolate", "drop", "fill_mean", "fill_previous"]32 scaler_methods = ["robust", "max_abs", "min_max"]33 models = [34 MLModel(MLPClassifier(), MODEL_PARAMS["mlp_params"], 5),35 MLModel(GaussianNB(), MODEL_PARAMS["nb_params"], 5),36 MLModel(KNeighborsClassifier(), MODEL_PARAMS["knn_params"], 5),37 MLModel(DecisionTreeClassifier(), MODEL_PARAMS["tree_params"], 5),38 MLModel(SVC(), MODEL_PARAMS["svc_params"], 5),39 MLModel(LinearDiscriminantAnalysis(), MODEL_PARAMS["lda_params"], 5),40 MLModel(LogisticRegression(), MODEL_PARAMS["lr_params"], 5)41 ]42 k_estimates = [2, 3, 5, 8, 10]43 best_score = 044 best_combination = {45 "null_method": None,46 "scale_method": None,47 "k": None,48 "m": None,49 "params": None,50 "features": None51 }52 for null_method in null_methods:53 for scale_method in scaler_methods:54 print(f"Testing out {null_method} replace null method with {scale_method} scaling.")55 preprocess = DataPreProcessor(master_data, TARGET, null_method)56 preprocess.scale_values(scale_method)57 preprocess.categorize_target(TARGET, [TARGET_MIN, 0, TARGET_MAX], [0, 1])58 59 train_X, train_y, test_X, test_y = preprocess.split_data(0.2)60 for k in k_estimates:61 features = FeatureExtractor(train_X, train_y)62 k_features = features.estimate_features(k)63 64 train_X_features = train_X[k_features]65 test_X_features = test_X[k_features]66 for m in models:67 start_time = time.time()68 trained_model = m.calculate_gridsearch(train_X, train_y[TARGET])69 end_time = time.time()70 print(f"Time to train model {str(m.estimator)} with {k} features: {(end_time - start_time)/60:.2f} minutes")71 print(f"Score: {(trained_model.best_score_)*100:.2f}%")72 if trained_model.best_score_ > best_score:73 best_score = trained_model.best_score_74 best_combination["k"] = k75 best_combination["m"] = trained_model.best_estimator_76 best_combination["params"] = trained_model.best_params_77 best_combination["null_method"] = null_method78 best_combination["scale_method"] = scale_method79 best_combination["features"] = k_features80 81 print("\n--- BEST OVERALL RESULTS ---\n")82 print(f"Best Score: {best_score}")83 print(f"""Best Combination:84 \t Null Method - {best_combination["null_method"]}85 \t Scaling Method - {best_combination["scale_method"]}86 \t Number of Features - {best_combination["k"]}87 \t Model - {str(best_combination["m"])}88 \t Model Parameters - {best_combination["params"]}89 """)90 print("\nTraining the full data training set using the combination.")91 preprocess = DataPreProcessor(master_data, TARGET, best_combination["null_method"])92 preprocess.scale_values(best_combination["scale_method"])93 preprocess.categorize_target(TARGET, [TARGET_MIN, 0, TARGET_MAX], [0, 1])94 train_X, train_y, test_X, test_y = preprocess.split_data(0.2)95 96 train_X_features = train_X[best_combination["features"]]97 test_X_features = test_X[best_combination["features"]]98 estimator = best_combination["m"]99 estimator.fit(train_X_features, train_y)100 training_predictions = estimator.predict(train_X_features)101 testing_predictions = estimator.predict(test_X_features)102 training_predictions_data = train_X_features.copy()103 testing_predictions_data = test_X_features.copy()104 training_predictions_data["prediction"] = training_predictions105 testing_predictions_data["prediction"] = testing_predictions106 training_predictions_data["actual_target"] = train_y[TARGET]107 testing_predictions_data["actual_target"] = test_y[TARGET]108 training_predictions_data.to_csv(DATA_FOLDER + TARGET + "_training_predictions.csv", index=False, header=True)109 testing_predictions_data.to_csv(DATA_FOLDER + TARGET + "_testing_predictions.csv", index=False, header=True)110 best_combination["m"] = str(best_combination["m"])111 best_combination["features"] = best_combination["features"].tolist()112 with open(DATA_FOLDER + TARGET + '_best_estimators.json', 'w') as f:...
mchammer_adpt.py
Source:mchammer_adpt.py
1from mc_hammer.null_distributions import pca_trans, random_order, min_max2from mc_hammer.similarity_functions import huberts_gamma, norm_gamma, sillhouette_euclidean,sillhouette_cosine, CH, DB3from mc_hammer.similarity_functions import dunn,S_Dbw,SD_score,IGP,BWC,CVNN, dunn_min4from mc_hammer.hypothesis_test import hypothesis_test5import numpy as np6def k_means(x,k,itt):7 kmeans = KMeans(n_clusters=k,random_state=4,n_init=3,max_iter=).fit(x)8 return kmeans.labels_, kmeans.cluster_centers_9class mchammer_adpt():10 def __init__(self):11 pass12 def get_null_distributions(self,x,null_method,repeats):13 if null_method == 'pca_trans':14 self._null_dists = [pca_trans(x,i) for i in range(repeats)]15 elif null_method == 'random_order':16 self._null_dists = [random_order(x, i) for i in range(repeats)]17 elif null_method == 'min_max':18 self._null_dists = [min_max(x, i) for i in range(repeats)]19 self._null_dists.append(x)20 def get_q_scores(self,cluster_method,q_methods = 'all',k = None, eps = None,min_samples = None):21 if q_methods == 'all':22 q_methods = ['huberts_gamma', 'norm_gamma', 'sillhouette_euclidean','sillhouette_cosine', 'CH', 'DB',23 'dunn','S_Dbw','SD_score','IGP','BWC','CVNN','dunn_min']24 if isinstance(q_methods,str):25 q_methods = [q_methods]26 if cluster_method == 'K_Means':27 labels = [k_means(i,k) for i in self._null_dists]28 if cluster_method == 'DBSCAN':29 labels = [dbscan(i, eps,min_samples) for i in self._null_dists]30 if cluster_method == 'spectral_clustering':31 labels = [spectral_clustering(i, k) for i in self._null_dists]32 self.x_labels = labels[-1]33 # results_dict = {k:hypothesis_test(v,k) for k,v in q_dict.items()}34 q_dict = {}35 for i in q_methods:36 res = []37 if i in ['BWC','dunn','dunn_min']:38 for j in range(len(labels)):39 res_small = eval(i + '(self._null_dists[' + str(j) + '],labels[' + str(j) + '][0],labels[' + str(j) + '][1])')40 res.append(res_small)41 else:42 for j in range(len(labels)):43 res_small = eval(i + '(self._null_dists['+str(j)+'],labels['+str(j)+'][0])')44 res.append(res_small)45 q_dict[i] = res46 results_dict = {k: hypothesis_test(v, k) for k, v in q_dict.items()}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!