Ai practical temporary

 P-2
Write Python code to create a Pandas DataFrame using any sequence data type.


a) Display the DataFrame.+


b) Display first 5 records.


c) Display last 10 records.


d) Display the number of missing values in the dataset.


import pandas as pd


import numpy as np


# Create a list of dictionaries with some missing values


data = [


('Name': 'Alice', 'Age': 25, 'City': 'New York'),


('Name': 'Bob', 'Age': 30, 'City': 'Los Angeles'),


{'Name': 'Charlie', 'Age': 22, 'City': 'Chicago'}, ('Name': 'David', 'Age': None, 'City': 'Houston'), {'Name': 'Eva', 'Age': 28, 'City': None), ('Name': 'Frank', 'Age': 33, 'City': 'Seattle'), {'Name': 'Grace', 'Age': 27, 'City': 'Austin'}, ('Name': 'Helen', 'Age': np.nan, 'City': 'Boston'),


('Name': 'lan', 'Age': 24, 'City': 'Denver'),



('Name': 'Jane', 'Age': 29, 'City': None),


('Name': 'Kyle', 'Age': 31, 'City': 'Miami'}


#Create DataFrame


df = pd.DataFrame(data)


# a) Display the DataFrame


print("Full DataFrame:")


print(df)


#b) Display first 5 records


print("\nFirst 5 records:")


print(df.head())


#c) Display last 10 records


print("\nLast 10 records:")


print(df.tail(10))


#d) Display the number of missing values in the dataset


print("\nNumber of missing values in each column:")


print(df.isnull().sum())


P-7

from sklearn.linear_model import Linear Regression


# Data: Hours and corresponding marks


hours = [[1], [2], [3], [4], [5]]


marks = [35, 45, 50, 70, 75]


# Create the model


model = LinearRegression()


#Train the model


model.fit(hours, marks)


# Predict marks for 6 hours of study


predicted = model.predict([[6]])


# Show result


print("Predicted marks for 6 hours:", predicted[0])

P-5

import pandas as pd.


from sklearn.model_selection import train_test split


from sklearn.linear_model import Linear Regression


from sklearn.metrics import mean squared error


#sample dataset: precting marks based on hours studied


data={


'Hours': [1,2,3,4,5,6,7,8,3.5),


'Marks': [34,45,66,87,94,100,56,78,96]}


#create dataframe



df=pd.DataFrame(data)


#split data into feature and target


X= df[['Hours']]#feature


Y= df['Marks']#target


#split training and testing set into 80% and 20%


X_train,X_test,Y_train,Y_test =


train_test_split(X,Y, test_size=0.2, random_state=1)


model = Linear Regression()# create model


model.fit(X_train, Y_train)#train model


#make predictions


Y_pred model.predict(X_test)


#print prediction and actual


print("predicted marks",Y_pred)


print("Actual marks", Y_test.values)


#Evaluate model


mse = mean_squared_error(Y_test,Y_pred)


print("Mse", mse)

P -3

import numpy as np

arr = np.arange (30,41)

arr1, arr2 = np.array_split(arr,2)

print("First part",arr1)

print("Sec part",arr2)








Post a Comment

Previous Post Next Post