May i get help see how am i going, - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: May i get help see how am i going, (/thread-24591.html) |
May i get help see how am i going, - AhmadMWaddah - Feb-21-2020 Hi All, OS: Ubuntu 18.04 Python 3.7 Editors PyCharm and Jupyter Lab I am Talking Course For Machine Learning. i have some code i made its some functions and i try to give it Class decorative, and i have issue in the code for accessing methods from other class. can you help me with a clue to search for ,i trued to search but i didn't get to any of this i had, mostly i got for creating class and access it. First of all i had to change my functions to OOP. i Just Need Clue to search for and go on after what i reached, i don't need to tell me the Solution, Here Is GitHub OOP Package Link: https://github.com/AhmadMWaddah/AMWPkgOOP.git Here is my code: import numpy as np # For Matrices Manipulations import pandas as pd # For Data frames etc import xlrd # For Excel Files import os # For Changing to the Correct Directory # -------------------- # class CharacterFind: def __init__(self, func): self.func = func @property def __call__(self): return self.func() @CharacterFind def character_find(target_string, icon_symbol): """ Custom Function Takes 2 Parameters String and Symbol and Find Where This Symbol and How Many in Target. :param target_string: to loop inside and look for symbol :param icon_symbol: will look for it inside that target :return: count how many times symbol found in the target. """ # Start Counting Variable. count_starter = 0 # List To Append Symbol Each Time Found In String. target_list = [] # Looping In String For Symbol. for character in target_string: if character.lower() == icon_symbol.lower(): # Append Symbol To List To Count It. target_list.append(target_string.find(character, count_starter)) # Add One For Starter To Skip Previous Character. count_starter += 1 # Printing The List With Symbol Positions On String (Target). print(target_list) # -------------------- # class CustomSplit: def __init__(self, split_func): self.split_func = split_func @property def __call__(self): return self.split_func() @CustomSplit def custom_split(target_string): """ Function Take One Parameter a an iterable object and loop and split it for parts and append it to a list. :param target_string: iterable object :return: List with splited part of iterable. """ # List To Append All Splited Words. splited_words_list = [] # Variable To Save Words. helper_variable = '' # Looping Inside String. for each_word in target_string: # checking the next character is space or not. if each_word == ' ': # If Yes. Append Word To List. splited_words_list.append(helper_variable) # Then Reset Temporary Variable helper_variable = '' else: # If Not Then Fill Variable With Word. helper_variable += each_word # Last Word Will Be Out Of Loop Then Go To List Right Away. if helper_variable: splited_words_list.append(helper_variable) print(splited_words_list) # -------------------- # class RenameFiles: def __init__(self, rename_func): self.rename_func = rename_func @property def __call__(self): return self.rename_func() @RenameFiles def rename_files(files_path, wanted_name, needed_extension): """ Function Takes 3 Parameters Files Directory and Name for files we need it to be and extension of the files. :param files_path: Folder that files inside. :param wanted_name: the name we need to rename the files with. :param needed_extension: the new extension for the files we need to rename it. :return: List with the new named Files. """ # List For All Files In Directory. directory_files = [] # New List After Renaming Files. renamed_files = [] # Numerical Range To Give To Files. numbering_list = range(0, 6) # Looping Inside Directory. for filename in os.listdir(files_path): # Adding Each File As Member In Directory Files List. directory_files.append(filename) print(directory_files) # Looping For Two Lists, (Directory Files List and Numerical List). for each_file, each_number in zip(directory_files, numbering_list): # Creating Pattern For The New Name. new_name = f'{wanted_name} {each_number}.{needed_extension}' # Adding New Names To New List (Renamed List). renamed_files.append(new_name) # Printing List With New Names. print(renamed_files) # Statistics Functions. # -------------------- # class MeanSquareError: def __init__(self, MSE_func): self.MSE_func = MSE_func @property def __call__(self): return self.MSE_func() @MeanSquareError # Mean Squared Error. def mean_square_error(actual_data, predicted_data): """ Function Takes 2 Parameters lists of data, making counter and add to it and zipping lists. :param actual_data: :param predicted_data: :return: Mean Squared Error Of Both Data Actual and Predicted. """ # Starter Counter. counter = 0 # Combining BOth List With Zipping Function. data_combined = zip(actual_data, predicted_data) # Loop For Each cell in both data. for (each_actual, each_predicted) in data_combined: # Adding Results To Starter Counter. counter += (each_actual - each_predicted) ** 2 return counter / len(actual_data) # -------------------- # class MeanData: def __init__(self, mean_func): self.mean_func = mean_func @property def __call__(self): return self.mean_func() @MeanData def data_mean(data_values): """ Getting The Mean Number For Data. :param data_values: Set Of Data :return: Mean Of Data Values. """ return sum(data_values) / len(data_values) # -------------------- # class VarianceData: def __init__(self, variance_func): self.variance_func = variance_func @property def __call__(self): return self.variance_func() @VarianceData def data_variance(data_values, mean_of_data): """ Getting The Variance Number For Data. :param data_values: Set Of Data :param mean_of_data: Mean Of Data From Previous Function. :return: Variance Of Data. """ starter_counter = 0 for data_cells in data_values: starter_counter += (data_cells - mean_of_data) ** 2 return starter_counter / len(data_values) # -------------------- # class CoVarianceData: def __init__(self, covariance_func): self.covariance_func = covariance_func @property def __call__(self): return self.covariance_func() @CoVarianceData def data_covariance(features_values, labels_values, features_mean, labels_mean): """ It measures the degree of change in the variables, i.e. when one variable changes, will there be the same/a similar change in the other variable. :param features_values: (X) Weights and Features Of Data :param labels_values: (Y) Predictions and Labels Of Data :param features_mean: Measured Mean Of Features For Data :param labels_mean: Measured Mean Of Labels For Data :return: Measure of relationship between 2 variables. """ # Labels and Features Added and Multiplied. features_labels_sum_multiply = 0 # Combine Both Features and Labels In One Object. feature_label_combined = zip(features_values, labels_values) for (features_values, labels_values) in feature_label_combined: features_labels_sum_multiply += ((features_values - features_mean) * (labels_values - labels_mean)) return features_labels_sum_multiply / len(features_values) # -------------------- # class CoEfficientData: def __init__(self, coefficient_func): self.coefficient_func = coefficient_func @property def __call__(self): return self.coefficient_func() @CoEfficientData def data_coefficient(features_values, labels_values): """ Measure of correlation overcomes the scale dependency of covariance by standardizing the measures. :param features_values: (X) Weights and Features Of Data :param labels_values: (Y) Predictions and Labels Of Data :return: Number Between 0 and 1. """ features_mean = data_mean(features_values) labels_values = data_mean(labels_values) features_variance = data_variance(features_values, features_mean) labels_variance = data_variance(labels_values, labels_values) print(features_variance) beta_one = data_covariance(features_values, labels_values, features_mean, labels_values) / features_variance beta_zero = labels_values - (beta_one * features_mean) return beta_one, beta_zero # -------------------- # class SimpleLinearRegression: def __init__(self, spl_func): self.spl_func = spl_func @property def __call__(self): return self.spl_func() @SimpleLinearRegression def simple_linear_regression(features_train, labels_train, features_test): """ Using a linear regression model will allow you to discover whether a relationship between variables exists at all. :param features_train: :param labels_train: :param features_test: :return: """ beta_one_train, beta_zero_train = data_coefficient(features_train, labels_train) predicted_data = beta_one_train * features_test + beta_zero_train return predicted_data # -------------------- # class EvaluateModel: def __init__(self, eval_func): self.eval_func = eval_func @property def __call__(self): return self.eval_func() @EvaluateModel def evaluate_model(features_train, labels_train, features_test, labels_test): """ Evaluate Model anc Compare Train Data With Test Data. :param features_train: :param labels_train: :param features_test: :param labels_test: :return: """ prediction_data = simple_linear_regression(features_train, labels_train, features_test) return mean_square_error(prediction_data, labels_test) # -------------------- # class Transformer: # Attributes def __init__(self, standarad_scaler, min_Max_Scaler): self.standarad_scaler = standarad_scaler self.min_Max_Scaler = min_Max_Scaler @property def __call__(self): self.min_Max_Scaler() self.standarad_scaler() @Transformer def standaradscaler(feature): Mean_feature = mean(feature) std_feature = std(feature) scaled_feature = (feature - Mean_feature) / std_feature return scaled_feature @Transformer def minMaxScaler(feature): min_max_scaler = (feature - (np.min(feature))) / ((np.max(feature)) - (np.min(feature))) return min_max_scalerThank You For Support All. RE: May i get help see how am i going, - jefsummers - Feb-21-2020 You have defined a lot of classes but never instantiate an object based on a class. Also, help me out, why are you using decorators on all those functions that are defined outside of the classes? RE: May i get help see how am i going, - AhmadMWaddah - Feb-22-2020 (Feb-21-2020, 07:04 PM)jefsummers Wrote: You have defined a lot of classes but never instantiate an object based on a class. Yes, i suppose to make instances objects whaen i am useing the classes after import it in a project. i will say Example. import AMW # Functions PAckage File. splits = CustomSplit.custom_split() Quote:Also, help me out, why are you using decorators on all those functions that are defined outside of the classes? I am using Decorator for each function to link it to a different Class. is that what you mean pal or Please can you explain question more. thank you so much for support. RE: May i get help see how am i going, - jefsummers - Feb-22-2020 Rather than decorators, why not just indent to define the function as part of the class, like you do for the __init__ functions? Would make your code more readable and easier to maintain... RE: May i get help see how am i going, - AhmadMWaddah - Feb-23-2020 when i try this i could not access class method from other class. example : i need the method Mean() to use it in class of Variance, could not do it, i got the methods undefined. Class Data_variance: def data_variance(data_values, mean_of_data): """ Getting The Variance Number For Data. :param data_values: Set Of Data :param mean_of_data: Mean Of Data From Previous Function. :return: Variance Of Data. """ starter_counter = 0 for data_cells in data_values: starter_counter += (data_cells - mean_of_data) ** 2 return starter_counter / len(data_values) RE: May i get help see how am i going, - jefsummers - Feb-23-2020 Basically: class Test_it_out: def test_it(self, a, b): return a*b class Test_it_in : def foobar(self, c, d) : life = Test_it_out() return life.test_it(c,d) alpha = Test_it_in() print(alpha.foobar(5,6))1. Class is not capitalized. 2. Functions defined within a class should have the first argument as self. 3. Instantiate the class before you call a related function. Note the syntax. K? RE: May i get help see how am i going, - AhmadMWaddah - Feb-24-2020 This is amazing i will try to change my code following this concepts my friend thanks again for support,. RE: May i get help see how am i going, - AhmadMWaddah - Feb-24-2020 I Did It and Works So Fine My Friend Thank You For Support Buddy. here is The Code To Check With Me If You Would like To. import os # --------------------------------------------------- # class CharacterFind: def __init__(self, target_string, icon_symbol): """ :param target_string: Provided String To Search Inside. :param icon_symbol: Character Search For Inside String :return: ----------> How Many Times Repeated The Character and Indexes Too. """ self.target_string = target_string self.icon_symbol = icon_symbol def character_find(self): # Start Counting Variable. count_starter = 0 # List To Append Symbol Each Time Found In String. target_list = [] # Looping In String For Symbol. for character in self.target_string: if character.lower() == self.icon_symbol.lower(): # Append Symbol To List To Count It. target_list.append(self.target_string.find(character, count_starter)) # Add One For Starter To Skip Previous Character. count_starter += 1 # Printing The List With Symbol Positions On String (Target). repeated_times = len(target_list) print(target_list, repeated_times) return target_list, repeated_times class CustomSplit: def __init__(self, provided_string): """ :param provided_string: String To Loop Inside & Split Words. :return: -----------> Splited Words In a List. """ self.provided_string = provided_string def custom_split(self): # List To Append All Splited Words. splited_words_list = [] # Variable To Save Words. helper_variable = '' # Looping Inside String. for each_word in self.provided_string: # checking the next character is space or not. if each_word == ' ': # If Yes. Append Word To List. splited_words_list.append(helper_variable) # Then Reset Temporary Variable helper_variable = '' else: # If Not Then Fill Variable With Word. helper_variable += each_word # Last Word Will Be Out Of Loop Then Go To List Right Away. if helper_variable: splited_words_list.append(helper_variable) print(splited_words_list) return splited_words_list class RenameFiles: def __init__(self, files_path, wanted_name, count_of_files, needed_extension): """ :param files_path: Directory To Rename Files Inside :param wanted_name: New Name To Rename Into. :param count_of_files: How Many File To Be Renamed :param needed_extension: Directory, Also, Wanted Name, & Extension. """ self.files_path = files_path self.wanted_name = wanted_name self.needed_extension = needed_extension self.count_of_files = count_of_files def rename_files(self): # List For All Files In Directory. directory_files = [] # New List After Renaming Files. renamed_files = [] # Numerical Range To Give To Files. numbering_list = range(1, self.count_of_files + 1) # Looping Inside Directory. for filename in os.listdir(self.files_path): # Adding Each File As Member In Directory Files List. directory_files.append(filename) print(directory_files) # Looping For Two Lists, (Directory Files List and Numerical List). for each_file, each_number in zip(directory_files, numbering_list): # Creating Pattern For The New Name. new_name = f'{self.wanted_name} ({each_number}).{self.needed_extension}' # Adding New Names To New List (Renamed List). renamed_files.append(new_name) # Printing List With New Names. print(renamed_files) return renamed_files test_string = 'here is my beautiful life to be engineer' find_character = CharacterFind(test_string, 'e') find_character.character_find() print('--------------------------------------') split_mine = CustomSplit(test_string) split_mine.custom_split() print('--------------------------------------') file_renaming_now = RenameFiles('/home/ahmdwd/Documents/Prjcts/AMWMLPkg/tst', 'New My Name File', 30, 'TIF') renamed_files_To_split = file_renaming_now.rename_files() RE: May i get help see how am i going, - jefsummers - Feb-24-2020 Works, and looks much easier to maintain RE: May i get help see how am i going, - AhmadMWaddah - Feb-28-2020 Thanks My Friend For Support. |