You are on page 1of 8

1.

Standard deviation of speed of cars


import numpy as np

speed = [52,111,138,78,59,77,97,67,98,102]

x = np.std(speed)

print('Standard deviation of speed of cars is : ',x)

Standard deviation of speed of cars is : 24.9096366894421

2. Percentile of marks of students


from scipy import stats
import numpy as np

arr = [20, 2, 7, 1, 7, 7, 34]

print ("\nPercentile of 7 : ", stats.percentileofscore(arr, 7))

print ("\nPercentile of 34 : ", stats.percentileofscore(arr, 34))

print ("\nPercentile of 2 : ", stats.percentileofscore(arr, 2))

Percentile of 7 : 57.142857142857146

Percentile of 34 : 100.0

Percentile of 2 : 28.571428571428573

3. Histogram for normal distribution

import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt

# Generate some data for this


# demonstration.
#np.random.seed(50)
data = np.random.normal(170, 10, 250)

# Fit a normal distribution to


# the data:
# mean and standard deviation
mu, std = norm.fit(data)
# Plot the histogram.
plt.hist(data, bins=25, density=True, alpha=0.8, color='m')

# Plot the PDF.


xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax)
p = norm.pdf(x, mu, std)

plt.plot(x, p)
title = "Fit Values: {:.2f} and {:.2f}".format(mu, std)
plt.title(title)

4. Scatter plot
import matplotlib.pyplot as plt
x =[15,57, 28, 37, 42, 17, 52, 19, 34, 11, 12, 59, 36]
y =[99, 86, 87, 88, 100, 86, 103, 87, 94, 78, 77, 85, 86]
plt.scatter(x,y,c='b')
plt.title("Scatter plot")
plt.show()
5. Polynomial regression

import numpy
import matplotlib.pyplot as plt

x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y = [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]

mymodel = numpy.poly1d(numpy.polyfit(x, y, 3))

myline = numpy.linspace(1, 22, 100)

plt.scatter(x, y)
plt.plot(myline, mymodel(myline))
plt.show()
6. Decision tree
from sklearn.datasets import load_iris
iris = load_iris()
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier

X = iris.data
y = iris.target

from sklearn.tree import plot_tree


clf = DecisionTreeClassifier().fit(X,y)
plot_tree(clf)
plt.title('Decision tree for iris data set')
plt.show()
7. Create and insert values in a table

import sqlite3
conn = sqlite3.connect('Bank.db')
c = conn.cursor()

def create_table():
c.execute('CREATE TABLE IF NOT EXISTS customer_table (Number int, Name TEXT)')
print('Table created')
print()

def data_entry(number,name):
c.execute("INSERT INTO customer_table(Number, Name) VALUES(?, ?)", (number, name))

def select_query():
c.execute("SELECT * FROM customer_table ")
myresult = c.fetchall()
for x in myresult:
print(x)
create_table()
data_entry(1234, 'Anu')
data_entry(1345,'Jeya lakshmi')
data_entry(1456,'Kumari')
select_query()
c.close()
conn.close()

8. Query for retrieving relevant information from a table

import sqlite3

conn = sqlite3.connect('inventory.db')
c = conn.cursor()

def create_table():
c.execute('CREATE TABLE IF NOT EXISTS stock_table (Number int, Name text, Price real)')

def data_entry(number,name, price):


c.execute("INSERT INTO stock_table(Number, Name, Price ) VALUES(?, ?, ?)", (number, name,price))

def select_query():
c.execute("SELECT * FROM stock_table where price >4500")
myresult = c.fetchall()
for x in myresult:
print(x)

create_table()
data_entry(1234, 'table',3450)
data_entry(1345,’chair’,4500)
data_entry(1456,'teepoy’,5000)
select_query()

c.close()
conn.close()

9. Delete records from the table


import sqlite3

# connect to database
con = sqlite3.connect('college.db')

c = con.cursor()
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS student (Number int, Name text, Mark int)')
print('Table created')
print()

def data_entry(number,name,mark):
c.execute("INSERT INTO student(Number, Name,Mark) VALUES(?, ?, ?)", (number, name, mark))

def select_query():
c.execute("SELECT * FROM student")
myresult = c.fetchall()
for x in myresult:
print(x)

def delete_table():
c.execute("DROP TABLE student")
print('Table dropped')

create_table()
data_entry(1234, 'Anu',67)
data_entry(1345,'Jeya lakshmi',80)
data_entry(1456,'Kumari',70)
select_query()
print()
delete_table()
select_query()
c.close()
con.close()
cur = con.cursor()

10.Update values in the table

import sqlite3

conn = sqlite3.connect('inventory1.db')
c = conn.cursor()

def create_table():
c.execute('CREATE TABLE IF NOT EXISTS stock_table (Number int, Name TEXT, Price real)')

def data_entry(number,name, price):


c.execute("INSERT INTO stock_table(Number, Name, Price ) VALUES(?, ?, ?)", (number, name,price))

def select_query():
c.execute("SELECT * FROM stock_table ")
myresult = c.fetchall()
for x in myresult:
print(x)

def update_table():
c.execute("UPDATE stock_table SET name = 'grinder' WHERE Number =1456")

create_table()
data_entry(1234, 'chair',345)
data_entry(1345,'table',4500)
data_entry(1456,'mixie',5000)
print()
print("Before updation")
select_query()
print()
update_table()
print("After updation")
select_query()
c.close()
conn.close()

You might also like