r/PythonLearning • u/BlankPageNot00 • 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:
- Load datasets:
- Retrieves boltztrap_mp, ucsb_thermoelectrics, and expt_gap_kingsbury.
- Standardise column names:
- Renames 'composition' to 'formula' in the temperature dataset
- so that the datasets can be merged.
- Merge datasets:
- Combines all three datasets using 'formula' as the common key.
- Keeps all rows from the electronic dataset.
- Identify numerical columns:
- Automatically detects columns containing numerical data.
- Handle missing values:
- Uses SimpleImputer(strategy="median") to fill missing numerical
- 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
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/Sweet_Computer_7116 4d ago
No dude this code is bloody amazing. Never seen anything like it. You should sell the code