ML (Sudhanshu)
ML (Sudhanshu)
Code:-
import random as r
import string as s
passwd="".join(passwd)
passwd="".join(passwd)
passwd="".join(passwd)
Output:-
Q2. Demonstrate the following random method in Python with appropriate outputs:
Solution:-
Code:-
import random as r
Output:-
Random Floating value ------>>> 0.13906128209448765
Random Floating value ------>>> 0.3077718443686104
Random Floating value ------>>> 0.4585025867516742
Code:-
import random as r
Output:-
Code:-
import random as r
Output:-
Code:-
import random as r
a=[11,12,13,14,15,16,17,18,19]
Output:-
Output:
➢ How to draw a graph of data set.
Code :
import numpy as np
x = np.array([1,4,3,8,6])
plt.plot(x, y)
plt.show()
Output:
➢ How to Give Title of Data Set in Graph.
Code :
import numpy as np
x = np.array([1, 3, 5, 7,9])
y = np.array([10,19,35,40,50])
plt.plot(x, y)
plt.xlabel("Roll Number")
plt.ylabel("Marks")
plt.title("Student Reports")
plt.show()
Output:
➢ How to draw doted line in a graph of data set .
Code :
import numpy as np
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("My Chart")
plt.show()
Output:
➢ How to fill colour b/w two line.
Code :
import numpy as np
x = np.array([2, 8, 6, 9,10])
plt.plot(x, y)
x1 = [2, 8, 6, 9,10]
plt.xlabel("X-axis data")
plt.ylabel("Y-axis data")
plt.title('multiple plots')
plt.fill_between(x, y, y1, color='black', alpha=.9)
plt.show()
Output;
Code :
import numpy as np
y=np.sin(x)
plt.subplot(5,2, 1)
plt.plot(x,y)
plt.show()
Output:
Code:
import numpy as np
x = [1,3,7,14,18,36,40]
plt.legend()
plt.show()
Output:
age = [10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
cardiac_cases = [16,23,25,27,29,31,32,40,41,43,45,50,56,57,59,61]
plt.xlabel("Age")
plt.ylabel("Percentage")
plt.show()
Output:
Code:
courses = list(data.keys())
values = list(data.values())
plt.xlabel("Courses offered")
plt.show()
Output:
Code:
import pandas as pd
data = pd.read_csv('data.csv')
print(data.isnull().sum())
Output:
x 0
y 0
dtype: int64
Code:
import pandas as pd
data = pd.read_csv('data.csv')
print(data.describe())
Output:
Code:
import pandas as pd
data = pd.read_csv('data.csv')
print(data.dtypes)
Output:
x float64
y float64
dtype: object
Code:-
import pandas as pd
import numpy as np
db=datasets.load_diabetes()
#ii. Identify the missing values and count the missing values feature wise.
dbs=pd.DataFrame(db.data, columns=db.feature_names)
print(dbs.isnull().sum())
Output:
plt.figure(figsize=(10, 6))
plt.boxplot(dbs)
plt.xlabel('No of column')
plt.ylabel('Data')
plt.title('Boxplot of Features')
plt.show()
Output:
x=db.data[:,np.newaxis,2]
y=db.target
db_x_train,db_x_test,db_y_train,db_y_test= train_test_split(x,y,
test_size=0.2)
model=linear_model.LinearRegression()
model.fit(db_x_train,db_y_train)
db_y_predicted=model.predict(db_x_test)
#v. Visualize the linear regression plot using matplotlib library.
plt.scatter(db_x_test,db_y_test,color='black')
plt.xlabel('Actual Values')
plt.ylabel('Predicted Values')
plt.plot(db_x_test,db_y_predicted,color='black')
plt.show()
Output:
Q- 5 Implement and demonstrate the FIND algorithm for finding the most specific
hypothesis based on a given set of training data samples. Read the training data from
a .CSV file.
Code:-
import pandas as pd
def load_data(file_name):
data = pd.read_csv(file_name)
return data
# FIND algorithm
hypothesis = []
if attribute != target_attribute:
attribute_values = data[attribute].unique()
hypothesis.append(f"{attribute} = {value}")
refined_hypothesis = []
refined_hypothesis.append(hypothesis_value)
return list(set(refined_hypothesis))
data = load_data('data2.csv')
target_attribute = 'class'
print(hypothesis_value)
Output:
Q-6 Implement the logistic regression algorithm and visualize the Sigmoid curve based on
the trained model.
Code:-
import numpy as np
np.random.seed(0)
X = np.random.randn(100, 1)
y = (X > 0).astype(int)
# Scale data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
model = LogisticRegression()
model.fit(X_scaled, y)
# Get coefficients
coef = model.coef_[0][0]
intercept = model.intercept_[0]
plt.figure(figsize=(8, 6))
plt.scatter(X_scaled, y, label='Data')
plt.title('Logistic Regression')
plt.xlabel('Input')
plt.ylabel('Probability')
plt.legend()
plt.show()
Output:-
Q-7 Write a program to implement k-Nearest Neighbor (KNN) algorithm to classify the iris
data set. Print both correct and wrong predictions.
Code:-
import numpy as np
iris = load_iris() # Load Iris dataset
X = iris.data
y = iris.target
print("\nWrong Predictions:")
for i in range(len(y_test)):
Output:-
print("Correct Predictions:")
for i in range(len(y_test)):
print("Accuracy:", accuracy)
Output:
Accuracy: 0.9444444444444444
print("Classification Report:")
print(classification_report(y_test, y_pred))
Output:
print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred))
Output:
Q – 8 Implement Support Vector Machine on any data set and analyze the accuracy with
Logistic regression.
Code:
import pandas as pd
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df['target'] = iris.target
df0=df[df.target==0]
df1=df[df.target==1]
df2=df[df.target==2]
# Split data
svm.fit(X_train, y_train)
lr.fit(X_train, y_train)
y_pred_lr = lr.predict(X_test)
Output:
Output:
print("SVM Classification Report:")
print(classification_report(y_test, y_pred_svm))
Output:
print(classification_report(y_test, y_pred_lr))
Output: