Python Programming Tutorial: Data Structures, Algorithms, and Libraries

Python Programming Tutorial

Data Structures and Algorithms

Linear Search

def linearSearch(array, n, x):
for i in range(0, n):
if (array[i] == x):
return i
return -1
array = [2, 4, 0, 1, 9]
x = eval(input("enter the element to be searched: "))
n = len(array)
result = linearSearch(array, n, x)
if(result == -1):
print("Element not found")
else:
print("Element found at index: ", result)

Insertion Sort

import bisect
def insert(list, n):
bisect.insort(list, n)
return list
# Driver function list = [1, 2, 4]
n = eval(input("enter the value to be inserted "))
bisect.insort(list, n)
print(list)

Object-Oriented Programming

Class Inheritance

class Base:
def __init__(self):
self.a = 10
self._b = 20
def display(self):
print(" the values are :")
print(f"a={self.a} b={self._b}")
class Derived(Base):
def __init__(self):
Base.__init__(self)
self.d = 30
def display(self):
Base.display(self)
print(f"d={self.d}")
def __add__(self, ob):
return self.a + ob.a+self.d + ob.d
obj2 = Derived()
obj3 = Derived()
obj2.display()
obj3.display()
print("\n Sum of two objects :",obj2 + obj3)

Data Analysis with Pandas

Data Cleaning and Preprocessing

import pandas as pd
cars_data=pd.read_csv("Toyota.csv")
cars_data.head()
cars2 =cars_data.copy()
cars3 =cars_data.copy()
cars2 = cars_data.drop(['Doors','Weight'],axis='columns')
cars2.shape
cars2.isna().sum()
missing = cars2[cars2.isnull().any(axis=1)]
missing
cars2.describe()
cars2['Age'].mean()
cars2['Age'].fillna(cars2['Age'].mean(), inplace = True)
cars2.isna().sum()
cars2['FuelType'].value_counts()
cars2['FuelType'].value_counts().index[0]
cars2['FuelType'].fillna(cars2['FuelType'].value_counts().index[0], inplace = True)
cars2['MetColor'].mode()
cars2['MetColor'].fillna(cars2['MetColor'].mode()[0], inplace = True)
cars2.isna().sum()
print(cars_data.shape)
print(cars_data.index)
cars_data.ndim

Data Manipulation with NumPy

Array Operations
import numpy as np
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[7, 8, 9], [10, 11, 12]])
print("concatenating two arrays \n",np.concatenate([arr1, arr2], axis=1))
print("vertical stacking \n",np.vstack((arr1, arr2)))
print("horizontal stacking \n",np.hstack((arr1,arr2)))
arr = np.array([1, 2, 3, 4, 5, 4, 4])
x = np.where(arr == 4)
print(x)
a = np.array([[1,4],[3,1]])
print("sorted array : ",np.sort(a))
print("\n sorted flattened array:", np.sort(a, axis=0))
x = np.array([3, 1, 2])
print("\n indices that would sort an array",np.argsort(x))
print("\n sorting complex number :" ,np.sort_complex([5, 3, 6, 2, 1]))
Array Splitting
import numpy as np
x = np.arange(9.0)
print(np.split(x, 3))
print(np.split(x, [3, 5, 6, 10]))
x = np.arange(9)
np.array_split(x, 4)
equal division cannot be made.
a = np.array([[1, 3, 5, 7, 9, 11],
[2, 4, 6, 8, 10, 12]])
print("Splitting along horizontal axis into 2 parts:\n", np.hsplit(a, 2))
print("\nSplitting along vertical axis into 2 parts:\n", np.vsplit(a, 2))

Data Visualization with Matplotlib

Line Plot

import matplotlib.pyplot as plt
x1 = [1,2,3,4,5] y1 = [2,5,2,6,8]
x2 = [1,2,3,4,5] y2 = [4,5,8,9,10]
plt.xlabel("X Axis",fontsize=12,fontstyle='italic')
plt.ylabel("Y Axis",fontsize=12)
plt.title("Line Plot",fontsize=15,fontname='DejaVu Sans')
plt.plot(x1,y1,color="red",label="First Graph")
plt.plot(x2,y2,color="blue",label="Second Graph")
plt.legend(loc=2)
plt.show()

Bar Plot

x = ['A',"B","C","D","E"]
y = [20,50,20,60,80]
plt.xlabel("X Axis",fontsize=12)
plt.ylabel("Y Axis",fontsize=12)
plt.title("Bar Plot",fontsize=15)
plt.bar(x,y,color="red",width=0.5)
plt.show()

Scatter Plot

x1 = [1,2,3,4,5] y1 = [2,5,2,6,8]
x2 = [1,2,3,4,5] y2 = [4,5,8,9,10]
plt.xlabel("X Axis",fontsize=12,fontstyle='italic')
plt.ylabel("Y Axis",fontsize=12)
plt.title("Scatter Plot",fontsize=15,fontname='Courier')
plt.scatter(x1,y1,color="red",label="First Graph")### line plot
plt.scatter(x2,y2,color="blue",s=120,marker='*',label="Second Graph")
plt.legend(loc=2)
plt.show()

