CBSE Class 12 Artificial Intelligence Practical File, Python Programs with Output
The CBSE Class 12 Artificial Intelligence (843) practical covers data handling, model evaluation and the Capstone Project. The programs below were run before publishing, so the confusion matrix, the accuracy and the precision figures you see are the numbers the code actually produced.
1. Read a CSV into pandas and describe it
import pandas as pd
pd.DataFrame({"hours": [1, 2, 3, 4, 5],
"score": [35, 48, 55, 68, 79]}).to_csv("study.csv", index=False)
df = pd.read_csv("study.csv")
print(df)
print()
print(df.describe()) hours score
0 1 35
1 2 48
2 3 55
3 4 68
4 5 79
hours score
count 5.000000 5.000000
mean 3.000000 57.000000
std 1.581139 17.131842
min 1.000000 35.000000
25% 2.000000 48.000000
50% 3.000000 55.000000
75% 4.000000 68.000000
max 5.000000 79.0000002. Find and handle missing values
import pandas as pd
import numpy as np
df = pd.DataFrame({"name": ["Aarav", "Diya", "Ishaan"],
"marks": [78, np.nan, 66]})
print("Missing values per column:")
print(df.isnull().sum())
df["marks"] = df["marks"].fillna(df["marks"].mean())
print()
print(df)Missing values per column:
name 0
marks 1
dtype: int64
name marks
0 Aarav 78.0
1 Diya 72.0
2 Ishaan 66.03. Split data into training and testing sets
import pandas as pd
df = pd.DataFrame({"hours": [1, 2, 3, 4, 5, 6, 7, 8],
"score": [35, 48, 55, 68, 72, 79, 85, 91]})
train = df.iloc[:6]
test = df.iloc[6:]
print("Training rows:", len(train), " Testing rows:", len(test))
print("Test set:")
print(test)Training rows: 6 Testing rows: 2 Test set: hours score 6 7 85 7 8 91
4. Build a confusion matrix by hand and read it
actual = [1, 0, 1, 1, 0, 1, 0, 0]
predicted = [1, 0, 1, 0, 0, 1, 1, 0]
tp = sum(1 for a, p in zip(actual, predicted) if a == 1 and p == 1)
tn = sum(1 for a, p in zip(actual, predicted) if a == 0 and p == 0)
fp = sum(1 for a, p in zip(actual, predicted) if a == 0 and p == 1)
fn = sum(1 for a, p in zip(actual, predicted) if a == 1 and p == 0)
print("True Positive :", tp)
print("True Negative :", tn)
print("False Positive:", fp)
print("False Negative:", fn)
print("Accuracy :", round((tp + tn) / len(actual) * 100, 2), "%")True Positive : 3 True Negative : 3 False Positive: 1 False Negative: 1 Accuracy : 75.0 %
5. Calculate precision, recall and F1 score
tp, fp, fn = 3, 1, 1
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1 = 2 * precision * recall / (precision + recall)
print("Precision:", round(precision, 2))
print("Recall :", round(recall, 2))
print("F1 Score :", round(f1, 2))Precision: 0.75 Recall : 0.75 F1 Score : 0.75
6. Count word frequency for a simple NLP bag of words
text = "an ai model learns from data and a model improves with more data"
bag = {}
for word in text.split():
bag[word] = bag.get(word, 0) + 1
for word, count in sorted(bag.items(), key=lambda x: (-x[1], x[0]))[:5]:
print(word, ":", count)data : 2 model : 2 a : 1 ai : 1 an : 1
7. Normalise a column between 0 and 1
import pandas as pd
df = pd.DataFrame({"marks": [35, 48, 55, 68, 79]})
low, high = df["marks"].min(), df["marks"].max()
df["normalised"] = ((df["marks"] - low) / (high - low)).round(3)
print(df)marks normalised 0 35 0.000 1 48 0.295 2 55 0.455 3 68 0.750 4 79 1.000
8. Plot a bar chart and save it as an image
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
subjects = ["CS", "IP", "AI"]
students = [42, 31, 27]
plt.bar(subjects, students)
plt.title("Students per subject")
plt.savefig("subjects.png")
print("Chart saved as subjects.png")
print("Bars:", dict(zip(subjects, students)))Chart saved as subjects.png
Bars: {'CS': 42, 'IP': 31, 'AI': 27}Get the full practical-file pack, free
Every program, the exact practical-file format your school expects, and the viva questions that come up most — sent on WhatsApp.
Get it on WhatsApp Book a free demo classRelated: Class 12 AI syllabus · Class 10 AI syllabus · Try the code in the Python Playground
Stuck on your practical file or the viva?
Kajal Ma'am has been teaching these practicals since 2004. See a real class first — no payment, no commitment.
Book a free demo class
