Machine Learning Practical PDF
Machine Learning Practical PDF
Machine Learning Practical PDF
import pandas as pd
df =
pd.read_excel('study
.xlsl') df
df.dropna(axis=0,
inplace=True) df
df.fillna(0) df
Output:
Program:
import
pandas as pd
# Sample
dataset
data = {'Length': [5.1, 4.9, 4.7, 4.6, 5.0],'Width': [3.5, 3.0, 3.2, 3.1, 3.6],'Height':
[1.4, 1.4, 1.3, 1.5, 1.4]}
df = pd.DataFrame(data)
df['Volume'] = df['Length'] * df['Width'] * df['Height'] print(df)
Output:
Program:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import
train_test_split from
sklearn.linear_model import
LinearRegression np.random.seed(0)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.rand(100, 1)
X_train, X_test, y_train, y_test = train_test_split(X, y,
model.fit(X_train,
y_train) y_pred =
model.predict(X_test
plt.scatter(X_train, y_train,
label='Training Data')
plt.scatter(X_test, y_test,
label='Test Data')
plt.yla
bel('y')
plt.leg
end()
plt.sho
w()
Output:
Program:
make_classification from
sklearn.neighbors import
KNeighborsClassifier from
sklearn.model_selection import
import accuracy_score
KNeighborsClassifier(n_neighbors=1)
accuracy =
accuracy_score(y_test,
y_pred) print(f'Accuracy:
{accuracy}')
output:
Program:
import numpy as np
import matplotlib.pyplot
as plt from
sklearn.cluster import
KMeans
from sklearn.datasets import make_blobs
n_samples = 400
n_features = 70
n_clusters = 80
X, _ = make_blobs(n_samples=n_samples, n_features=n_features,
centers=n_clusters, random_state=42)
kmeans =
KMeans(n_clusters=3)
kmeans.fit(X)
labels = kmeans.labels_
centers = kmeans.cluster_centers_
cmap='viridis')
plt.title('K-Means
Clustering')
plt.show()
Output:
Program:
import
numpy as
import
matplotlib.p
yplot as plt
make_classification from
sklearn.linear_model import
LogisticRegression from
sklearn.model_selection import
train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix,
classification_report
X, y = make_classification(n_samples=100, n_features=2, n_classes=2,
n_clusters_per_class=1, n_redundant=0, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y,
clf.fit(X_train,
y_train) y_pred =
clf.predict(X_test)
conf_matrix =
confusion_matrix(y_test, y_pred)
print('Confusion Matrix:')
print(conf_matrix)
class_report = classification_report(y_test, y_pred) print('Classification Report:')
print(class_report)
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap='coolwarm', marker='o',
plt.ylabel('Feature 2')
h = .02 # Step size in the mesh
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape)
Output:
Program:
import numpy as np
import matplotlib.pyplot as plt from sklearn import datasets
SVC
SVC(kernel='linear')
print(conf_matrix)
class_report = classification_report(y_test, y_pred) print('Classification
Report:')
print(class_report)
def plot_decision_boundary(X, y, model, title): x_min, x_max = X[:, 0].min() - 1,
X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02), np.arange(y_min,
Output: