python ma[1]
python ma[1]
Problem Statement:
1. Write Python program to create, append, update, delete records from database using GUI.
Theory:
Ans. - SQLite3 can be integrated with Python using sqlite3 module . To use SQLite3 module,
you must first create a connection object that represents the database and then optionally you
can create a cursor object, which will help you in executing in all the SQL statements.
Ans. - Tkinter is the standard GUI library for Python. Python when combined with Tkinter
provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-
oriented interface to the Tk GUI toolkit.
There are two main methods used which the user needs to remember while creating the Python
application with GUI.
To change the name of the window, you can change the className to the desired one.
The basic code used to create the main window of the application is:
m=tkinter.Tk() where m is the name of the main window object
2) mainloop(): There is a method known by the name mainloop() is used when your
application is ready to run. mainloop() is an infinite loop used to run the application, wait
for an event to occur and process the event as long as the window is not closed.
m.mainloop()
Write Python program to obtain histogram of any image
import cv2
from matplotlib import pyplot as plt
img=cv2.imread(r'C:\Users\rutvi\OneDrive\Pictures\Wallpapers\Other\Skull_smoke-liveWP-
73IaEKaPJvmBHhvPdHiy.jpg',0)
histr=cv2.calcHist([img],[0],None,[256],[0,256])
plt.plot(histr)
plt.show()
Output:
Experiment No. 7
Problem Statement:
OpenCV is a huge open-source library for computer vision, machine learning, and image
processing. OpenCV supports a wide variety of programming languages like Python, C++, Java,
etc. It can process images and videos to identify objects, faces, or even the handwriting of a
human. When it is integrated with various libraries, such a Numpy which is a highly optimized
library for numerical operations i.e., whatever operations one can do in Numpy can be
combined with OpenCV.
PROGRAM 1
PROGRAM 2
PROGRAM 3
Code:
Experiment no. 8
Problem Statement:
Theory:
The canvas widget is used to add the structured graphics to the python application. It is used to
draw the graph and plots to the python application. The syntax to use the canvas is given below.
Syntax
w = canvas(parent, options)
SN Option Description
top = Tk()
top.geometry("200x200")
c.pack()
top.mainloop()
Output:
Experiment no.9
Aim: To understand the concept of numpy and pandas library and different in built functions in
python.
Problem Statement: Evaluate the dataset containing the GDPs of different countries to:
a) Find and print the name of the country with the highest GDP b) Find and print the name of the
country with the lowest GDP
e) Print the highest GDP value, lowest GDP value, mean GDP value, and the sum of
all the GDPs.
Theory:
Ans. In Python we have lists that serve the purpose of arrays, but they are slow to
process.NumPy aims to provide an array object that is up to 50x faster than traditional Python
lists.The array object in NumPy is called ndarray, it provides a lot of supporting functions that
make working with ndarray very easy.Arrays are very frequently used in data science, where
speed and resources are very important.
Ans. Pandas is an open source Python package that is most widely used for data science/data
analysis and machine learning tasks. It is built on top of another package named Numpy, which
provides support for multi-dimensional arrays. As one of the most popular data wrangling
packages, Pandas works well with many other data science modules inside the Python ecosystem,
and is typically included in every Python distribution, from those that come with your operating
system to commercial vendor distributions like Active State’s Active Python.
Program:
A) Find and print the name of the country with the highest GDP
import csv
CountryNameH = 'None'
Greatest = 0
Lowest = 10000000000000000000000
a=0
next(str1)
for row in str1:
a = float(row[3])
if a > Greatest:
Greatest = a
CountryNameH = row[0]
Output:
A) Find and print the name of the country with the lowest
CountryNameL = 'None'
Greatest = 0
Lowest = 10000000000000000000000
next(str1)
b=
float(row[3]) if b <
Lowest:
Lowest = b
CountryNameL = row[0]
print('Country with Lowest gdp is ', CountryNameL,' with GDP of : ', Lowest)
Output:
import csv
next(str1)
for row in str1:
print('Country: ', row[0], ' GDP: ', row[3])
Output:
'None' next(str1)
Output:
D) Print the highest GDP value, lowest GDP value, mean GDP value, standardized GDP value,
and the sum of all the GDPs.
import csv
CountryNameH =
'None' CountryNameL =
'None' Greatest = 0
Lowest = 10000000000000000000000
a=0
c=0
num = 0
den = 0
mean = 0
total = 0
next(str1)
for row in str1:
a = float(row[3])
if a > Greatest:
Greatest = a
CountryNameH =
row[0]
b=
float(row[3]) if
b < Lowest:
Lowest = b
CountryNameL = row[0]
c = float(row[3])
num = num + c
den += 1
mean = num/den
d=
float(row[3])
total = total + d
Output:
Experiment No. 10
SciPy - Installation
scipy
Programs:
Experiment No. 11
Theory:
Regression:
⮚ A statistical measure that determines the strength of the relationship between the
one dependent variable (y) and other independent variables (x1, x2, x3……)
⮚ This is done to gain information about one through knowing values of the others.
⮚ It is basically used for predicting and forecasting.
Linear Regression:
⮚ The simplest mathematical linear relationship between two variables x and y.
⮚ The change in one variable make the other variable change.
⮚ In other words, a dependency of one variable to other.
𝑌 = 𝑏0 + 𝑏1 ∗ 𝑋 + ∈
Y = Dependent Variable
X = Independent Variable
b0 = Y – Intercept
b1 = Slope of the line
∈ = Error Variable
Understanding Linear Regression
y = mx + c
3.6 = 0.2*3 + c → c = 3
X - AXIS
3 3.6 10
y = 0.2x + 3
Mean square Error:
X - AXIS
For these values, the predicted values for y for x = (1,2,3,4,5) will be -
y = 0.2 * 1 + 3 = 3 .2
y = 0.2 * 2 + 3 = 3.4
y = 0.2 * 3 + 3 = 3.6
y = 0.2 * 4 + 3 = 3.8
y = 0.2 * 5 + 3 = 4.0
Single Dimension Linear Regression
• Single dimension linear regression has pairs of x and y values as input training samples.
• It uses these training sample to derive a line that predicts values of y.
• The training samples are used to derive the values of a and b that minimise the error
between actual and predicated values of y.
• We want a line that minimises the error between the Y values in training samples and
the Y values that the line passes through.
• Or put another way, we want the line that “best fits’ the training samples.
• So we define the error function for our algorithm so we can minimise that error.
Program:
Problem statement:
Theory:
• Each training sample has an x made up of multiple input values and a corresponding y
with a single value.
• The inputs can be represented as an X matrix in which each row is sample and
each column is a dimension.
• Our predicated y values are calculated by multiple the X matrix by a matrix of weights,
w.
• If there are 2 dimensions, then this equation defines plane. If there are more
dimensions then it defines a hyper-plane.
Program
Experiment No. 12
Problem Statement:
Theory:
Q.1 What is Decision Tree Algorithm?
A tree can be “learned” by splitting the source set into subsets based on an attribute value
test. This process is repeated on each derived subset in a recursive manner called
recursive partitioning. The recursion is completed when the subset at a node all has the
same value of the target variable, or when splitting no longer adds value to the
predictions. The construction of decision tree classifier does not require any domain
knowledge or parameter setting, and therefore is appropriate for exploratory knowledge
discovery. Decision trees can handle high dimensional data. In general decision tree
classifier has good accuracy. Decision tree induction is a typical inductive approach to
learn knowledge on classification.
In other words, we can say that decision tree represents a disjunction of conjunctions of
constraints on the attribute values of instances.
Q.3 What are the packages used in the Decision Tree Algorithm?
1. sklearn: -
2.NumPy: -
It is a numeric python module which provides fast math’s functions for calculations. It is
used to read data in numpy arrays and for manipulation purpose.
3.Pandas: -
Used to read and write different files. Data manipulation can be done easily with
dataframes.
2.Regression Trees.
Such a tree is built through a process known as binary recursive partitioning. This is an
iterative process of splitting the data into partitions, and then splitting it up further on
each of the branches.
Decision trees where the target variable can take continuous values (typically real
numbers) are called regression trees. (e.g. the price of a house, or a patient’s length of
stay in a hospital)
Q.5 What are the applications of Decision Tree in real life?
4. System Control.
import numpy as np
import pandas as pd
def importdata():
balance_data = pd.read_csv(
'https://archive.ics.uci.edu/ml/machine-learning-'
+ 'databases/balance-scale/balance-scale.data',
",balance_data.head())
return balance_data
def splitdataset(balance_data):
X = balance_data.values[:, 1:5]
Y = balance_data.values[:, 0]
X_train, X_test, y_train, y_test = train_test_split(
clf_gini = DecisionTreeClassifier(criterion =
"gini",
random_state = 100,max_depth=3,
return clf_gini
max_depth = 3, min_samples_leaf = 5)
clf_entropy.fit(X_train, y_train)
return clf_entropy
y_pred =
clf_object.predict(X_test)
print("Predicted values:")
print(y_pred)
return y_pred
confusion_matrix(y_test, y_pred))
accuracy_score(y_test,y_pred)*100)
print("Report : ",
classification_report(y_test,
y_pred))
def main():
data = importdata()
y_train)
y_pred_entropy = prediction(X_test,
clf_entropy) cal_accuracy(y_test,
y_pred_entropy)
main()
Output:
Data Infomation:
Dataset: 01234
0B 1 1 1 1
1R 1 1 1 2
2R 1 1 1 3
3R 1 1 1 4
4R 1 1 1 5
'L' 'R' 'L' 'R' 'L' 'L' 'R' 'L' 'L' 'L' 'R' 'L' 'L' 'L' 'R' 'L' 'L' 'L'
'L' 'R' 'L' 'L' 'R' 'L' 'R' 'L' 'R' 'R' 'L' 'L' 'R' 'L' 'R' 'R' 'L' 'R'
'R' 'L' 'R' 'R' 'L' 'L' 'R' 'R' 'L' 'L' 'L' 'L' 'L' 'R' 'R' 'L' 'L'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