Histogram

import numpy as np
sample = np.random.randint(10,100,30)
plt.hist(sample,rwidth=0.7)
plt.show()

Pie Chart

plt.figure(figsize=(7,7))
slices = [10,20,50,30,34]
act = ["A","B","C","D","E"]
cols = ["red","blue","green","pink","yellow"]
plt.pie(slices,labels=act,colors=cols,
autopct="%1.2f%%",explode=(0,0.2,0,0.1,0))
plt.show()

NumPy Array Attributes

import numpy as np
a = np.array([[12,4,5],[23,45,66],[45,34,23]])
print("Printing Array")
print()
print(a)
print()
print("Printing numpy array Attributes")
print("1>. Array Shape is: ", a.shape)
print("2>. Array dimensions are ", a.ndim)
print("3>. Datatype of array is ", a.dtype)
print("4>. Length of each element of array in bytes is ", a.itemsize)
print("5>. Number of elements in array are ", a.size)

Machine Learning with Scikit-learn

Linear Regression

import numpy as np
from sklearn.linear_model import LinearRegression
X = np.array([[1, 2], [2, 4], [3, 6], [4, 8], [5, 10]])
y = np.array([2, 4, 6, 8, 10])
model = LinearRegression()
model.fit(X, y)
print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)
new_data = np.array([[6, 12], [7, 14]])
predictions = model.predict(new_data)
print("Predictions:", predictions)

Linear Regression Visualization

import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
X = np.array([[1, 2], [2, 4], [3, 6], [4, 8], [5, 10]])
y = np.array([2, 4, 6, 8, 10])
model = LinearRegression()
model.fit(X, y)
print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)
plt.scatter(X[:, 0], y, color='blue')
plt.plot(X[:, 0], model.predict(X), color='red')
plt.title("Linear Regression Model")
plt.xlabel("X")
plt.ylabel("y")
plt.show()

Logistic Regression

import numpy as np
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
X = np.array([[1, 2], [2, 4], [3, 6], [4, 8], [5, 10]])
y = np.array([0, 0, 0, 1, 1])
model = LogisticRegression()
model.fit(X, y)
print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)
new_data = np.array([[6, 12], [7, 14]])
predictions = model.predict(new_data)
print("Predictions:", predictions)
plt.scatter(X[:, 0], y, color='blue')
plt.plot(X[:, 0], model.predict(X), color='red')
plt.title("Logistic Regression Model")
plt.xlabel("X")
plt.ylabel("y")
plt.show()

Time Series Analysis with Pandas

Date and Time Indexing

import pandas as pd
df = pd.read_csv("aapl.csv",parse_dates=["Date"], index_col="Date")
df.head()
df.index
df.loc['2017-06-30']
df.loc["2017-01"]
df.loc['2017-06'].head()
df.loc['2017-06'].Close.mean()
df.loc['2017'].head(2)
df['2017-01-08':'2017-01-03']
df.loc['2017-01']
df['Close'].resample('M').mean().head()
df.loc['2016-07']
%matplotlib inline
df['Close'].plot()
df['Close'].resample('M').mean().plot(kind='bar')

Data Visualization with Seaborn

Regression Plots

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
cars_data = pd.read_csv('Toyota.csv', index_col=0, na_values=["??","????"])
cars_data.dropna(axis=0, inplace= True)
sns.set(style="darkgrid")
sns.regplot(x=cars_data['Age'],y= cars_data['Price'])
plt.show()
sns.regplot(x=cars_data['Age'],y= cars_data['Price'],marker='*' ,fit_reg=False)
plt.show()
sns.lmplot(x='Age',y='Price', data=cars_data, fit_reg=False, hue="FuelType", l
egend=True, palette="Set1")
plt.show()

Distribution Plots

sns.distplot(cars_data['Age'])
plt.show()
sns.distplot(cars_data['Age'],kde=False, bins = 8)
plt.show()

Categorical Plots

sns.countplot(x="FuelType", data=cars_data)
plt.show()
sns.countplot(x="FuelType", data=cars_data, hue="Automatic")
plt.show()

Box Plots

sns.boxplot(y="Price", data=cars_data)
plt.show()
sns.boxplot(x= cars_data["FuelType"], y=cars_data["Price"])
plt.show()
sns.boxplot(x= cars_data["FuelType"], y=cars_data["Price"], hue="Automatic", d
ata= cars_data)
plt.show()

NumPy Broadcasting

import numpy as np            5-2
v = np.array([1, 2, 3])
w = np.array([4, 5])
print("v = ",v)
print("w = ",w)
print("\n outer product of v and w is :\n")
print(np.reshape(v, (3, 1)) * w)
X = np.array([[1, 2, 3], [4, 5, 6]])
print("\n X = ",X)
print("\n v = ",v)
print("\n X + v = ",X + v)
print("\n Transposing this final result :")
print((X.T + w).T)
print("\n X+ np.reshape(w, (2, 1))")
print(X+ np.reshape(w, (2, 1)))
print(X * 2)

Plotting Sine and Cosine Functions

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])
plt.show()