0% found this document useful (0 votes)
15 views

Advance Python Lab Solution

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Advance Python Lab Solution

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

‭ 1) Write a program to calculate mean median and mode using numpy()‬

Q
‭method.‬
‭Ans:-‬
‭import numpy as np‬
‭data = np.array([1, 2, 3, 4, 5, 5, 6, 7, 8, 9])‬
‭# Mean‬
‭mean = np.mean(data)‬
‭# Median‬
‭median = np.median(data)‬
‭# Mode‬
‭mode = np.argmax(np.bincount(data))‬
‭print("Mean:", mean)‬
‭print("Median:", median)‬
‭print("Mode:", mode)‬
‭OUTPUT:-‬
‭Mean: 5.0‬
‭Median: 5.0‬
‭Mode: 5‬

‭ 2) write a python program to calculate the standard deviation.‬


Q
‭Ans:‬
‭import numpy as np‬
‭data = np.array([1, 2, 3, 4, 5, 5, 6, 7, 8, 9])‬
‭# Standard deviation‬
‭std_dev = np.std(data)‬
‭print("Standard Deviation:", std_dev)‬
‭OUTPUT:-‬
‭Standard Deviation: 2.581988897471611‬

‭ 3) Write programs to perform matplotlib in python with examples:‬


Q
‭(a) . Matplotlib Line‬
‭(b). Matplotlib Grid‬
‭(c). Matplotlib Bar‬
‭(d). Matplotlib Scatter‬
‭(e). Matplotlib Histogram.‬
‭Ans:-‬

(‭ a) . Matplotlib Line‬
‭import matplotlib.pyplot as plt‬
‭x = np.arange(0, 10, 0.1)‬
‭y = np.sin(x)‬
‭plt.plot(x, y)‬
‭plt.title('Matplotlib Line Example')‬
‭plt.xlabel('x')‬
‭ lt.ylabel('y')‬
p
‭plt.show()‬

(‭ b). Matplotlib Grid‬


‭plt.plot(x, y)‬
‭plt.title('Matplotlib Grid Example')‬
‭plt.xlabel('x')‬
‭plt.ylabel('y')‬
‭plt.grid(True)‬
‭plt.show()‬

‭(c). Matplotlib Bar‬

‭ = ['A', 'B', 'C', 'D']‬


x
‭y = [10, 20, 15, 25]‬

‭ lt.bar(x, y)‬
p
‭plt.title('Matplotlib Bar Example')‬
‭plt.xlabel('Categories')‬
‭plt.ylabel('Values')‬
‭plt.show()‬

‭(d). Matplotlib Scatter‬

‭ = np.random.rand(50)‬
x
‭y = np.random.rand(50)‬
‭colors = np.random.rand(50)‬
‭sizes = 1000 * np.random.rand(50)‬

‭ lt.scatter(x, y, c=colors, s=sizes, alpha=0.5)‬


p
‭plt.title('Matplotlib Scatter Example')‬
‭plt.xlabel('X')‬
‭plt.ylabel('Y')‬
‭plt.show()‬

‭(e). Matplotlib Histogram.‬

‭data = np.random.normal(0, 1, 1000)‬

‭ lt.hist(data, bins=30)‬
p
‭plt.title('Matplotlib Histogram Example')‬
‭ lt.xlabel('Value')‬
p
‭plt.ylabel('Frequency')‬
‭plt.show()‬

‭Q4) Write a program to implement linear regression using python.‬


‭Ans:-‬
‭from sklearn.linear_model import LinearRegression‬
‭X = np.array([[1], [2], [3], [4]])‬
‭y = np.array([2, 4, 6, 8])‬
‭model = LinearRegression()‬
‭model.fit(X, y)‬
‭predicted = model.predict([[5]])‬
‭print("Predicted value for 5:", predicted[0])‬
‭OUTPUT:-‬
‭Predicted value for 5: 10.0‬

‭Q5) Write a program to implement Polynomial regression using python.‬


‭Ans:-‬
‭from sklearn.preprocessing import PolynomialFeatures‬
‭from sklearn.linear_model import LinearRegression‬
‭X = np.array([[1], [2], [3], [4]])‬
‭y = np.array([2, 4, 9, 16])‬
‭poly = PolynomialFeatures(degree=2)‬
‭X_poly = poly.fit_transform(X)‬
‭model = LinearRegression()‬
‭model.fit(X_poly, y)‬
‭predicted = model.predict(poly.transform([[5]]))‬
‭print("Predicted value for 5:", predicted[0])‬
‭OUTPUT:-‬
‭Predicted value for 5: 23.999999999999968‬
‭Q6) Write a program to insert and clean data in python using pandas.‬
‭ ns:-‬
A

i‭mport pandas as pd‬


‭data = {'Name': ['John', 'Anna', 'Peter', 'Linda'],‬
‭'Age': [25, 30, 35, 40],‬
‭'Gender': ['M', 'F', 'M', 'F']}‬

‭ f = pd.DataFrame(data)‬
d
‭new_data = {'Name': 'Mike', 'Age': 45, 'Gender': 'M'}‬
‭df = df.append(new_data, ignore_index=True)‬
‭df_cleaned = df.dropna()‬

‭print(df_cleaned)‬

‭OUTPUT:-‬
‭Name Age Gender‬
‭0 John 25 M‬
‭1 Anna 30 F‬
‭2 Peter 35 M‬
‭3 Linda 40 F‬
‭4 Mike 45 M‬

You might also like