Unit 6 / 11

Chemical Data Analysis and Python: pandas, Stoichiometry and Calibration

Gains:

  • Ability to base numerical results on executed Python code rather than verbal guessing of the language model
  • Ability to write the stoichiometry, calibration and data cleaning code to artificial intelligence and test it with samples with known answers
  • Ability to prevent data analysis errors by always specifying units and checking their order

Chemistry is full of numbers: weighings, volumes, absorbance values, yields, concentrations. Processing this data manually is slow and error-prone. AI provides two major services here: (1) writes Python code that processes the data, (2) runs that code (in an environment that can run the code) and gives a deterministic result. The critical principle is this: leave the calculation to the output of the executed code, not to the "verbal prediction" of the language model. Language model "What is 142 × 0.05?" may sometimes answer the question incorrectly; but the Python line he writes always calculates correctly. In this unit, we will learn chemical data analysis with AI with this philosophy.

Why is code better than verbal guessing?

A language model does arithmetic by "predicting the possible outcome"; so it can be wrong with large numbers or multi-step calculus. Whereas in Python the expression 142 * 0.05 is deterministic: it always returns 7.1. So strategy: don't make the AI ​​do the calculation, print the CODE of the calculation and run the code. So you use the creativity of the AI ​​(building the code) and disable the unreliable side (head arithmetic).

Tip: When you get a numerical result from an AI, say "represent this with a line of Python." The code itself both verifies the account and allows you to run it and confirm it. If you don't see a code, don't trust the number.

Typical data analysis tasks in chemistry

  • Stoichiometry: mole, mass, limiting reagent, theoretical yield, percent yield calculation.
  • Concentration: molarity, dilution (M1V1 = M2V2), ppm conversions.
  • Calibration: Drawing a calibration line with the Beer-Lambert law (absorbance ∝ concentration) and calculating the unknown.
  • Data cleaning: reading measurement tables with pandas, flagging outliers, mean/standard deviation.
  • Visualization: drawing calibration curve, kinetics graph, titration curve.

Step by step: Safe data analysis with AI

  1. Define data: Clearly give columns, units, sample rows. If there is no unit, the AI ​​makes assumptions.
  2. Ask for code, not results: say "Write pandas/NumPy code that calculates this".
  3. Run the code: Run it yourself or in an environment that can run code.
  4. Test with simple case: Test the code with a small example where you know the answer.
  5. Unit and order check: Are the unit and magnitude of the result physically reasonable?
  6. Document: Add the code and output to the experiment notebook (unit 8).

Four copyable templates

1) Stoichiometry and percent yield:

Reaction: salicylic acid (MA 138.12) + acetic anhydride -> aspirin (MA 180.16).Data: 2.00 g of salicylic acid was used, acetic anhydride was in excess.Aspirin obtained: 2.15 g.Task: Write Python code that calculates theoretical yield and percent yield.Define molecular weights as variables, print the result in units.Do not calculate in head; just give executable code.

2) Beer-Lambert calibration:

Calibration data (concentration mol/L -> absorbance): 0.00->0.02, 0.02->0.21, 0.04->0.40, 0.06->0.62, 0.08->0.79. Unknown sample absorbance: 0.50. Task: Perform linear calibration with numpy.polyfit, slope/intercept and Calculate R^2, find the unknown concentration. Plot the data + fit line with matplotlib.

3) measurement chart with pandas:

I have a CSV: columns = sample, weigh_g, volume_mL, absorbance. Task: read with pandas, calculate concentration (g/L) for each sample, mark rows with absorbance < 0 as incorrect, give code to print the mean and std deviation of the groups. Specify the units with a comment line.

4) Dilution account verification:

I want to prepare 100 mL of 0.05 M solution from 0.5 M stock solution. Task: Write the Python line that calculates the required stock volume with M1V1=M2V2. Print the result in mL and verify as a comment that a 10-fold dilution is expected for a logic check.

Weak prompt / Strong prompt

Weak:

How much aspirin comes from 2 grams of salicylic acid?

The AI ​​gives a number from the head; If it remembers the molecular weights incorrectly, the result will be distorted and how it was calculated will not be visible.

Strong:

Assume salicylic acid MA=138.12, aspirin MA=180.16, mass=2.00 g, yield 100%.Task: Write Python code that calculates the theoretical mass of aspirin; comment each step (mole -> mol -> mass), print the result in g.

Difference: molecular weights given, code requested, steps commented, unit specified. The result is both accurate and auditable.

Verbal prediction etc. executed code

Size

Language model verbal prediction

Python run

Arithmetic accuracy

Variable, error may occur

Deterministic, certain

Auditability

Miscarriage (it is unclear how it came out)

High (code visible)

repeatability

low

high

Unit tracking

weak

Can be kept open in code

Proper use

Idea, building structure

Final numerical result

mini cases

Case 1 — Verbal arithmetic error. A student asks AI “0.0145 mol × 180.16 g/mol?” he asked; “2.72 g,” the AI ​​said. Actually it is 2.61g. When the student said "show this in Python", YZ wrote 0.0145 * 180.16 and it came out as 2,612; The first verbal response was incorrect. Lesson: prefer code even for very simple multiplication.

Case 2 — R² check in calibration. One user had a Beer-Lambert calibration done; It turns out R² = 0.981. The AI ​​said "linear, reliable" but the point at the highest concentration was below the line (saturation). When the user saw the graph, it moved the highest point out of the linear range, R² increased to 0.999. Lesson: R² alone is not enough; see residuals/plot.

Case 3 — Unit confusion. In one analysis it was unclear whether the concentration was mg/L or g/L; The AI ​​assumed g/L and the results were 1000 times wrong. It was fixed when the user explicitly specified the column unit. Lesson: always shortchange the unit, assuming AI is expensive.

Common mistakes

  • Relying on verbal number. Always request and run code rather than doing mental arithmetic.
  • Not specifying the unit. Mixing mg/L with g/L, mL with L, causes a 1000-fold error.
  • Not testing the code. Applying to big data without testing with a small example where the answer is known.
  • Considering R² as the only criterion. Trusting the non-linearity points without seeing them graphically.
  • Skip the tester reagent. Calculating theoretical yield without determining the limiting reagent in stoichiometry.
  • Significant step outpouring. Reporting the result to 8 digits when the measurement is 3 significant digits.
Caution: Even the code the AI ​​writes may be faulty (wrong column name, wrong formula). Running the code makes the result deterministic, but you must confirm with a known example that the code calculates the right thing.

In summary

  • Leave the numerical results to the executed Python code, not to the verbal guess of the language model.
  • AI quickly writes code for stoichiometry, calibration, data cleaning and visualization in chemical data analysis.
  • Always put units on; Unit confusion is the most expensive mistake.
  • Examine the residuals and graph in addition to R² in the calibration.
  • Test the code with a simple example with a known answer; Human verifies the accuracy of the code.

Application task

Have a calibration or stoichiometry data set (if not, use the Beer-Lambert data above). Ask the AI ​​for the Python code that does the analysis (the code, not the result). Run the code; then test it with a small sample of known answers. Ask the AI ​​verbally for the same calculation and compare the two results: is there a difference? Interpret the plot of R² and residual in calibration. Put the code, output, and short comment in a document.

checklist

  • [ ] I get the numerical results from the code, not from the verbal guess.
  • [ ] I clearly indicate the unit of each data column.
  • [ ] I test the code with a small sample whose answer is known.
  • [ ] I perform residual/graph analysis next to R² in calibration.
  • [ ] I determine the limiting reagent in stoichiometry.
  • [ ] I report the results with the appropriate significant digit.