Data Science With Python
Data Science With Python
LAB-01
Basics, Loops, Functions, Classes and Objects of Python
Output:
#if
a=15
if a>10:
print("Value of a is greater than 10")
#if else
if (a<10):
print("Value of a is less than 10")
else:
print("Value of a is greater than 10")
#elif
ef=-6
if ef>0:
print("Positive +ve")
elif ef<0:
print("Negative -ve")
else:
print("Zero")
#nested
c=18
if (c>10):
if (c>20):
print("The c is greater than 20")
else:
print("THE c is less than 20 and greater than 10")
Output:
#while
rk=3
while rk>0:
print(rk)
rk=rk-1
#for
for i in range(3):
print(i)
Output:
print("Multiplication Table:")
for row in range(1, 4): # Rows from 1 to 3
for col in range(1, 4): # Columns from 1 to 3
print(f"{row} x {col} = {row * col}", end=" ")
print()
print('\n')
Output:
Output:
#lists
n_l=[1,2,3,[4,8,10,19]]
print(n_l)
li=[1,2,3]
print(li)
li.append(4)
print(li)
m=[5,6,7]
li.extend(m)
print(li)
print(5 in li)
print(90 not in li)
li.remove(3)
print(li)
po=li.pop()
print(li)
print(po)
Data Science With Python Laboratory 3
R. RADHA KRISHNA N2000157
del li[0]
print(li)
li.clear()
print(li)
#Tuples
t1=(1,2,3)
t2=(4,5,6)
print(t1+t2)
print(t1)
Output:
Output:
#break
for i in range(1,10):
if i==5:
break
print(i)
#contine
for i in range(1,6):
if i==3:
continue
print(i)
#pass
for i in range(4):
if i==3:
pass
print(i)
Output:
Output:
print(a==b)
print(a!=b)
Output:
Output:
print(a^b)
print(a>>2)
print(b<<3)
Output:
Output:
s="RK "
print('z' in s)
print('k' not in s)
Output:
print(a is b)
print(a is not b)
Output:
Output:
s={1,2,3,4}
print(s)
l=set(['a','b','c',1,2,3,4])
print(l)
s.add(78)
print(s)
s.update((4,5))
print(s)
s.discard(3)
print(s)
print(s.union(l))
print(s&l)
print(l-s)
print(l^s)
Output:
result = add_numbers(5, 3)
print(f"Sum: {result}")
Output:
def sum_of_digits(n):
if n < 10:
return n
else:
return n % 10 + sum_of_digits(n // 10)
number = 1234
result = sum_of_digits(number)
print(f"Sum of the digits of {number}: {result}")
Output:
Output:
class Movie:
name = ''
rating = ''
def show_details(self):
print("Movie name:", self.name)
print("Rating:", self.rating)
movie = Movie()
movie.name = "Gabbar Singh"
movie.rating = "8/10"
movie.show_details()
Output:
class Movie:
def __init__(self, name, rating):
self.name = name
self.rating = rating
def show_details(self):
print("Movie name:", self.name)
print("Rating:", self.rating)
Output:
class Movie:
def __init__(self, name, price):
self.name = name
self.price = price
def show_details(self):
print("Movie name:", self.name, "and price:", self.price)
class PawanKalyanMovie(Movie):
def add_rating(self, rating):
self.rating = rating
def show_details(self):
print("Movie name:", self.name, "and price:", self.price, "rating:", self.rating)
Data Science With Python Laboratory 10
R. RADHA KRISHNA N2000157
Output:
class Movie:
def __init__(self, name, price):
self.name = name
self.price = price
def show_details(self):
print("Movie name:", self.name, "and price:", self.price)
class PawanKalyanMovie(Movie):
def show_details(self):
print("Movie name:", self.name, "and price:", self.price)
Output:
class Movie:
def __init__(self, name, price):
self.name = name
self.price = price
def show_details(self):
pass
class PawanKalyanMovie(Movie):
def show_details(self):
print("Movie name:", self.name, "and price:", self.price)
LAB -02
Files And Regular Expressions
Output:
if match:
print("Pattern found:", match.group())
Output:
LAB -03
Numpy , Pandas , Data Preprocessing And Web Scraping
import numpy as np
# Creating an array
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Concatenating arrays
arr2 = np.array([[7, 8, 9]])
concatenated_arr = np.concatenate((arr, arr2), axis=0)
print("Concatenated array:")
print(concatenated_arr)
Output:
import pandas as pd
# Creating a Series
data_series = {'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10],
'C': [11, 12, 13, 14, 15]}
series = pd.Series(data_series['A'])
# Series methods
print("Series Methods:")
print("----------------")
# 1. head()
print("1. head():")
print(series.head())
print()
# 2. tail()
print("2. tail():")
print(series.tail())
print()
# 3. describe()
print("3. describe():")
print(series.describe())
print()
# 4. value_counts()
print("4. value_counts():")
print(series.value_counts())
print()
# 5. sum()
print("5. sum():", series.sum())
# 6. mean()
print("6. mean():", series.mean())
# 7. std()
print("7. std():", series.std())
# 8. min()
print("8. min():", series.min())
# 9. max()
print("9. max():", series.max())
# 10. median()
print("10. median():", series.median())
# 11. idxmax()
print("11. idxmax():", series.idxmax())
# 12. idxmin()
print("12. idxmin():", series.idxmin())
# 13. sort_values()
print("13. sort_values():")
print(series.sort_values())
print()
Data Science With Python Laboratory 16
R. RADHA KRISHNA N2000157
# 14. sort_index()
print("14. sort_index():")
print(series.sort_index())
print()
# 15. apply()
print("15. apply():")
print(series.apply(lambda x: x**2))
print()
# Creating a DataFrame
data_df = {'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10],
'C': [11, 12, 13, 14, 15]}
df = pd.DataFrame(data_df)
# DataFrame methods
print("DataFrame Methods:")
print("-------------------")
# 1. head()
print("1. head():")
print(df.head())
print()
# 2. tail()
print("2. tail():")
print(df.tail())
print()
# 3. describe()
print("3. describe():")
print(df.describe())
print()
# 4. info()
print("4. info():")
print(df.info())
print()
# 5. shape
print("5. shape:", df.shape)
# 6. columns()
print("6. columns():", df.columns)
# 7. index()
print("7. index():", df.index)
# 8. mean()
print("8. mean():")
print(df.mean())
print()
Data Science With Python Laboratory 17
R. RADHA KRISHNA N2000157
# 9. std()
print("9. std():")
print(df.std())
print()
# 10. min()
print("10. min():")
print(df.min())
print()
# 11. max()
print("11. max():")
print(df.max())
print()
# 12. median()
print("12. median():")
print(df.median())
print()
# 13. corr()
print("13. corr():")
print(df.corr())
print()
# 14. dropna()
print("14. dropna():")
print(df.dropna())
print()
# 15. fillna()
print("15. fillna():")
print(df.fillna(0))
Output:
import requests
try:
url="https://www.4icu.org/in/a-z/"
r=requests.get(url)
print(r.status_code)
except:
print(r.status_code)
print("rejected")
pass
from bs4 import BeautifulSoup
soup = BeautifulSoup(r.text, 'html.parser')
college_List["location"].append(None)
college_List
import pandas as pd
print(len(college_List['rank']))
print(len(college_List['name']))
print(len(college_List['location']))
data= pd.DataFrame(college_List)
data
Output:
LAB-04
Linear ,Multiple and Polynomial Regression
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
data = pd.read_csv('data.csv')
X = data[['X']].values # Ensure X is in the right shape for scikit-learn
y = data['y'].values
model = LinearRegression()
model.fit(X, y)
y_predict = model.predict(X)
residuals = y - y_predict
plt.scatter(X, residuals, color='blue', label='Residuals')
plt.axhline(y=0, color='red', linestyle='--', linewidth=2)
plt.xlabel('X')
plt.ylabel('Residuals')
plt.title('Residual Plot')
plt.legend()
plt.show()
print(f'Intercept: {model.intercept_}')
print(f'Slope: {model.coef_[0]}')
Output:
np.random.seed(0)
X = 2 * np.random.rand(100, 1)
y = 2 * X + 9+np.random.randn(100, 1)
y_pred = model.predict(X)
# Plotting the regression plot using Seaborn
sns.regplot(x=X.flatten(), y=y.flatten(),scatter_kws={'color': "blue"}, line_kws={'color': 'yellow'})
plt.xlabel('X')
plt.ylabel('y')
plt.title('Regression Plot')
plt.show()
Output:
Output:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
data = pd.read_csv('data.csv')
X = data[['X']].values y = data['y'].values
model = LinearRegression()
model.fit(X, y)
y_predict = model.predict(X)
# Plotting the results
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, y_predict, color='red', linewidth=2, label='Regression line')
plt.xlabel('X')
plt.ylabel('y')
plt.title('Linear Regression Example')
plt.legend()
plt.show()
# Printing the model parameters
print(f'Intercept: {model.intercept_}')
print(f'Slope: {model.coef_[0]}')
Output:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
np.random.seed(0)
X = 5* np.random.rand(100, 1)
y = 8* X+np.random.randn(100,1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
poly_features = PolynomialFeatures(degree=5)
X_train_poly = poly_features.fit_transform(X_train)
X_test_poly = poly_features.transform(X_test)
model = LinearRegression()
model.fit(X_train_poly, y_train)
y_pred = model.predict(X_test_poly)
X_plot = np.linspace(0, 2, 100).reshape(-1, 1)
X_plot_poly = poly_features.transform(X_plot)
y_plot = model.predict(X_plot_poly)
plt.scatter(X, y, label='Data')
plt.plot(X_plot, y_plot, color='green', label='Polynomial Regression')
plt.xlabel('X')
plt.ylabel('y')
plt.title('Polynomial Regression')
plt.legend()
plt.show()
Output:
LAB-05
Data Visualization: Visualizing Data using different type of plotting techniques
Output:
#multibar
import matplotlib.pyplot as plt
import numpy as np
pk_movies = ['Jalsa', 'Kushi', 'Thammudu', 'Badri', 'Gabbar Singh', 'Attarintiki Daredi', 'Sardaar
Gabbar Singh', 'Katamarayudu', 'Agnyaathavaasi', 'Vakeel Saab']
child_ratings = [8.5, 8.0, 7.5, 7.0, 8.7, 9.2, 7.8, 7.3, 6.5, 8.9]
old_ratings = [7.5, 7.0, 6.5, 6.0, 7.7, 8.2, 6.8, 6.3, 5.5, 7.9]
bar_width = 0.35
index = np.arange(len(pk_movies))
plt.xlabel('Movies')
plt.ylabel('Ratings')
plt.title('Pawan Kalyan Movies and Ratings by Audience Type')
plt.xticks(index + bar_width / 2, pk_movies, rotation=45)
plt.legend()
plt.tight_layout()
plt.show()
Output:
Output:
Output:
LAB-06
Model Evaluation
Output: