r/PythonLearning • • 4d ago

Discussion Well.... i think this code is totally fine right?

Edited:

"""
Function: load_and_prepare_data()

Purpose:
Collects and combines three semiconductor datasets from Matminer,
then prepares the data for further analysis and machine learning.

Process:

  1. Load datasets:
  2. Retrieves boltztrap_mp, ucsb_thermoelectrics, and expt_gap_kingsbury.
  3. Standardise column names:
  4. Renames 'composition' to 'formula' in the temperature dataset
  5. so that the datasets can be merged.
  6. Merge datasets:
  7. Combines all three datasets using 'formula' as the common key.
  8. Keeps all rows from the electronic dataset.
  9. Identify numerical columns:
  10. Automatically detects columns containing numerical data.
  11. Handle missing values:
  12. Uses SimpleImputer(strategy="median") to fill missing numerical
  13. values with their respective column medians.

Output:
A dictionary containing the cleaned combined dataset (data),
numerical column names (num_cols), and total row count (rows).
"""

(ps: o am just 16 so please nice *v*)

from sklearn.impute import SimpleImputer
import pandas as pd
from matminer.datasets import load_dataset

electronic_df = load_dataset("boltztrap_mp")
temperature_df = load_dataset("ucsb_thermoelectrics")
gap_df = load_dataset("expt_gap_kingsbury")

# Rename composition to formula
df_temp = temperature_df.rename(columns={"composition": "formula"})
print(df_temp.columns.tolist())

# Combine the three datasets
a = pd.merge(electronic_df, df_temp, on="formula", how="left")
com_df = pd.merge(a, gap_df, on="formula", how="left")

# Identify numerical columns
num_cols = com_df.select_dtypes(include="number").columns

# Fill missing numerical values
num_imputer = SimpleImputer(strategy="median")
com_df[num_cols] = num_imputer.fit_transform(com_df[num_cols])
0 Upvotes

5 comments sorted by

2

u/Sea-Ad7805 4d ago

Maybe you want to tell us a bit more about what the intend of the code is? Otherwise most people won't take the time to read it.

-1

u/BlankPageNot00 3d ago edited 3d ago

Yup, sure I have added some infor about my code. Thank for giving advice. The code I shared is my data loader, which combines and prepares semiconductor datasets for the agent's analysis and prediction tasks. I'm exploring whether using these datasets can help address the high MAE in my current prediction model while keeping computational costs manageable. Since this is also part of a competition project, I'd prefer not to disclose too many implementation details at this stage. Hope you understand!

-2

u/BlankPageNot00 3d ago

I'm currently building an AI teaching agent focused on semiconductors. The idea is to help students learn semiconductor concepts through explanations and interactive data analysis. The dataset I'm using contains material properties that the agent can use for comparing materials and analysing properties such as band gaps. I'm also working on integrating an LLM with Python functions so it can understand questions and call the appropriate tools when needed.

1

u/Sweet_Computer_7116 4d ago

No dude this code is bloody amazing. Never seen anything like it. You should sell the code

-1

u/BlankPageNot00 3d ago edited 3d ago

I don't thing it is worth selling, but thank for your appreciation。