<jupyter_start><jupyter_text>Read regression model and parameters to scale back<jupyter_code>with open('best_gaussr_RI.pickle', mode='rb') as f:
    best_gaussr_RI= pickle.load(f)
RI_mean_std_GPR = np.load('RI_mean_std_GPR.npy')<jupyter_output><empty_output><jupyter_text>Raed random samples generated<jupyter_code>df_MMF = pd.read_csv('generated_samples_fit_dist.csv')
df_GMM = pd.read_csv('generated_samples_GMM_adj.csv')
print(df_MMF.shape)
df_MMF.head(2)
print(df_GMM.shape)
df_GMM.head(2)
#　Get column numbers of start and end

start = df_MMF.columns.get_loc('Na')
end   = df_MMF.columns.get_loc('Fe') + 1
print('start = ', start)
print('end   = ', end)
# scaling

def standardize_dataframe(X, X_base):
    X_scaled = (X - X_base.mean(axis = 0)) / X_base.std(axis = 0)
    
    return X_scaled<jupyter_output><empty_output><jupyter_text>Backward analysis<jupyter_code># read variables for scaling

df_mean_std = pd.read_csv('glass.csv')
df_mean_std = df_mean_std.iloc[:, 2:10]
print(df_mean_std.shape)
df_mean_std.head()
# Compute prediction on distribution fit samples

y_MMF_mean_s, y_MMF_std_s = best_gaussr_RI.predict(standardize_dataframe(df_MMF, df_mean_std), return_std = True)

y_MMF_mean = y_MMF_mean_s * RI_mean_std_GPR[1] + RI_mean_std_GPR[0]
y_MMF_std  = y_MMF_std_s * RI_mean_std_GPR[1]

df_MMF['pred_RI_mean'] = y_MMF_mean
df_MMF['pred_RI_std']  = y_MMF_std

df_MMF['group'] = 'MMF'
df_MMF.head()
# Compute prediction on GMM samples

y_GMM_mean_s, y_GMM_std_s = best_gaussr_RI.predict(standardize_dataframe(df_GMM, df_mean_std), return_std = True)

y_GMM_mean = y_GMM_mean_s * RI_mean_std_GPR[1] + RI_mean_std_GPR[0]
y_GMM_std  = y_GMM_std_s * RI_mean_std_GPR[1]

df_GMM['pred_RI_mean'] = y_GMM_mean
df_GMM['pred_RI_std']  = y_GMM_std

df_GMM['group'] = 'GMM'
df_GMM.head()
fig = plt.figure(figsize=(6, 4))

plt.hist(y_MMF_mean, color = 'r', alpha = 0.3, label = 'MMF')
plt.hist(y_GMM_mean, color = 'b', alpha = 0.3, label = 'GMM')

plt.xlabel('RI')
plt.ylabel('Frequency')
plt.legend(loc = 'best')

plt.show()<jupyter_output><empty_output><jupyter_text>Concatnate predictions<jupyter_code>df_sample_all = pd.concat([df_MMF, df_GMM])
print(df_sample_all.shape)
df_sample_all.head()<jupyter_output>(20001, 11)<jupyter_text>Narrow down in the targets<jupyter_code>df_sample_all = df_sample_all[df_sample_all['pred_RI_mean'] >= 1.523]
df_sample_all = df_sample_all[df_sample_all['pred_RI_mean'] <= 1.5235]
print(df_sample_all.shape)
pd.options.display.float_format = '{:.2f}'.format
pd.set_option('display.max_columns', 50)
df_sample_all
# save the solution

df_sample_all.to_csv('solution_GPR.csv', index = None)<jupyter_output><empty_output><jupyter_text>Clustering of the solution<jupyter_code>df_experiment = pd.read_csv('solution_GPR.csv')
df_experiment = df_experiment.iloc[:, start:end]
print(df_experiment.shape)
df_experiment.head()<jupyter_output>(191, 8)<jupyter_text>Clustering by Gaussian mixture model (GMM)Reference:https://datachemeng.com/gaussianmixturemodel/<jupyter_code>max_number_of_components = 20

covariance_types = ['full', 'diag', 'tied', 'spherical']
bic_values = np.empty([max_number_of_components, len(covariance_types)])
bic_values.shape
import warnings
warnings.filterwarnings('ignore')
X = df_experiment.iloc[:, start:end]

from sklearn.mixture import GaussianMixture

# Grid search using BIC
for covariance_type_index, covariance_type in enumerate(covariance_types):
    for number_of_components in range(max_number_of_components):
        gmm_model = GaussianMixture(n_components = number_of_components + 1, covariance_type = covariance_type)
        gmm_model.fit(X)
        bic_values[number_of_components, covariance_type_index] = gmm_model.bic(X)
# Plot

plt.rcParams["font.size"] = 12
plt.figure()

plt.plot(bic_values[:, 0], 'b-', label='full')
plt.plot(bic_values[:, 1], 'g-', label='diag')
plt.plot(bic_values[:, 2], 'k-', label='tied')
plt.plot(bic_values[:, 3], 'r-', label='spherical')

plt.xlabel('Number of components')
plt.ylabel('BIC values')
plt.legend(bbox_to_anchor=(0.88, 0.9, 0.5, 0.001), borderaxespad=0., )
plt.show()
# Select the cluster number with smallest BIC

optimal_index = np.where(bic_values == bic_values.min())
optimal_number_of_components = optimal_index[0][0] + 1
optimal_covariance_type = covariance_types[optimal_index[1][0]]

print(optimal_index)
print(optimal_number_of_components)
print(optimal_covariance_type)
# GMM fit

gmm = GaussianMixture(n_components = optimal_number_of_components, covariance_type = optimal_covariance_type)
gmm.fit(X)
# mean and covariance
means = gmm.means_
if gmm.covariance_type == 'full':
    all_covariances = gmm.covariances_
    
elif gmm.covariance_type == 'diag':
    all_covariances = np.empty([gmm.n_components, gmm.covariances_.shape[1], gmm.covariances_.shape[1]])
    
    for component_number in range(gmm.n_components):
        all_covariances[component_number, :, :] = np.diag(gmm.covariances_[component_number, :])
        
elif gmm.covariance_type == 'tied':
    all_covariances = np.tile(gmm.covariances_, (gmm.n_components, 1, 1))
    
elif gmm.covariance_type == 'spherical':
    all_covariances = np.empty([gmm.n_components, gmm.means_.shape[1], gmm.means_.shape[1]])
    for component_number in range(gmm.n_components):
        all_covariances[component_number, :, :] = np.diag(gmm.covariances_[component_number] * np.ones(gmm.means_.shape[1]))
labels = gmm.predict(X)
labels
df_experiment['CLS_label'] = labels
print(df_experiment.shape)
df_experiment.head()
# dictionary of colors

color_codes = {0:'red',
               1:'green',
               2:'blue',
               3:'yellow',
               4:'magenta',
               5:'orange',
               6:'gray'}

# make color list

colors = []
for i in range(len(labels)) :
    colors.append(color_codes[labels[i]])<jupyter_output><empty_output><jupyter_text>Plot results (small = high variance, large = small variance of GPR model)<jupyter_code>horz = 5
vert = 5
graph_num = horz * vert
ax = []

fig = plt.figure(figsize=(15, 15))

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

    x_sample = df_sample_all.iloc[:, 3]
    y_sample = df_sample_all.iloc[:, i]
    
    RI_std_max = df_sample_all['pred_RI_std'].max()
    RI_std_min = df_sample_all['pred_RI_std'].min()
    m_size = 10 + 300 * (RI_std_max - df_sample_all['pred_RI_std']) / (RI_std_max - RI_std_min)
    
    ax[i].scatter(x_sample, y_sample, c = colors, marker = 'o', alpha = 0.2, s = m_size)
    ax[i].set_xlabel(df_experiment.columns[3], size = 12)
    ax[i].set_ylabel(df_experiment.columns[i], size = 12)

plt.subplots_adjust(wspace = 0.4, hspace = 0.5)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13747758517632994, 'fraction_numerical': 0.016138673042438732, 'mean_word_length': 5.746975806451613, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 1, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '421077', 'license': 'MIT', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2644, 'n_words': 420, 'path': '[5]_Backward_prediction_and_screening_for_multiple_targets/Backward_prediction_distribution_fit_and_GMM.ipynb', 'repo_name': 'Masa-Ishimaru/Material-Informatics', 'size': '6692'}