markdown
stringlengths
0
37k
code
stringlengths
1
33.3k
path
stringlengths
8
215
repo_name
stringlengths
6
77
license
stringclasses
15 values
4.2. Selected Population 4.2.1. Remove Excluded Population, Remove Unused Features <i>Nothing to do!<i/> <br/> <font style="font-weight:bold;color:red">Notes: </font> - Ideally the features must be configured before generating the CSV feature file, as it is very inefficient to derive new features at this stage - This...
# Exclusion of unused features # excluded = [name for name in features_input.columns if name not in features_names_group.keys()] # features_input = features_input.drop(excluded, axis=1) # print("Number of columns: ", len(features_input.columns), "; Total records: ", len(features_input.index))
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<br/><br/> 5. Set Samples &amp; Target Features 5.1. Set Features 5.1.1. Train & Test Samples Set the samples
frac_train = 0.50 replace = False random_state = 100 nrows = len(features_input.index) features = {"train": dict(), "test": dict()} features["train"] = features_input.sample(frac=frac_train, replace=False, random_state=100) features["test"] = features_input.drop(features["train"].index) features["train"] = features["...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:red">Clean-Up</font>
features_input = None gc.collect()
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
5.1.2. Independent & Target variable¶ Set independent, target &amp; ID features
target_labels = list(features_types_group["TARGET"]) target_id = ["patientID"] features["train_indep"] = dict() features["train_target"] = dict() features["train_id"] = dict() features["test_indep"] = dict() features["test_target"] = dict() features["test_id"] = dict() # Independent and target features def set_featur...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
Verify features visually
display(pd.concat([features["train_id"].head(), features["train_target"].head(), features["train_indep"].head()], axis=1)) display(pd.concat([features["test_id"].head(), features["test_target"].head(), features["test_indep"].head()], axis=1))
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:red">Clean-Up</font>
del features["train"] del features["test"] gc.collect()
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
5.5. Save Samples Serialise &amp; save the samples before any feature transformation. <br/>This snapshot of the samples may be used for the population profiling
file_name = "Step_05_Features" readers_writers.save_serialised_compressed(path=CONSTANTS.io_path, title=file_name, objects=features) # print print("Number of columns: ", len(features["train_indep"].columns), "features: {train: ", len(features["train_indep"]), ", test: ", len(features["test_indep"]), "}")
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
5.2. Remove - Near Zero Variance In order to reduce sparseness and invalid features, highly stationary ones were withdrawn. The features that had constant counts less than or equal a threshold were ltered out, to exclude highly constants and near-zero variances. The near zero variance rules are presented in below: - F...
thresh_unique_cut = 100 thresh_freq_cut = 1000 excludes = [] file_name = "Step_05_Preprocess_NZV_config" features["train_indep"], o_summaries = preprocess.near_zero_var_df(df=features["train_indep"], excludes=excludes, ...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
5.3. Remove Highly Linearly Correlated In this step, features that were highly linearly correlated were excluded. <font style="font-weight:bold;color:red">Configure:</font> the function - A numeric value for the pair-wise absolute correlation cutoff. e.g. 0.95 <br/>&#9; &#8594; thresh_corr_cut
thresh_corr_cut = 0.95 excludes = list(features_types_group["CATEGORICAL"]) file_name = "Step_05_Preprocess_Corr_config" features["train_indep"], o_summaries = preprocess.high_linear_correlation_df(df=features["train_indep"], excludes=excludes, ...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
5.4. Descriptive Statistics Produce a descriptive stat report of 'Categorical', 'Continuous', & 'TARGET' features
# columns file_name = "Step_05_Data_ColumnNames_Train" readers_writers.save_csv(path=CONSTANTS.io_path, title=file_name, data=list(features["train_indep"].columns.values), append=False) # Sample - Train file_name = "Step_05_Stats_Categorical_Train" o_stats = preprocess.stats_discrete_df(df=fe...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<br/><br/> 6. Recategorise &amp; Transform Verify features visually
display(pd.concat([features["train_id"].head(), features["train_target"].head(), features["train_indep"].head()], axis=1)) display(pd.concat([features["test_id"].head(), features["test_target"].head(), features["test_indep"].head()], axis=1))
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
6.1. Recategorise Define the factorisation function to generate dummy features for the categorical features.
def factorise_settings(max_categories_frac, min_categories_num, exclude_zero): categories_dic = dict() labels_dic = dict() dtypes_dic = dict() dummies = [] for f_name in features_types_group["CATEGORICAL"]: if f_name in features["train_indep"]: # find top & valid states ...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
Select categories: by order of freq., max_categories_frac, & max_categories_num <br/><font style="font-weight:bold;color:red">Configure:</font> The input arguments are: - Specify the maximum number of categories a feature can have <br/>&#9; &#8594; max_categories_frac - Specify the minimum number of categories a featur...
max_categories_frac = 0.90 min_categories_num = 1 exclude_zero = False # if possible remove state zero categories_dic, labels_dic, dtypes_dic, features_types_group["DUMMIES"] = \ factorise_settings(max_categories_frac, min_categories_num, exclude_zero)
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
Manually add dummy variables to the dataframe &amp; remove the original Categorical variables
features["train_indep_temp"] = preprocess.factoring_feature_wise(features["train_indep"], categories_dic, labels_dic, dtypes_dic, threaded=False) features["test_indep_temp"] = preprocess.factoring_feature_wise(features["test_indep"], categories_dic, labels_dic, dtypes_dic, threaded=False) # print print("Number of colu...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
Verify features visually
display(pd.concat([features["train_id"].head(), features["train_target"].head(), features["train_indep_temp"].head()], axis=1)) display(pd.concat([features["test_id"].head(), features["test_target"].head(), features["test_indep_temp"].head()], axis=1))
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
Set
features["train_indep"] = features["train_indep_temp"].copy(True) features["test_indep"] = features["test_indep_temp"].copy(True)
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:red">Clean-Up</font>
del features["train_indep_temp"] del features["test_indep_temp"] gc.collect()
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
6.2. Remove - Near Zero Variance Optional: Remove more features with near zero variance, after the factorisation step. <font style="font-weight:bold;color:red">Configure:</font> the function
# the cutoff for the percentage of distinct values out of the number of total samples (upper limit). e.g. 10 * 100 / 100 thresh_unique_cut = 100 # the cutoff for the ratio of the most common value to the second most common value (lower limit). eg. 95/5 thresh_freq_cut = 1000 excludes = [] file_name = "Step_06_Preproce...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
6.3. Remove Highly Linearly Correlated Optional: Remove more features with highly linearly correlated, after the factorisation step. <font style="font-weight:bold;color:red">Configure:</font> the function
# A numeric value for the pair-wise absolute correlation cutoff. e.g. 0.95 thresh_corr_cut = 0.95 excludes = [] file_name = "Step_06_Preprocess_Corr_config" features["train_indep"], o_summaries = preprocess.high_linear_correlation_df(df=features["train_indep"], ...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
6.4. Descriptive Statsistics Produce a descriptive stat report of 'Categorical', 'Continuous', & 'TARGET' features
# columns file_name = "Step_06_4_Data_ColumnNames_Train" readers_writers.save_csv(path=CONSTANTS.io_path, title=file_name, data=list(features["train_indep"].columns.values), append=False) # Sample - Train file_name = "Step_06_4_Stats_Categorical_Train" o_stats = preprocess.stats_discrete_df(d...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
6.5. Transformations Verify features visually
display(pd.concat([features["train_id"].head(), features["train_target"].head(), features["train_indep"].head()], axis=1)) display(pd.concat([features["test_id"].head(), features["test_target"].head(), features["test_indep"].head()], axis=1))
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:blue">Tranformation:</font> scale <font style="font-weight:bold;color:brown">Note:</font>: It is highly resource intensive
transform_type = "scale" kwargs = {"with_mean": True} method_args = dict() excludes = list(features_types_group["CATEGORICAL"]) + list(features_types_group["DUMMIES"]) features["train_indep"], method_args = preprocess.transform_df(df=features["train_indep"], excludes=excludes, ...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:blue">Tranformation:</font> Yeo-Johnson <font style="font-weight:bold;color:brown">Note:</font>: It is highly resource intensive
transform_type = "yeo_johnson" kwargs = {"lmbda": -0.5, "derivative": 0, "epsilon": np.finfo(np.float).eps, "inverse": False} method_args = dict() excludes = list(features_types_group["CATEGORICAL"]) + list(features_types_group["DUMMIES"]) features["train_indep"], method_args = preprocess.transform_df(df=features["tra...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
6.6. Summary Statistics Produce a descriptive stat report of 'Categorical', 'Continuous', & 'TARGET' features
# Statsistics report for 'Categorical', 'Continuous', & 'TARGET' variables # columns file_name = "Step_06_6_Data_ColumnNames_Train" readers_writers.save_csv(path=CONSTANTS.io_path, title=file_name, data=list(features["train_indep"].columns.values), append=False) # Sample - Train file_name = "...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<br/><br/> 7. Rank &amp; Select Features <font style="font-weight:bold;color:red">Configure:</font> the general settings
# select the target variable target_feature = "label365" # "label30", "label365" # number of trials num_trials = 1 model_rank = dict() o_summaries_df = dict()
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
7.1. Define <font style="font-weight:bold;color:blue">Ranking Method:</font> Random forest classifier (Brieman) <br/>Define a set of classifiers with different settings, to be used in feature ranking trials.
def rank_random_forest_brieman(features_indep_arg, features_target_arg, num_trials): num_settings = 3 o_summaries_df = [pd.DataFrame({'Name': list(features_indep_arg.columns.values)}) for _ in range(num_trials * num_settings)] model_rank = [None] * (num_trials * num_settings) # trials for i in ran...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:blue">Ranking Method:</font> Gradient Boosted Regression Trees (GBRT) <br/>Define a set of classifiers with different settings, to be used in feature ranking trials.
def rank_gbrt(features_indep_arg, features_target_arg, num_trials): num_settings = 3 o_summaries_df = [pd.DataFrame({'Name': list(features_indep_arg.columns.values)}) for _ in range(num_trials * num_settings)] model_rank = [None] * (num_trials * num_settings) # trials for i in range(num_trials): ...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:blue">Ranking Method:</font> Randomized Logistic Regression <br/>Define a set of classifiers with different settings, to be used in feature ranking trials.
def rank_randLogit(features_indep_arg, features_target_arg, num_trials): num_settings = 3 o_summaries_df = [pd.DataFrame({'Name': list(features_indep_arg.columns.values)}) for _ in range(num_trials * num_settings)] model_rank = [None] * (num_trials * num_settings) # trials for i in range(num_trial...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
7.2. Run Run one or more feature ranking methods and trials <font style="font-weight:bold;color:blue">Ranking Method:</font> Random forest classifier (Brieman) <font style="font-weight:bold;color:brown">Note:</font>: It is moderately resource intensive
rank_model = "rfc" model_rank[rank_model] = dict() o_summaries_df[rank_model] = dict() model_rank[rank_model], o_summaries_df[rank_model] = rank_random_forest_brieman( features["train_indep"], features["train_target"][target_feature], num_trials)
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:blue">Ranking Method:</font> Gradient Boosted Regression Trees (GBRT) <font style="font-weight:bold;color:brown">Note:</font>: It is moderately resource intensive
rank_model = "gbrt" model_rank[rank_model] = dict() o_summaries_df[rank_model] = dict() model_rank[rank_model], o_summaries_df[rank_model] = rank_gbrt( features["train_indep"], features["train_target"][target_feature], num_trials)
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:blue">Ranking Method</font>: Randomized Logistic Regression <font style="font-weight:bold;color:brown">Note:</font>: It is moderately resource intensive
rank_model = "randLogit" model_rank[rank_model] = dict() o_summaries_df[rank_model] = dict() model_rank[rank_model], o_summaries_df[rank_model] = rank_randLogit( features["train_indep"], features["train_target"][target_feature], num_trials)
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
7.3. Summaries
# combine scores def rank_summarise (features_arg, o_summaries_df_arg): summaries_temp = {'Order_avg': [], 'Order_max': [], 'Order_min': [], 'Importance_avg': []} summary_order = [] summary_importance = [] for f_name in list(features_arg.columns.values): for i in range(len(o_summaries_df_a...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
Save
for rank_model in model_rank.keys(): file_name = "Step_07_Model_Train_model_rank_" + rank_model readers_writers.save_serialised_compressed(path=CONSTANTS.io_path, title=file_name, objects=model_rank[rank_model]) file_name = "Step_07_Model_Train_model_rank_summaries_" + rank_model readers_writers.sa...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
7.4. Select Top Features <font style="font-weight:bold;color:red">Configure:</font> the selection method
rank_model = "rfc" file_name = "Step_07_Top_Features_" + rank_model rank_top_features_max = 400 rank_top_features_score_min = 0.1 * (10 ^ -20) # sort features features_names_selected = summaries_df[rank_model]['Name'][summaries_df[rank_model]['Order_avg'] >= rank_top_features_score_min] features_names_selected = (feat...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
Save
# save to CSV readers_writers.save_csv(path=CONSTANTS.io_path, title=file_name, data=features_names_selected, append=False, header=False) # print print("Number of columns: ", len(features["train_indep"].columns)) print("features: {train: ", len(features["train_indep"]), ", test: ", len(features["test_indep"]), "...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:red">Configure</font>: the selected feature manually if it isnecessary!
file_name = "Step_07_Top_Features_rfc_adhoc" features_names_selected = readers_writers.load_csv(path=CONSTANTS.io_path, title=file_name, dataframing=False)[0] features_names_selected = [f.replace("\n", "") for f in features_names_selected] display(pd.DataFrame(features_names_selected))
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
Verify the top features visually
# print print("Number of columns: ", len(features["train_indep"].columns), ";\nNumber of top columns: ", len(features["train_indep"][features_names_selected].columns)) print("features: {train: ", len(features["train_indep"][features_names_selected]), ", test: ", len(features["test_indep"][features_names_sele...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
7.5. Summary Statistics Produce a descriptive stat report of 'Categorical', 'Continuous', & 'TARGET' features
# columns file_name = "Step_07_Data_ColumnNames_Train" readers_writers.save_csv(path=CONSTANTS.io_path, title=file_name, data=list(features["train_indep"][features_names_selected].columns.values), append=False) # Sample - Train file_name = "Step_07_Stats_Categorical_Train" o_stats = preproces...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
7.6. Save Features
file_name = "Step_07_Features" readers_writers.save_serialised_compressed(path=CONSTANTS.io_path, title=file_name, objects=features) # print print("File size: ", os.stat(os.path.join(CONSTANTS.io_path, file_name + ".bz2")).st_size) print("Number of columns: ", len(features["train_indep"].columns)) print("feature...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<br/><br/> <br/><br/> 8. Model <font style="font-weight:bold;color:orange">Load a Saved Samples and Features Ranking:</font> <br/> It is an optional step. The step loads the serialised & compressed outputs of Step-7.
# open fetures file_name = "Step_07_Features" features = readers_writers.load_serialised_compressed(path=CONSTANTS.io_path, title=file_name) # print print("File size: ", os.stat(os.path.join(CONSTANTS.io_path, file_name + ".bz2")).st_size) print("Number of columns: ", len(features["train_indep"].columns)) print(...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<br/><br/> 8.1. Initialise 8.1.1. Algorithms <font style="font-weight:bold;color:red">Configure:</font> the trianing algorithm <font style="font-weight:bold;color:brown">Algorithm 1</font>: Random Forest
method_name = "rfc" kwargs = {"n_estimators": 20, "criterion": 'gini', "max_depth": None, "min_samples_split": 100, "min_samples_leaf": 50, "min_weight_fraction_leaf": 0.0, "max_features": 'auto', "max_leaf_nodes": None, "bootstrap": True, "oob_score": False, "n_jobs": -1, "random_state": None, "verbose": 0...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:brown">Algorithm 2</font>: Logistic Regression
method_name = "lr" kwargs = {"penalty": 'l1', "dual": False, "tol": 0.0001, "C": 1, "fit_intercept": True, "intercept_scaling": 1, "class_weight": None, "random_state": None, "solver": 'liblinear', "max_iter": 100, "multi_class": 'ovr', "verbose": 0, "warm_start": False, "n_jobs": -1}
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:brown">Algorithm 3</font>: Logistic Cross-Validation
method_name = "lr_cv" kwargs = {"Cs": 10, "fit_intercept": True, "cv": None, "dual": False, "penalty": 'l2', "scoring": None, "solver": 'lbfgs', "tol": 0.0001, "max_iter": 10, "class_weight": None, "n_jobs": -1, "verbose": 0, "refit": True, "intercept_scaling": 1.0, "multi_class": "ovr", "random_s...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:brown">Algorithm 4</font>: Neural Network
method_name = "nn" kwargs = {"solver": 'lbfgs', "alpha": 1e-5, "hidden_layer_sizes": (5, 2), "random_state": 1}
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:brown">Algorithm 5</font>: k-Nearest Neighbourhood
method_name = "knc" kwargs = {"n_neighbors": 5, "weights": 'distance', "algorithm": 'auto', "leaf_size": 30, "p": 2, "metric": 'minkowski', "metric_params": None, "n_jobs": -1}
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:brown">Algorithm 6</font>: Decision Tree
method_name = "dtc" kwargs = {"criterion": 'gini', "splitter": 'best', "max_depth": None, "min_samples_split": 30, "min_samples_leaf": 30, "min_weight_fraction_leaf": 0.0, "max_features": None, "random_state": None, "max_leaf_nodes": None, "class_weight": None, "presort": False}
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:brown">Algorithm 7</font>: Gradient Boosting Classifier
method_name = "gbc" kwargs = {"loss": 'deviance', "learning_rate": 0.1, "n_estimators": 100, "subsample": 1.0, "min_samples_split": 30, "min_samples_leaf": 30, "min_weight_fraction_leaf": 0.0, "max_depth": 3, "init": None, "random_state": None, "max_features": None, "verbose": 0, "max_leaf_nodes": None,...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<font style="font-weight:bold;color:brown">Algorithm 8</font>: Naive Bayes<br/> Note: features must be positive
method_name = "nb" training_method = TrainingMethod(method_name) kwargs = {"alpha": 1.0, "fit_prior": True, "class_prior": None}
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
<br/><br/> 8.1.2. Other Settings <font style="font-weight:bold;color:red">Configure:</font> other modelling settings
# select the target variable target_feature = "label365" # "label30" , "label365" # file name file_name = "Step_09_Model_" + method_name + "_" + target_feature # initialise training_method = TrainingMethod(method_name)
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
8.1.3. Features
sample_train = features["train_indep"][features_names_selected] # features["train_indep"][features_names_selected], features["train_indep"] sample_train_target = features["train_target"][target_feature] # features["train_target"][target_feature] sample_test = features["test_indep"][features_names_selected] # features["...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
8.3. Fit Fit the model, using the train sample
o_summaries = dict() # Fit model = training_method.train(sample_train, sample_train_target, **kwargs) training_method.save_model(path=CONSTANTS.io_path, title=file_name) # load model # training_method.load(path=CONSTANTS.io_path, title=file_name) # short summary o_summaries = training_method.train_summaries()
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
Predict & report performance, using the train sample
o_summaries = dict() # predict model = training_method.predict(sample_train, "train") # short summary o_summaries = training_method.predict_summaries(pd.Series(sample_train_target), "train") # Print the main performance statistics for k in o_summaries.keys(): print(k, o_summaries[k]) # Print the by risk-bands o...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
8.4. Predict Predict & report performance, using the test sample
o_summaries = dict() # predict model = training_method.predict(sample_test, "test") # short summary o_summaries = training_method.predict_summaries(pd.Series(sample_test_target), "test") # Print the main performance statistics for k in o_summaries.keys(): print(k, o_summaries[k]) # Print the by risk-bands of a ...
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
8.5. Cross-Validation Perform k-fold cross-validation
o_summaries = dict() score = training_method.cross_validate(sample_test, sample_test_target, scoring="neg_mean_squared_error", cv=10) # short summary o_summaries = training_method.cross_validate_summaries() print("Scores: ", o_summaries)
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
8.6. Save Save the training model.
training_method.save_model(path=CONSTANTS.io_path, title=file_name)
TCARER_Basic.ipynb
mesgarpour/T-CARER
apache-2.0
Calcolo matrice adiacenza Calcolo il raggio medio che definisce Roma entro il raccordo anulare NB: da verificare che distanza euclidea non crei troppi problemi
colosseo = (41.890173, 12.492331) raccordo = [(41.914456, 12.615807),(41.990672, 12.502714),(41.793883, 12.511297),(41.812566, 12.396628),(41.956277, 12.384611)] raggi = [] def geodesicDistance(A, B=colosseo): return geopy.distance.vincenty(A, B).meters raggioTerra = 6372795 def euclidDistance(A, B=colosseo): ...
src/Adiacenza, grafo e grado.ipynb
FedericoMuciaccia/SistemiComplessi
mit
Popolo il dataframe e faccio una prima grossa scrematura
dataframe = pandas.read_csv("/home/protoss/Documenti/Siscomp_datas/data/cell_towers.csv") #dataframe = pandas.read_csv("/home/protoss/Documenti/SistemiComplessi/data/cell_towers_diff-2016012100.csv") #dataframe criterioMCC = dataframe.mcc == 222 criterioMinsamples = dataframe.samples > 1 italydoitcleaner = dataframe[cr...
src/Adiacenza, grafo e grado.ipynb
FedericoMuciaccia/SistemiComplessi
mit
Seleziono le antenne in Roma e faccio dei .csv appositi
#istruzione che fa selezione alcune righe con criteri su alcune colonne, #ne seleziona alcune e restituisce un array nompy di valori desiderati coordinate = dataframe[criterioMCC & criterioMinsamples][['lat', 'lon']].values %time distanza = numpy.array(map(geodesicDistance, coordinate), dtype = int) raggiomedioGeo =...
src/Adiacenza, grafo e grado.ipynb
FedericoMuciaccia/SistemiComplessi
mit
Prendo le antenne di Roma e faccio matrice adiacenza
#definisco la funzione che mi calcola la matrice di adiacenza def matriceSupEuclid(datiCoordinate, datiRaggi): a = numpy.zeros((numdati,numdati), dtype=int) for i in xrange(numdati): for j in xrange(numdati-i-1): sommaraggi = datiRaggi[i] + datiRaggi[j+i+1] #è equivalente a un if...
src/Adiacenza, grafo e grado.ipynb
FedericoMuciaccia/SistemiComplessi
mit
Faccio grafo e calcolo distr grado
#for azienda in gestore: #italydoitcleaner['distanze'] = distanza #romaCell.to_csv("../data/Roma_towers.csv") adiacenzaRoma = numpy.genfromtxt("/home/protoss/Documenti/Siscomp_datas/data/AdiacenzaEuclidea_Roma.csv",delimiter=',',dtype='int') adiacenzaTim = numpy.genfromtxt("/home/protoss/Documenti/Siscomp_datas/data/A...
src/Adiacenza, grafo e grado.ipynb
FedericoMuciaccia/SistemiComplessi
mit
Topologia iniziale con networkx (molto lento)
gestore = ["Tim", "Vodafone", "Wind", "Tre"] def topologyNetx(gestore): adiacenza = numpy.genfromtxt("/home/protoss/Documenti/Siscomp_datas/data/AdiacenzaEuclidea_{0}.csv".format(gestore),delimiter=',',dtype='int') grafo = networkx.Graph(adiacenza) c = networkx.average_clustering(grafo) d = networkx.d...
src/Adiacenza, grafo e grado.ipynb
FedericoMuciaccia/SistemiComplessi
mit
NB. num antenne * TIM - 1756 * Vodafone - 1771 * Wind - 2365 * 3 - 1395 Tot antenne: 6571 TODO: Prendere array coordinate ✔ fare array distanze ✔ mettere colonna distanze in dataframe ✔ selezionare righe con variabile compresa entro raggiomedio ✔ fare un nuovo dataframe ✔ escludere tu...
colori = ['#4d4d4d', '#004184','#ff3300','#ff8000','#018ECC'] paletta = seaborn.color_palette(palette = colori) seaborn.palplot(paletta) paletta = seaborn.color_palette(palette = 'muted') seaborn.palplot(paletta) paletta = seaborn.color_palette(palette = 'bright') seaborn.palplot(paletta) paletta = seaborn.color_palett...
src/Adiacenza, grafo e grado.ipynb
FedericoMuciaccia/SistemiComplessi
mit
Kruk-Jaroniec-Sayari $\displaystyle{t_\mathrm{i} = \left(\frac{C_\mathrm{1}}{C_\mathrm{2}-\log{(P_\mathrm{rel,\ i})}}\right)^{C_\mathrm{3}}}$ where typically $C_\mathrm{1} = 60.6500$, $C_\mathrm{2} = 0.03071$, and $C_\mathrm{3} = 0.3968$ M. Kruk, M. Jaroniec, and A. Sayari. "Application of Large Pore MCM-41 Molecular S...
plots.plotThickness( Prel, t.KrukJaroniecSayari()(Prel), 'Kruk-Jaroniec-Sayari' )
documentation/thickness.ipynb
lowks/micromeritics
gpl-3.0
Halsey The Halsey equation assumes the adsorbed liquid monolayer has the same density and packing as the normal liquid. The values 3.54 and 5.00 are empirical and are user adjustable. $\displaystyle{t_\mathrm{i} = C_\mathrm{1}\left(\frac{C_\mathrm{2}}{\ln{(P_\mathrm{rel,\ i})}}\right)^{C_\mathrm{3}}}$ where typically $...
plots.plotThickness( Prel, t.Halsey()(Prel), 'Halsey')
documentation/thickness.ipynb
lowks/micromeritics
gpl-3.0
Harkins and Jura The work of Harkins and Jura has shown that a plot of $\log{(P_\mathrm{rel})} = B - A/V_\mathrm{a}^\mathrm{2}$ returns a linear region where the film is condensed (where B is the intercept and A is the slope of the linear region). The relative pressure can be related to the statistical thickness $(t_\...
plots.plotThickness( Prel, t.HarkinsJura()(Prel), 'Harkins and Jura')
documentation/thickness.ipynb
lowks/micromeritics
gpl-3.0
Broekhoff-de Boer $\displaystyle{\log{(P_\mathrm{rel,\ i})} = \frac{C_\mathrm{1}}{t_\mathrm{i}^{2}}+C_\mathrm{2}e^{C_\mathrm{3}t_\mathrm{i}}}$ where typically $C_\mathrm{1} = -16.1100$, $C_\mathrm{2} = 0.1682$, and $C_\mathrm{3} = -0.1137$ Broekhoff, J.C.P. and de Boer, J.H., "The Surface Area in Intermediate Pores," P...
plots.plotThickness( Prel, t.BroekhoffDeBoer()(Prel), 'Broekhoff-de Boer')
documentation/thickness.ipynb
lowks/micromeritics
gpl-3.0
Carbon Black STSA $\displaystyle{t_\mathrm{i} = C_\mathrm{1}(P_\mathrm{rel,\ i})^2+C_\mathrm{2}(P_\mathrm{rel,\ i})+C_\mathrm{3}}$ where typically $C_\mathrm{1} = 2.9800$, $C_\mathrm{2} = 6.4500$, and $C_\mathrm{3} = 0.8800$
plots.plotThickness( Prel, t.CarbonBlackSTSA()(Prel), 'Carbon Black STSA')
documentation/thickness.ipynb
lowks/micromeritics
gpl-3.0
We'll use some of the data that comes pre-packaged with statsmodels to demonstrate the library functionality. The data set below comprises incomplete, daily measurements of CO2 levels in Hawaii. Note: at the time of this writing, the current release of statsmodels includes a utility method for loading these datasets as...
def get_statsmodels_df(): """Return packaged data in a pandas.DataFrame""" # some hijinks to get around outdated statsmodels code dataset = sm.datasets.co2.load() start = dataset.data['date'][0].decode('utf-8') index = pd.date_range(start=start, periods=len(dataset.data), freq='W-SAT') obs = pd....
STL-usage-example.ipynb
jrmontag/STLDecompose
mit
Because it's based on some existing statsmodels functionality, STLDecompose requires two things of the input dataframe: 1. continuous observations (no missing data points) 2. a pandas DateTimeIndex Since these are both very situation-dependent, we leave it to the user to define how they want to acheive these goals - pa...
obs = (obs .resample('D') .mean() .interpolate('linear')) obs.head(10) obs.index obs.head(1000).plot()
STL-usage-example.ipynb
jrmontag/STLDecompose
mit
Decompose One of the primary pieces of functionality is the STL decomposition. The associated method requires the observation frame, and the primary (largest) period of seasonality. This period is specified in terms of index positions, and so care is needed for the user to correctly specify the periodicity in terms of ...
decomp = decompose(obs, period=365) decomp
STL-usage-example.ipynb
jrmontag/STLDecompose
mit
The resulting object is an extended version of the statsmodels.tsa.seasonal.DecomposeResult. Like the statsmodels object, the arrays of values are available on the object (the observations; and the trend, seasonal, and residual components). An extra attribute (the average seasonal cycle) has been added for the purpose ...
decomp.plot();
STL-usage-example.ipynb
jrmontag/STLDecompose
mit
Forecast While the STL decomposition is interesting on its own, STLDecompose also provides some relatively naive capabilities for using the decomposition to forecast based on our observations. We'll use the same data set, but pretend that we only had the first two third of observations. Then we can compare our forecas...
len(obs) short_obs = obs.head(10000) # apply the decomp to the truncated observation short_decomp = decompose(short_obs, period=365) short_decomp
STL-usage-example.ipynb
jrmontag/STLDecompose
mit
The forecast() method requires the following arguments: - the previously fit DecomposeResult - the number of steps forward for which we'd like the forecast - the specific forecasting function to be applied to the decomposition There are a handful of predefined functions that can be imported from the stldecompose.foreca...
fcast = forecast(short_decomp, steps=8000, fc_func=drift) fcast.head()
STL-usage-example.ipynb
jrmontag/STLDecompose
mit
If desired, we can then plot the corresponding components of the observation and forecast to check and verify the results.
plt.plot(obs, '--', label='truth') plt.plot(short_obs, '--', label='obs') plt.plot(short_decomp.trend, ':', label='decomp.trend') plt.plot(fcast, '-', label=fcast.columns[0]) plt.xlim('1970','2004'); plt.ylim(330,380); plt.legend();
STL-usage-example.ipynb
jrmontag/STLDecompose
mit
To include the estimated seasonal component in the forecast, use the boolean seasonal keyword.
fcast = forecast(short_decomp, steps=8000, fc_func=drift, seasonal=True) plt.plot(obs, '--', label='truth') plt.plot(short_obs, '--', label='obs') plt.plot(short_decomp.trend, ':', label='decomp.trend') plt.plot(fcast, '-', label=fcast.columns[0]) plt.xlim('1970','2004'); plt.ylim(330,380); plt.legend(); fcast.head(...
STL-usage-example.ipynb
jrmontag/STLDecompose
mit
RiiDataFrame has an attribute named catalog that is a Pandas DataFrame provinding the catalog of experimental data as shown below. The columns formula and tabulated indicate the type of data. If n or k is included in the column tamulated, the experimentally observed refractive index n or extinction coefficient k is gi...
ri.catalog.head(3)
docs/notebooks/02_RiiDataFrame.ipynb
mnishida/RII_Pandas
mit
The experimental data are given by raw_data:
ri.raw_data.loc[3].head(5) # first 5 rows for the material whose id is 3
docs/notebooks/02_RiiDataFrame.ipynb
mnishida/RII_Pandas
mit
where n is the refractive index and k is the extinction coefficient at the vacuum wavelength wl_n (wl_k) in the unit of &mu;m. The column c gives the coefficients for the dielectric function model. In the above example, no coefficient is given because only the tabulated data are given (formula number in catalog is 0). ...
ri.catalog.tail(3) ri.raw_data.loc[2911].head(5) # first 5 rows for the material whose id is 2912
docs/notebooks/02_RiiDataFrame.ipynb
mnishida/RII_Pandas
mit
Using the method load_grid_data(), you can get grid data calculated at 200 wavelength values in the range [wl_min, wl_max], which is the intersection between the domain of n [wl_n_min, wl_n_max] and the domain of k [wl_k_min, wl_k_max]. These values are shown in catalog.
grid_data = ri.load_grid_data(3) grid_data
docs/notebooks/02_RiiDataFrame.ipynb
mnishida/RII_Pandas
mit
Helper Methods By using the functionality of Pandas, you may find what you want, easily. But, here some simple helper methods are implemented. plot plot(id: int, comp: str = "n", fmt1: Optional[str] = "-", fmt2: Optional[str] = "--", **kwargs) * id (int): ID number. * comp (str): 'n', 'k' or 'eps'. * fmt1 (Union[str, N...
import matplotlib.pyplot as plt ri.plot(3, "n") plt.show() ri.plot(3, "k") plt.show() ri.plot(3, "eps") plt.show()
docs/notebooks/02_RiiDataFrame.ipynb
mnishida/RII_Pandas
mit
search search(name: str) -&gt; DataFrame This method searches data whose book or book_name contain given name and return a simplified catalog for them.
ri.search("NaCl") ri.search("sodium") # upper or lower case is not significant
docs/notebooks/02_RiiDataFrame.ipynb
mnishida/RII_Pandas
mit
select select(condition: str) -&gt; DataFrame This method make a query with the given condition and return a simplified catalog. It will pick up materials whose experimental data contains some data that fulfill given condition.
ri.select("2.5 < n < 3 and 0.4 < wl < 0.8").head(10) ri.plot(157)
docs/notebooks/02_RiiDataFrame.ipynb
mnishida/RII_Pandas
mit
show show(id: int | Sequence[int]) -&gt; DataFrame This method shows a simplified catalog for given id.
ri.show(1)
docs/notebooks/02_RiiDataFrame.ipynb
mnishida/RII_Pandas
mit
read read(id, as_dict=False) This method returns the contants of a page associated with the id. If you want the page contents as a python dict, give True for argument as_dict.
print(ri.read(0)) ri.read(0, as_dict=True)
docs/notebooks/02_RiiDataFrame.ipynb
mnishida/RII_Pandas
mit
references references(id: int) This method returns the REFERENCES of a page associated with the id.
ri.references(20)
docs/notebooks/02_RiiDataFrame.ipynb
mnishida/RII_Pandas
mit
material material(params: dict) -&gt; Material Create Material-class instance for given parameter dict params. params can includes the following parameters, * 'id': ID number. (int) * 'book': book value in catalog of RiiDataFrame. (str) * 'page': page value in catalog of RiiDataFrame. (str) * 'RI': Constant refractive ...
water = ri.material({'id': 428}) water.catalog
docs/notebooks/02_RiiDataFrame.ipynb
mnishida/RII_Pandas
mit
Python depends on packages for most of its functionality; these can be either built-in (such as sys), or third-party (like all the packages below). Either way you need to import the packages you need before using them. The Notebook Look up http:/www.google.com Lets eat a burrito. $\alpha = \frac{\beta}{\gamma}$ Longer:...
df=pd.read_csv("all.csv", header=None, names=["rating", 'review_count', 'isbn', 'booktype','author_url', 'year', 'genre_urls', 'dir','rating_count', 'name'], ) df.head()
Lab1-pythonpandas_original.ipynb
stevenydc/2015lab1
mit
Notice we have a table! A spreadsheet! And it indexed the rows. Pandas (borrowing from R) calls it a DataFrame. Lets see the types of the columns... df, in python parlance, is an instance of the pd.DataFrame class, created by calling the pd.read_csv function, which cllas the DataFrame constructor inside of it. If you d...
df.dtypes
Lab1-pythonpandas_original.ipynb
stevenydc/2015lab1
mit
This gives us Trues and Falses. Such a series is called a mask. If we count the number of Trues, and divide by the total, we'll get the fraction of ratings $\lt$ 3. To do this numerically see this:
np.sum(df.rating < 3)
Lab1-pythonpandas_original.ipynb
stevenydc/2015lab1
mit
Notice that you could just find the average since the Trues map to 1s.
np.mean(df.rating < 3.0)
Lab1-pythonpandas_original.ipynb
stevenydc/2015lab1
mit
Oppos we got an error. Something is not right. Its trying to convert some python datatype: None into an int. This usually means data was missing. Was it?
df[df.year.isnull()]
Lab1-pythonpandas_original.ipynb
stevenydc/2015lab1
mit
Much cleaner now! Visualizing Pandas has handy built in visualization.
df.rating.hist();
Lab1-pythonpandas_original.ipynb
stevenydc/2015lab1
mit
We can do this in more detail, plotting against a mean, with cutom binsize or number of bins. Note how to label axes and create legends.
sns.set_context("notebook") meanrat=df.rating.mean() #you can get means and medians in different ways print meanrat, np.mean(df.rating), df.rating.median() with sns.axes_style("whitegrid"): df.rating.hist(bins=30, alpha=0.4); plt.axvline(meanrat, 0, 0.75, color='r', label='Mean') plt.xlabel("average rating ...
Lab1-pythonpandas_original.ipynb
stevenydc/2015lab1
mit
One can see the sparseness of review counts. This will be important when we learn about recommendations: we'll have to regularize our models to deal with it.
df.review_count.hist(bins=np.arange(0, 40000, 400))
Lab1-pythonpandas_original.ipynb
stevenydc/2015lab1
mit
Here we make a scatterplot in matplotlib of rating against year. By setting the alpha transparency low we can how the density of highly rated books on goodreads has changed.
plt.scatter(df.year, df.rating, lw=0, alpha=.08) plt.xlim([1900,2010]) plt.xlabel("Year") plt.ylabel("Rating")
Lab1-pythonpandas_original.ipynb
stevenydc/2015lab1
mit
In the cell below I have calculated in the previous cell the loglikelyhood score of the partitioned sampling and vanilla sampling technique image-wise. So I have a score for each image. I have done this for all the MNIST digits that have been 'corrupted' by the bar images. That is RBM's trained models 1 - 9 and an RBM ...
for key in results: logging.info("Plotting, win, lose and tie images for the {}".format(key)) results[key].plot_various_images()
Max/MNIST-ORBM-Inference.ipynb
garibaldu/multicauseRBM
mit
Thoughts So on a dataset of size 50, with 100 gibbs alterations we see in all cases that for the digit model, 1,2,3,..,9 that the partitioned sampling technique does either better or the same more often than the vanilla does. Let's try some different configurations.
results.update(results_for_models(models, corruption_model_name, 400, 500))
Max/MNIST-ORBM-Inference.ipynb
garibaldu/multicauseRBM
mit
results.update(results_for_models(models, corruption_model_name, 10, 1))
results # with open('results_dict', 'wb') as f3le: # pickle.dump(results,f3le, protocol = None) with open('results_dict', 'rb') as f4le: results = pickle.load(f4le) # for key in results: # if key.startswith('400'): # logging.info("Results for hiddens") # r = results[key].stored_hiddens # ...
Max/MNIST-ORBM-Inference.ipynb
garibaldu/multicauseRBM
mit
1b. KNN (K=5) cf. Machine Learning with Scikit-Learn
from sklearn.neighbors import KNeighborsClassifier knn = KNeighborsClassifier(n_neighbors=5) knn.fit(X,y) y_pred=knn.predict(X) # compute classification accuracy for the logistic regression model from sklearn import metrics print(metrics.accuracy_score(y,y_pred)) ### 1c. KNN (K=1) knn = KNeighborsClassifier(n_neigh...
coursera_Ng/KNN.ipynb
ernestyalumni/MLgrabbag
mit
2. Evaluation procedure 2 - Train/test split Data is randomly assigned unless you use random_state hyperparameter * If you use random_state=4 - Your data will be split exactly the same way
# STEP 1: split X and y into training and testing sets from sklearn.cross_validation import train_test_split X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.4,random_state=4) knn=KNeighborsClassifier(n_neighbors=5) knn.fit(X_train,y_train) y_pred=knn.predict(X_test) print(metrics.accuracy_score(y_test,y...
coursera_Ng/KNN.ipynb
ernestyalumni/MLgrabbag
mit