<jupyter_start><jupyter_text>Brute force random sample generation  Original data import<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline
# Load dataset
df = pd.read_csv('glass.csv')
df.head()
# Remove targets
df = df.drop(df.columns[[0,1,10]], axis=1)
print(df.shape)
df.head()
# See the distributions of variables

start = df.columns.get_loc('Na')          # column of 'cylinders'
end   = df.columns.get_loc('Fe') + 1     # column of 'acceleration'

fig = plt.figure(figsize=(10, 4))
sns.boxplot(data=df.iloc[:, start:end])
plt.xlabel('variables')
plt.ylabel('values')
plt.show()
# Remove outliers if necessary
df = df[df['K'] < 5.0]
df = df[df['Ca'] < 10.0]
print(df.shape)<jupyter_output>(185, 8)<jupyter_text>Brute force<jupyter_code># Input the size of random variables
N = input('Input number of random variables for simulation : ')         # simulation sample size like 10000
N = int(N)
# Generate number N of random variables between min and max values in columns
list_columns = df.columns.values
print('id', 'element', 'min', 'max', sep = '\t')

random_var = np.zeros(N * len(list_columns[start:end])).reshape(N, len(list_columns[start:end]))

for i, name in enumerate(list_columns[start:end]) :
    min_column = min(df.iloc[:, start+i])
    max_column = max(df.iloc[:, start+i])
    column_rand = np.random.uniform(min_column, max_column, N)
    random_var[:, i] = column_rand
    print(i, name, min_column, max_column, sep = '\t')

df_comp_bf = pd.DataFrame(random_var)
df_comp_bf.columns = list_columns[start:end]

print('Shape of dataframe : ', df_comp_bf.shape)
print(df_comp_bf.shape)
df_comp_bf.head()
# Draw scatter plots of samples generated for reverse analysis
horz = 5                     # vertical number of graph
vert = 5                     # horizontal number of graph
graph_num = horz * vert      # maximum number of graphs
axes = list()

print('Samples generated for backward prediction')
fig = plt.figure(figsize=(15, 15))

for i in range(0, end-start):
    axes.append(fig.add_subplot(vert, horz, i+1))

    for j in range(start, end) :
        x_sample = df_comp_bf.iloc[:, start]
        y_sample = df_comp_bf.iloc[:, i]
        axes[i].scatter(x_sample, y_sample, c='b', marker = '.', alpha = 0.01)
        axes[i].set_xlabel(df.columns[start], size = 12)
        axes[i].set_ylabel(df.columns[i], size = 12)

plt.subplots_adjust(wspace=0.4, hspace=0.4)
plt.show()
# Adjust total amount say 100% if necessary
df_bf_adj = df_comp_bf.apply(lambda x: 100 * x/np.sum(x), axis=1)

print(df_bf_adj.shape)
df_bf_adj.head()
# draw scatter plots of samples generated for reverse analysis

horz = 5                     # vertical number of graph
vert = 5                     # horizontal number of graph
graph_num = horz * vert      # maximum number of graphs
axes = list()

print('Samples generated for reverse analysis')
fig = plt.figure(figsize=(15, 15))

for i in range(0, end-start):
    axes.append(fig.add_subplot(vert, horz, i+1))

    for j in range(start, end) :
        x_sample = df_bf_adj.iloc[:, start]
        y_sample = df_bf_adj.iloc[:, i]
        axes[i].scatter(x_sample, y_sample, c='r', marker = '.', alpha = 0.01)
        axes[i].set_xlabel(df.columns[start], size = 12)
        axes[i].set_ylabel(df.columns[i], size = 12)

plt.subplots_adjust(wspace=0.4, hspace=0.4)
plt.show()
# Sanity check adjustment result
test = np.sum(df_bf_adj, axis=1)
print(test[0:2])
print(test[998:1000])
# Save generated samples
df_comp_bf.to_csv('generated_samples_brute_force.csv', index = None)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1371900826446281, 'fraction_numerical': 0.021487603305785124, 'mean_word_length': 3.960382513661202, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '350843', 'license': 'MIT', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1277, 'n_words': 328, 'path': '[4]_Random_sample_generation_for_backward_prediction/Brute_force_random_sample_generation.ipynb', 'repo_name': 'Masa-Ishimaru/Material-Informatics', 'size': '3630'}