0% found this document useful (0 votes)
7 views

Labrecord

The document discusses programs written in Python to perform various machine learning tasks like linear regression, data preprocessing, and splitting data for training and testing. Linear regression is applied to predict glucose levels from insulin values in a diabetes dataset. The data is preprocessed, normalized, split for training and testing, and the model is evaluated on test data.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Labrecord

The document discusses programs written in Python to perform various machine learning tasks like linear regression, data preprocessing, and splitting data for training and testing. Linear regression is applied to predict glucose levels from insulin values in a diabetes dataset. The data is preprocessed, normalized, split for training and testing, and the model is evaluated on test data.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

BANASTHALI VIDYAPITH

Artificial Intelligence and Machine Learning


LAB RECORD

SUBMITTED TO:- DR. URVASHI PRAKASH SHUKLA


SUBMITTED BY:- MANSI SINGHAL
ROLL NO. :- 2016776
CLASS:- B.TECH (IT-A)
SMART ID:- BTBTI20050
PROGRAM # 1
AIM:- Write a Python programme that uses a for loop to print prime numbers
between 1 to 1000.

CODE:-

OUTPUT :-

CONCLUSION:- For loops are used when we have a block of code which we want
to repeat a fixed number of times. To loop through a set of code a specified number
of times, we can use the range() function.
PROGRAM # 2

AIM:- Write a while loop-based Python programme to print the even values
between 4000 and 10000.

CODE:-

OUTPUT:-

CONCLUSION :- In while loop we can execute a set of statements as long as a


condition is true. Always remember to increment ‘i’, or else the loop will continue
forever.
PROGRAM # 3

AIM :– Write a programme that uses a single function to calculate the area of a
specific shapes.

CODE :–

OUTPUT :–
LAB - 2
PROGRAM # 1

AIM :- Program to learn preprocessing via sklearn via minMax Scaler.

Step 1 :- Import preprocessing and StandardScaler.

Step 2 :- Standardize the values of variables into a standard format using


“MinMaxScaler.”

OUTPUT :-

CONCLUSION :– Transform features by scaling each feature to a given range.This


estimator scales and translates each feature individually such that it is in the given
range on the training set, e.g. between zero and one.
LAB - 3
PROGRAM # 1

AIM :– Program to learn Python Basics.


STEP 1 :- Print any statement.

OUTPUT:-

STEP 2:- if statement

OUTPUT:-

STEP 3:- Type Casting

OUTPUT :-
Step 4:- Assigning Multiple Values

OUTPUT :-

Step 5:- Function

OUTPUT :-

Step 6 :- Data Types


OUTPUT :-

Step 7 :- Array

OUTPUT :-

LAB - 4
PROGRAM # 1

AIM :– To pre-process data to fit in the machine learning model.


STEP 1 :- Pyplot

OUTPUT :-

CODE :-
OUTPUT :-

CODE :-

OUTPUT :-

CODE :-
OUTPUT :-

Step 2 :- Histogram
CODE :–

OUTPUT :-
CONCLUSION :– Matplotlib is a cross-platform, data visualization and graphical
plotting library for Python and its numerical extension NumPy.

PROGRAM # 2
AIM :– Program to read a CSV File Format using “pandas Library”.
CODE :–

OUTPUT :–

CONCLUSION :– It is very easy and simple to read a CSV file using pandas library
functions. Here the read_csv() method of the pandas library is used to read data
from CSV files. We must import the Pandas library. read_csv() that retrieves data in
the form of the Dataframe.

PROGRAM # 3
AIM :– Program to check for “NaN” value in Pandas DataFrame

CODE :–
OUTPUT :–

PROGRAM # 4
AIM :– Program to fill the missing values.
Step 1 : Program to fill the missing glucose value with a constant value.
CODE :–

OUTPUT :–

CONCLUSION :- All the missing values in the price column are filled with the same
value that is 183.
STEP 2 :- Program to fill the missing glucose value with the median value of the
entire column.

CODE :–

OUTPUT :–
STEP 3 :- Program to fill the missing glucose value with the mean value of the
entire column.

CODE :–

OUTPUT :–

STEP 4 :- To fill the missing glucose values using “Forward Fill”..


CODE :–
OUTPUT :–

STEP 5 :- To fill the missing prices using “Interpolate”.


CODE :–

OUTPUT :–

STEP 6 :- Standardize the values of variables into a standard format using


“MinMaxScaler.”
CODE :–
OUTPUT :–

STEP 6 :- Convert to dataframe.


CODE :–
OUTPUT :–

LAB - 6
PROGRAM # 1
AIM :– Program to predict the value of one variable based on the value of another
variable using “Linear Regression” in an array.
STEP 1 :- Importing all the necessary libraries .

STEP 2 :- Passing values to x and y array.

STEP 3 :- Calculating mean.

OUTPUT :–

STEP 4 :- Calculating sample covariance and sample variance.


CODE :–
OUTPUT :–

STEP 5 :- Calculating slope and intercept

CODE :–

OUTPUT :–

STEP 6 :- Plot the given data points.


CODE :–

OUTPUT :–
STEP 7 :- Calculating y_pred.
CODE :–

OUTPUT :–

STEP 8 :- Plot the given data points and fit the regression line.
CODE :–

OUTPUT :–

