#program 1a
n=int(input("enter the number of elements in an integer list: "))
num_list=[]
print("enter the numbers in ascending order: ")
for i in range(0,n):
num_list.append(int(input()))
print(num_list)
element=int(input("enter the element to be searched: "))
low=0
high=n-1
mid=(low+high)//2
while low<=high:
if num_list[mid]==element:
print("element:", element, " found at location", mid+1)
break
elif num_list[mid]<element:
low=mid+1
else:
high=mid-1
mid=(low+high)//2
if low>high:
print("element: ", element, " was not found")
#program 1b
n=int(input("enter the number of elements in an integer list: "))
input_list=[]
for i in range(0,n):
input_list.append(int(input()))
print("original list: ", input_list)
for i in range(0,n):
for j in range(0, n-i-1):
if input_list[j]>input_list[j+1]:
temp=input_list[j]
input_list[j]=input_list[j+1]
input_list[j+1]=temp
print("sorted list: ", input_list)
#program 2a
n=int(input("enter the number of elements in the list: "))
input_list=[]
print("enter the elements of the list: ")
for i in range(0,n):
input_list.append(int(input()))
for i in range(n//2):
temp=input_list[i]
input_list[i]=input_list[n-i-1]
input_list[n-i-1]=temp
print(input_list)
#program 2b
input_sen=input('please enter the sentence: ').lower()
word=input_sen.split()
freqs={}
for w in word:
freqs[w]=[Link](w,0)+1
k=[Link]()
print("frequency list")
print("--------")
for key in k:
print("%s:%d"%(key,freqs[key]))
#program 3
input_word=input("enter a word:")
c=input("enter a character:")
count=0
for ch in input_word:
if c==ch:
count=count+1
print("character {} in the word {} is {} times.".format(c, input_word,
str(count)))
# program 4
import os
while True:
inp_file = input("Enter the name of the correct input file: ")
if [Link](inp_file):
fi = open(inp_file, "r")
break
else:
print(f"Input file '{inp_file}' doesn't exist.")
op_file = input("Enter the name of the output file: ")
fo = open(op_file, "w+")
data1 = [Link]()
print(f"Contents of input file '{inp_file}':")
for line in data1:
print([Link]())
[Link](line)
[Link]()
fo = open(op_file, "r")
data2 = [Link]()
[Link]()
print(f"Contents of output file '{op_file}':")
for word in data2:
print([Link]())
[Link]()
#program 5
import math
from collections import Counter
max_input=int(input("enter how many no.s:"))
x=[]
sum=0
sum_squares=0
variance=0.0
print("enter the no.s:")
for i in range (0, max_input):
[Link](int(input()))
print("the given list of number is:", x)
#mean calculation
for i in range(0, len(x)):
sum=sum+x[i]
mean=sum/max_input
print("mean:", mean)
#variance calculation
for i in range(0, max_input):
sum_squares=sum_squares+pow(x[i]-mean, 2)
variance=sum_squares/float(max_input-1)
print("variance:", variance)
#standard deviation calculation
std_dev=[Link](variance)
print("standard deviation:", std_dev)
#median calculation:
n=len(x)
[Link]()
if n%2==0:
m1=x[n//2]
m2=x[(n//2)-1]
median=(m1+m2)/2
else:
median=x[n//2]
print("the median of the list", x, "is", str(median))
#mode calculation
n=len(x)
val=Counter(x)
get_mode=dict(val)
mode=[i for i, v in get_mode.items() if v==max(list([Link]()))]
if len(mode)==n:
get_mode="no mode"
else:
get_mode="mode is:"+",".join(map(str,mode))
print(get_mode)
#program 6
from [Link] import randn
from [Link] import seed
from [Link] import pearsonr
import [Link]
import pandas as pd
import numpy as np
seed(1)
data1=20*randn(1000)+100
data2=data1+(10*randn(1000)+50)
corr,_=pearsonr(data1, data2)
print('persons correlation: %.3f'%corr)
def spearmans_rank_correlation(d1, d2):
d1rank=[Link](d1).rank()
d2rank=[Link](d2).rank()
return [Link](d1rank, d2rank)
result=spearmans_rank_correlation(data1, data2)
print("Rank correlation:", result)
print("Scipy rank correlation:", [Link](data1, data2)[0])
#program 7a
import numpy as np
arr1=[Link]([1,3,4])
print("array with rank 1:\n",arr1)
arr2=[Link]([[1,3,4],[4,5,6]])
print("array with rank 2:\n",arr2)
arr3=[Link]([1,8,8])
print("\n array created using passes tuple:",arr3)
#program 7b
import numpy as np
ai=[[1,2],[3,5]]
a=[Link](ai)
inv_a=[Link](a)
print("inverse of a :\n ",inv_a)
b=[Link]([[1],[3]])
x=[Link](inv_a,b)
print("result of ax=b using inverse of a:\n",x)
x2=[Link](a,b)
print("rsult of ax=b using [Link]:\n",x2)
#program 8
import pandas as pd
data={
"fruits":["apple","orange","grapes"],
"costs":[50,40,45],
"books":["wings of fire","harry potter","psychology"]
}
df=[Link](data)
print(df)
data_drop=[Link]("books",axis=1)
print(data_drop)
# PROGRAM 9 A
import matplotlib. pyplot as plt
Year = [1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010]
unemployment_rate = [9, 8, 12, 8, 7, 2, 6, 9, 7, 61]
[Link] (Year, unemployment_rate)
[Link](" unemployment Rate Vs Year")
[Link] ("Year")
[Link] ("unemployment Rate")
[Link]()
# program 9 B
import [Link] as plt
import numpy as np
year = [1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010]
unemployment_rate = [9, 8, 12, 8, 7, 2, 6, 9, 7, 6]
[Link](year, unemployment_rate, color="#A0F0F0", marker='o')
[Link]("Unemployment Rate vs Year", fontsize=14)
[Link]("Year")
[Link]("Unemployment Rate (%)")
[Link]()
# program 9 C
import [Link] as plt
x=[1,3,4,5,6]
y=[2,4,6,8,1]
[Link](x,y,color='red',linestyle='dashed',linewidth=3,marker='o',markerfa
cecolor='blue',markersize=12)
[Link](1,8)
[Link](1,8)
[Link]('x-axis')
[Link]('y-axis')
[Link]('some cool customizations')
[Link]()
# program 9 D
import [Link] as plt
x=[1,2,3,4,5,6]
y=[7,8,9,0,4,3]
[Link](x,y)
[Link]()
# program 9 E
import [Link] as plt
import numpy as np
a=[Link]([22,87,5,43,56,73,55])
fig,ax=[Link](figsize=(10,7))
[Link](a,bins=[0,25,50,75,100])
[Link]()
# program 9 F
import [Link] as plt
[Link]('classic')
%matplotlib inline
import numpy as np
x = [Link](0, 2 * [Link], 100)
y = [Link](x[:, [Link]] + [Link] * [Link](0, 2, 0.5))
lines = [Link](x, y)
[Link](lines[:2], ['first', 'second'])
[Link]() # Added parentheses to call the function
# program 9 g
import [Link] as plt
import numpy as np
[Link](10)
data=[Link](100,20,200)
fig=[Link](figsize=(10,7))
[Link](data)
[Link]()
# program 9 h
import [Link] as plt
import numpy as np
fig, ax = [Link]()
lines = []
styles = ['-', '--', '-.', ':']
x = [Link](0, 10, 1000)
for i in range(4):
lines += [Link](x, [Link](x + i * [Link] / 2), styles[i], color='red') # Fixed
'x=i*[Link]/2'
# Set axis properties
[Link]('equal')
# Add first legend
[Link](lines[:2], ['Line A', 'Line B'], loc='upper right', frameon=False)
# Add second legend
from [Link] import Legend
leg = Legend(ax, lines[2:], ['Line C', 'Line D'], loc='lower right',
frameon=False)
ax.add_artist(leg)
# Display the plot
[Link]()
# program 9 i
import [Link] as plt
import numpy as np
# Create subplots
fig, axes = [Link](nrows=2, ncols=2)
# Loop through each subplot
for ax in [Link]:
# Display a random 10x10 image with imshow
im = [Link]([Link]((10, 10)), vmin=0, vmax=1) #
Fixed 'Vmin' and 'Vmax' to 'vmin' and 'vmax'
# Add a single colorbar for all subplots
[Link](im, ax=[Link]().tolist(), shrink=0.8)
# Show the plot
[Link]()
# program 9 j
%matplotlib inline
import [Link] as plt
import matplotlib as mpl
[Link]('ggplot')
import numpy as np
import pandas as pd
# Load the data
births = pd.read_csv('[Link]')
# Calculate quartiles and filter outliers
quartiles = [Link](births['births'], [25, 50, 75])
mu, sig = quartiles[1], 0.74 * (quartiles[2] - quartiles[0])
births = [Link]('(births > @mu - 5 * @sig) & (births < @mu + 5 *
@sig)')
# Convert day column to integer and create datetime index
births['day'] = births['day'].astype(int)
[Link] = pd.to_datetime(10000 * [Link] + 100 * [Link] +
[Link], format='%Y%m%d')
# Create pivot table
births_by_date = births.pivot_table('births', [[Link],
[Link]])
# Fix the index to a specific year (e.g., 2012) to avoid leap year issues
births_by_date.index = [[Link](2012, month, day) for (month, day)
in births_by_date.index]
# Plot the data
fig, ax = [Link](figsize=[12, 4])
births_by_date.plot(ax=ax)
[Link]()
# program 10 a
import pandas as pd
data={'gender':['male','female','male'],'race':['group D','group C','group
none'],'parental level of education':['associate','bachelor','high
school'],'write score':[12,23,45]}
df=[Link](data)
print([Link]())
dropped_df=[Link]([1,2],axis=0)
print(dropped_df)
df['write score']=df['write score']
print(df)
# program 10 b
import pandas as pd
from [Link] import LabelEncoder,OneHotEncoder
df=[Link]([['male','group D','assosciate',12],['femal','group
C','bachelor',23],['male','none','high school',45]])
[Link]=['gender','race','parental level of education','write score']
print(df)
race_mapping={'group D':3,'group C':2,'none':1}
df['race']=df['race'].map(race_mapping)
print(df)
class_le=LabelEncoder()
df['write score']=class_le.fit_transform(df['write score'].values)
df['gender']=class_le.fit_transform(df['gender'].values)
print(df)
x=df[['gender','race','parental level of education','write score']].values
color_le=LabelEncoder()
x[:,3]=color_le.fit_transform(x[:,3])
print(x)
one_hot=OneHotEncoder()
print(one_hot.fit_transform(x).toarray())
pd.get_dummies(df[['gender','race','parental level of education','write
score']])
# program 10 c (i)
import pandas as pd
import numpy as np
import [Link] as plt
from [Link] import MinMaxScaler
%matplotlib inline
# Create DataFrame
df = [Link]({'weight': [15, 18, 12, 10], 'price': [1, 3, 2, 5]},
index=['orange', 'apple', 'banana', 'grape'])
print(df)
# Scale the data
scaler = MinMaxScaler()
df1 = [Link](scaler.fit_transform(df), columns=['weight', 'price'],
index=['orange', 'apple', 'banana', 'grape'])
# Plot before and after scaling
ax = [Link](x='weight', y='price', color=['red', 'green', 'blue',
'yellow'], marker='*', s=80, label='before scaling')
[Link](x='weight', y='price', color=['red', 'green', 'blue', 'yellow'],
marker='o', s=60, label='after scaling', ax=ax)
# Add horizontal and vertical lines
[Link](0, color='red', alpha=0.2)
[Link](0, color='red', alpha=0.2)
[Link]()
# program 10 c (ii)
import pandas as pd
import numpy as np
import [Link] as plt
from [Link] import StandardScaler
%matplotlib inline
# Create the dataframe
df = [Link]({'weight': [15, 18, 12, 10], 'price': [1, 3, 2, 5]},
index=['orange', 'apple', 'banana', 'grape'])
print(df)
# Apply Standard Scaling
scaler = StandardScaler()
df1 = [Link](scaler.fit_transform(df), columns=['weight', 'price'],
index=['orange', 'apple', 'banana', 'grape'])
# Plotting
ax = [Link](x='weight', y='price', color='blue', marker='*', s=80,
label='BEFORE SCALING')
[Link](x='weight', y='price', color='green', marker='o', s=60,
label='AFTER SCALING', ax=ax)
# Adding lines at 0 for scaled plot reference
[Link](0, color='red', alpha=0.2)
[Link](0, color='red', alpha=0.2)
[Link]()
# program 11
import pandas as pd
import numpy as np
import [Link] as plt
from [Link] import load_digits # Corrected here
from [Link] import PCA # Corrected typo here
# Load digits dataset
digits = load_digits()
data = [Link]
print([Link])
# Reshape a sample image and display it
image_sample = data[1, :].reshape(8, 8) # Corrected typo here
[Link](image_sample, cmap='gray')
[Link]("Sample Image")
[Link]()
# Apply PCA
pca = PCA(n_components=2) # Corrected case typo
converted_data = pca.fit_transform([Link])
print(converted_data.shape)
# Display the same image after PCA processing
[Link](image_sample, cmap='gray') # Image sample doesn't change
from PCA; for visualization
[Link]("Same Image after PCA Transformation")
[Link]()
# program 12
# program 13
# program 14