Data Science Lab Python
Data Science Lab Python
This document contains all 10 experiments as per the syllabus for Semester III B.Tech in
Data Science.
🔹 Code:
# Example: File I/O
with open('sample.txt', 'w') as f:
f.write("Hello, Data Science Lab!")
🔹 Code:
> Try: Shortcut keys, Markdown + Code, variable watching.
🔹 Code:
# Bubble Sort Example
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
🔹 Code:
import matplotlib.pyplot as plt
x = ['A', 'B', 'C']
y = [10, 20, 15]
plt.bar(x, y)
plt.title('Bar Chart')
plt.show()
🔹 Code:
import pandas as pd
data = {'A': [10, 20, 30, 40]}
df = pd.DataFrame(data)
print("Mean:", df['A'].mean())
print("Std Dev:", df['A'].std())
🔹 Code:
import seaborn as sns
sns.histplot(df['A'], kde=True)
🔹 Code:
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
iris = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2)
model = SVC()
model.fit(X_train, y_train)
print("Accuracy:", model.score(X_test, y_test))
🔹 Code:
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
kmeans = KMeans(n_clusters=3)
kmeans.fit(iris.data)
plt.scatter(iris.data[:, 0], iris.data[:, 1], c=kmeans.labels_)
plt.title("K-Means Clustering")
plt.show()
🔹 Code:
import networkx as nx
G = nx.Graph()
G.add_edges_from([('A', 'B'), ('B', 'C'), ('C', 'A')])
nx.draw(G, with_labels=True)
🔹 Code:
print("Degree Centrality:", nx.degree_centrality(G))
print("PageRank:", nx.pagerank(G))