STEP 9 :- Calculating Absolute squared error.

OUTPUT :–

STEP 10 :- Calculating mean squared error.

OUTPUT :–
STEP 11 :- Calculating root mean squared error.

OUTPUT :-

STEP 12 :- Calculating r square.

OUTPUT :-

STEP 13 :- Calculating adjusted r square.


OUTPUT :-

CONCLUSION :–

Linear Mean Root Mean R square Adjusted R Mean


Regression Squared squared square Absolute
Model Error error Error
Single 2.1600000000 1.469693845 0.878923766 - 1.2800000000
00001 6699071 8161435 0.484304932735 000007
Variable 4261

LAB - 7
PROGRAM # 1
AIM :– Program to predict the value of a Glucose based on the value of another
variable using “Linear Regression” in “Diabetes Dataset” and splitting the data for
training and testing purposes.
CODE –
STEP 1 :- Importing all the necessary libraries and loading the diabetes dataset.

OUTPUT :-

STEP 2 :- Converting the x and y column to the numpy array.

STEP 3 :- Reshape array in such a way that the resulting array has only 1 column.
STEP 4 :- Standardize the values of variables into a standard format using
“MinMaxScaler.”
“Normalizing X”
CODE:-
OUTPUT :–

“Normalizing Y”
CODE:-
OUTPUT :–

Step 5 :- Importing Library and constructing arrays

import matplotlib.pyplot as plt


Step 6 :- Calculating mean of both independent and dependent variable.
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
from sklearn.linear_model import LinearRegression
OUTPUT :–

STEP 7 :- Calculating sample covariance and sample variance.

OUTPUT :–

STEP 8 :- Calculating slope and intercept.

Sxy = np.sum((x_norm-x_mean)*(y_norm-y_mean))
Sxx = np.sum(pow((x_norm-x_mean),2))
Sxy , Sxx

OUTPUT :–

b1 = Sxy/Sxx
b0 = y_mean-b1*x_mean
STEP 9 :– Plot the given data points.

CODE :–

OUTPUT :–

plt.scatter(x_norm,y_norm)
plt.xlabel('Insulin X')

STEP 10 :- Calculating y_pred. plt.ylabel('Glucose Y')

CODE :–

y_pred = b1 * x_norm + b0
OUTPUT :–

STEP 11 :- Plot the given data points and fit the regression line.

CODE :–

OUTPUT :–

plt.scatter(x_norm,y_norm,color = 'red')
plt.plot(x_norm,y_pred,color = 'green')
plt.xlabel('X')
plt.ylabel('y')
STEP 12 :- Calculating Mean Absolute error.
CODE :–

OUTPUT :–

STEP 13 :- Calculating mean squared error.


CODE :–

OUTPUT :–

print("MAE = ", mean_absolute_error(y_norm,y_pred))


STEP 14 :- Calculating root mean squared error.
CODE :–

OUTPUT :–

STEP 15 :- Calculating R square.


print("MSE = ", mean_squared_error(y_norm,y_pred))
CODE :–
OUTPUT :–

CONCLUSION :–

Linear Mean Squared Root Mean R square Absolute R


Regression Error squared square
Model error
Single 0.0252938 0.159040 0.08596897 0.0847757
Variable
PROGRAM 2

//Splitting The Data

STEP 1 :– Import the library

STEP 2 :-Splitting the data.s

CODE :–

STEP 3 :– Calculating mean.


CODE :–

from sklearn.model_selection import train_test_split

OUTPUT :–
x_train, x_test, y_train, y_test = train_test_split(
x_norm, y_norm, train_size=538, test_size=230, random_
STEP 4 :- Calculating sample covariance and sample variance.

xtr_mean = np.mean(x_train)
ytr_mean = np.mean(y_train)
print(xtr_mean)
print(ytr_mean)
OUTPUT :–

STEP 5 :- Calculating slope and intercept.

OUTPUT :–

STEP 6 :- Plot the given data points.


CODE :–
b1_t = Sxyt/Sxxt
b0_t = ytr_mean-b1*xtr_mean
print('slope b1 is ',b1_t)
print('intercept b0 is',b0_t)

OUTPUT :–

plt.scatter(x_train,y_train)
plt.xlabel('Insulin X')
plt.ylabel('Glucose Y')
STEP 7 :- Calculating y_pred.
CODE :–

OUTPUT :–

y_predt = b1_t * x_test + b0_t

STEP 8 :- Plot the given data points and fit the regression line.
y_predt

CODE :–
OUTPUT :–

STEP 9 :- Calculating Mean Absolute error.

CODE :–

OUTPUT :–

STEP 10 :- Calculating mean squared error.


CODE :–

OUTPUT :–

STEP 11 :- Calculating root mean squared error.

print("MAE = ", mean_absolute_error(y_test,y_predt))


CODE :–

OUTPUT :–

STEP 12 :- Calculating R square.


CODE :–

OUTPUT :–

CONCLUSION :–

Linear Mean Mean Squared Root Mean


print("RMSE R square
= ", np.sqrt(mean_squared_error(y_test,y_pre
Regression Absolute Error squared
Model Error error
Single 0.13295691502 0.02921714818 0.1709302436 0.0109610308
902732 042113 09553 20120121
Variable

print("R2 = ", r2_score(y_test,y_predt))

You might also like