Class 12 AI Practical File
Q1. Numpy Arrays
a) Write a statement to create a NumPy array named np1 from a list [10, 20, 30, 40]
np1 = np.array([10, 20, 30, 40])
b) Write a statement to replace 20 with 15
np1[1] = 15
c) Write three statements to demonstrate slicing on np1
np1[1:3] # slice from index 1 to 2 np1[:2] # slice first two elements np1[2:] # slice from index 2 to the end
d) Write a statement to create a 2D NumPy array named np2
np2 = np.array([[1, 2, 3], [4, 5, 6]])
e) Write a statement to display a NumPy array
print(np1)
Q2. DataFrames
a) Write statement to create a Series s1 from a list [11,22,33,44,55]
import pandas as pd s1 = pd.Series([11, 22, 33, 44, 55]) print(s1)
b) Write statement to create a DataFrame from series s1
df1 = pd.DataFrame(s1, columns=['Numbers']) print(df1)
c) Write statement to create a DataFrame from a list of countries
countries = ['India', 'USA', 'China', 'Japan'] df2 = pd.DataFrame(countries, columns=['Country']) print(df2)
d) Create DataFrame from three NumPy arrays containing marks of Maths, Science, SST
import numpy as np
maths = np.array([78, 85, 90])
science = np.array([80, 88, 92])
sst = np.array([75, 89, 84])
df3 = pd.DataFrame({
'Maths': maths,
'Science': science,
'SST': sst
})
print(df3)
e) Create a DataFrame from a dictionary containing marks of 3 students
data = {
'Name': ['Amit', 'Riya', 'Sohan'],
'English': [88, 92, 85],
'Maths': [90, 87, 80],
'AI': [95, 89, 78]
}
df4 = pd.DataFrame(data)
f) Statement to display data stored in the above DataFrame
print(df4)
g) Display first 2 rows from DataFrame
print(df4.head(2))
h) Display last 2 rows from DataFrame
print(df4.tail(2))
i) Modify marks of a particular student (Change Sohan’s AI marks to 90)
df4.loc[df4['Name'] == 'Sohan', 'AI'] = 90
j) Delete a specific row from DataFrame (Delete row with index 1)
df4 = df4.drop(1)
k) Delete a specific column from DataFrame (Delete column “AI”)
df4 = df4.drop(columns=['AI'])
Q3. Reading and Writing CSV files
a) Write statement to store data of a DataFrame containing student names along with their percentage into a CSV file named student.csv
countries = ['India', 'USA', 'China', 'Japan']
df = pd.DataFrame(countries, columns=['Country'])
df.to_csv("student.csv", index=False)
b) Write statement to load data from student.csv into a DataFrame and display it
df1 = pd.read_csv("student.csv")
Q4. Online Retail Sales Analysis
1. Write statement to load the Sales dataset from a CSV file?
df = pd.read_csv("sales.csv")
2. Write statement to preview the first few rows?
df.head()
3. Write statement to calculate sum of sales amount grouped by month? Assuming the dataset has columns: ‘Month’ and ‘Sales_Amount’
df.groupby("Month")["Sales_Amount"].sum()
4. Write statement to visualize sum of sales grouped by month using a stacked bar chart?
df.groupby("Month")["Sales_Amount"].sum().plot(kind="bar", stacked=True)
plt.xlabel("Month")
plt.ylabel("Total Sales")
plt.title("Monthly Sales Summary")
plt.show()
5. Write statement to calculate average sales grouped by product type? Assuming column name: ‘Product_Type’
df.groupby("Product_Type")["Sales_Amount"].mean()
6. How do you summarize key findings?
By interpreting the results of the analysis, such as:
• Identifying months with highest and lowest sales
• Recognizing which product types generate the most revenue
• Observing sales patterns across regions or categories
• Highlighting areas where the company can increase marketing or stock
• Suggesting strategies to improve sales based on data trends
Q5. Linear Regression
i) Write statement to import the Linear Regression model?
from sklearn.linear_model import LinearRegression
ii) Write statement to define features (X) and target (y) for house price prediction
Independent Variable (X): Size of the house
Dependent Variable (Y): Price of the house
x = df[['Size']] y = df['Price']
iii) Write statement to train the Linear Regression model?
model = LinearRegression() model.fit(x, y)
iv) Write statement to predict price of a house (Predicting for a house size of 1500 sq ft)
predicted_price = model.predict([[1500]])
v) Display the predicted value in a user-friendly format?
print("The predicted house price is:", predicted_price[0])