0% found this document useful (0 votes)
14 views20 pages

ML Lab

The document outlines a series of tasks related to data science and machine learning, including data preprocessing, deploying various regression models, implementing classification algorithms, and simulating neural networks. Each task includes code snippets for practical implementation using Python libraries such as pandas, scikit-learn, and TensorFlow. Additionally, it covers techniques like K-Means clustering and the Apriori algorithm for market basket analysis.

Uploaded by

ayushk2375
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)
14 views20 pages

ML Lab

The document outlines a series of tasks related to data science and machine learning, including data preprocessing, deploying various regression models, implementing classification algorithms, and simulating neural networks. Each task includes code snippets for practical implementation using Python libraries such as pandas, scikit-learn, and TensorFlow. Additionally, it covers techniques like K-Means clustering and the Apriori algorithm for market basket analysis.

Uploaded by

ayushk2375
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

INDEX

Sr. Task Page Remarks


No. No.
1 Data Preprocessing.
2 Deploy Simple Linear
Regression.
3 Simulate Multiple Linear
Regression.
4 Implement Decision Tree.
5 Implement Random Forest
Classification
6 Simulate Naïve Bayes
algorithm
7 Implement K-Nearest
Neighbors (K-NN), k-Means
8 Deploy Support Vector
Machine, Apriori algorithm
9 Simulate Artificial Neural
Network
10 Implement the Genetic
Algorithm code
TASK-1
Data Preprocessing.
import numpy as np
import [Link] as plt
import pandas as pd

dataset = pd.read_csv('[Link]')
X = [Link][:, :-1].values
y = [Link][:, -1].values
from [Link] import SimpleImputer
imputer = SimpleImputer(missing_values=[Link], strategy='mean')
[Link](X[:, 1:3])
X[:, 1:3] = [Link](X[:, 1:3])

from [Link] import ColumnTransformer


from [Link] import OneHotEncoder
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [0])], remainder='passthrough')
X = [Link](ct.fit_transform(X))

from [Link] import LabelEncoder


le = LabelEncoder()
y = le.fit_transform(y)
from [Link] import StandardScaler
sc = StandardScaler()
X_train[:, 3:] = sc.fit_transform(X_train[:, 3:])
X_test[:, 3:] = [Link](X_test[:, 3:])
TASK-2
Deploy Simple Linear Regression.
Program:
import numpy as np
import [Link] as plt
import pandas as pd
dataset = pd.read_csv('/kaggle/input/salary-dataset/data_Set (1).csv')
X = [Link][:, :-1].values
print([Link])
y = [Link][:, 1].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=0)
"""
# Scaling
from [Link] import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)
"""
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
[Link](X_train, y_train)
viz_train = plt
viz_train.scatter(X_train, y_train, color='red')
viz_train.plot(X_train, [Link](X_train), color='blue')
viz_train.title('Salary VS Experience (Training set)')
viz_train.xlabel('Year of Experience')
viz_train.ylabel('Salary')
viz_train.show()
viz_test = plt
viz_test.scatter(X_test, y_test, color='red')
viz_test.plot(X_train, [Link](X_train), color='blue')
viz_test.title('Salary VS Experience (Test set)')
viz_test.xlabel('Year of Experience')
viz_test.ylabel('Salary')
viz_test.show()
x_test = [Link]([Link](5).reshape(-1,1))
print(x_test)

Outout:
TASK-3
Simulate Multiple Linear Regression.
import numpy as np
import [Link] as plt
import pandas as pd
dataset = pd.read_csv('50_Startups.csv')
X = [Link][:, :-1].values
y = [Link][:, -1].values

from [Link] import ColumnTransformer


from [Link] import OneHotEncoder
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [3])], remainder='passthrough')
X = [Link](ct.fit_transform(X))
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

from sklearn.linear_model import LinearRegression


regressor = LinearRegression()
[Link](X_train, y_train)

y_pred = [Link](X_test)
np.set_printoption(precision=2)
print([Link]((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1)
TASK-4
Implement Decision Tree.
import numpy as np
import [Link] as plt
import pandas as pd
dataset = pd.read_csv('Position_Salaries.csv')
X = [Link][:, 1:-1].values
y = [Link][:, -1].values

from [Link] import DecisionTreeRegressor


regressor = DecisionTreeRegressor(random_state = 0)
[Link](X, y)

X_grid = [Link](min(X), max(X), 0.01)


X_grid = X_grid.reshape((len(X_grid), 1))
[Link](X, y, color = 'red')
[Link](X_grid, [Link](X_grid), color = 'blue')
[Link]('Truth or Bluff (Decision Tree Regression)')
[Link]('Position level')
[Link]('Salary')
[Link]()
TASK-5
Implement Random Forest Classification
import numpy as np
import [Link] as plt
import pandas as pd
dataset = pd.read_csv('Position_Salaries.csv')
X = [Link][:, 1:-1].values
y = [Link][:, -1].values

from [Link] import RandomForestRegressor


regressor = RandomForestRegressor(n_estimators = 10, random_state = 0)
[Link](X, y)

X_grid = [Link](min(X), max(X), 0.01)


X_grid = X_grid.reshape((len(X_grid), 1))
[Link](X, y, color = 'red')
[Link](X_grid, [Link](X_grid), color = 'blue')
[Link]('Truth or Bluff (Random Forest Regression)')
[Link]('Position level')
[Link]('Salary')
[Link]()
TASK-6
Simulate Naïve Bayes algorithm
import numpy as np
import [Link] as plt
import pandas as pd
dataset = pd.read_csv('Social_Network_Ads.csv')
X = [Link][:, :-1].values
y = [Link][:, -1].values

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

from [Link] import StandardScaler


sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = [Link](X_test)

from sklearn.naive_bayes import GaussianNB


classifier = GaussianNB()
[Link](X_train, y_train)

y_pred = [Link](X_test)

from [Link] import confusion_matrix, accuracy_score


cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)

from [Link] import ListedColormap


X_set, y_set = sc.inverse_transform(X_test), y_test
X1, X2 = [Link]([Link](start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step =
0.25),
[Link](start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step = 0.25))
[Link](X1, X2, [Link]([Link]([Link]([[Link](),
[Link]()]).T)).reshape([Link]),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
[Link]([Link](), [Link]())
[Link]([Link](), [Link]())
for i, j in enumerate([Link](y_set)):
[Link](X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label =
j)
[Link]('Naive Bayes (Test set)')
[Link]('Age')
[Link]('Estimated Salary')
[Link]()
[Link]()
TASK-7
Implement K-Nearest Neighbors (K-NN), k-Means
import numpy as np
import [Link] as plt
import pandas as pd
dataset = pd.read_csv('Social_Network_Ads.csv')
X = [Link][:, :-1].values
y = [Link][:, -1].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

from [Link] import StandardScaler


sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = [Link](X_test)

from [Link] import KNeighborsClassifier


classifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
[Link](X_train, y_train)

y_pred = [Link](X_test)
from [Link] import ListedColormap
X_set, y_set = sc.inverse_transform(X_test), y_test
X1, X2 = [Link]([Link](start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step =
1),
[Link](start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step = 1))
[Link](X1, X2, [Link]([Link]([Link]([[Link](),
[Link]()]).T)).reshape([Link]),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
[Link]([Link](), [Link]())
[Link]([Link](), [Link]())
for i, j in enumerate([Link](y_set)):
[Link](X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label =
j)
[Link]('K-NN (Test set)')
[Link]('Age')
[Link]('Estimated Salary')
[Link]()
[Link]()

K-Means

import numpy as np
import [Link] as plt
import pandas as pd
dataset = pd.read_csv('Mall_Customers.csv')
X = [Link][:, [3, 4]].values
from [Link] import KMeans
wcss = []
for i in range(1, 11):
kmeans = KMeans(n_clusters = i, init = 'k-means++', random_state = 42)
[Link](X)
[Link](kmeans.inertia_)

kmeans = KMeans(n_clusters = 5, init = 'k-means++', random_state = 42)


y_kmeans = kmeans.fit_predict(X)

[Link](X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], s = 100, c = 'red', label = 'Cluster 1')


[Link](X[y_kmeans == 1, 0], X[y_kmeans == 1, 1], s = 100, c = 'blue', label = 'Cluster 2')
[Link](X[y_kmeans == 2, 0], X[y_kmeans == 2, 1], s = 100, c = 'green', label = 'Cluster 3')
[Link](X[y_kmeans == 3, 0], X[y_kmeans == 3, 1], s = 100, c = 'cyan', label = 'Cluster 4')
[Link](X[y_kmeans == 4, 0], X[y_kmeans == 4, 1], s = 100, c = 'magenta', label = 'Cluster 5')
[Link](kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s = 300, c = 'yellow', label =
'Centroids')
[Link]('Clusters of customers')
[Link]('Annual Income (k$)')
[Link]('Spending Score (1-100)')
[Link]()
[Link]()
TASK-8
Deploy Support Vector Machine, Apriori algorithm
import numpy as np
import [Link] as plt
import pandas as pd
dataset = pd.read_csv('Social_Network_Ads.csv')
X = [Link][:, :-1].values
y = [Link][:, -1].values

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

from [Link] import StandardScaler


sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = [Link](X_test)

from [Link] import StandardScaler


sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = [Link](X_test)

y_pred = [Link](X_test)
print([Link]((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))

from [Link] import confusion_matrix, accuracy_score


cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)

from [Link] import ListedColormap


X_set, y_set = sc.inverse_transform(X_test), y_test
X1, X2 = [Link]([Link](start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step =
0.25),
[Link](start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step = 0.25))
[Link](X1, X2, [Link]([Link]([Link]([[Link](),
[Link]()]).T)).reshape([Link]),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
[Link]([Link](), [Link]())
[Link]([Link](), [Link]())
for i, j in enumerate([Link](y_set)):
[Link](X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label =
j)
[Link]('SVM (Test set)')
[Link]('Age')
[Link]('Estimated Salary')
[Link]()
[Link]()

Apriori algorithm
import numpy as np
import [Link] as plt
import pandas as pd

dataset = pd.read_csv('Market_Basket_Optimisation.csv', header = None)


transactions = []
for i in range(0, 7501):
[Link]([str([Link][i,j]) for j in range(0, 20)])
from apyori import apriori
rules = apriori(transactions = transactions, min_support = 0.003, min_confidence = 0.2, min_lift = 3,
min_length = 2, max_length = 2)
def inspect(results):
lhs = [tuple(result[2][0][0])[0] for result in results]
rhs = [tuple(result[2][0][1])[0] for result in results]
supports = [result[1] for result in results]
confidences = [result[2][0][2] for result in results]
lifts = [result[2][0][3] for result in results]
return list(zip(lhs, rhs, supports, confidences, lifts))
resultsinDataFrame = [Link](inspect(results), columns = ['Left Hand Side', 'Right Hand Side',
'Support', 'Confidence', 'Lift'])

[Link](n = 10, columns = 'Lift')


TASK-9
Simulate Artificial Neural Network
import numpy as np
import pandas as pd
import tensorflow as tf
dataset = pd.read_csv('Churn_Modelling.csv')
X = [Link][:, 3:-1].values
y = [Link][:, -1].values

from [Link] import LabelEncoder


le = LabelEncoder()
X[:, 2] = le.fit_transform(X[:, 2])

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

ann = [Link]()
[Link]([Link](units=6, activation='relu'))
[Link]([Link](units=6, activation='relu'))
[Link]([Link](units=1, activation='sigmoid'))
[Link](optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

[Link](X_train, y_train, batch_size = 32, epochs = 100)


y_pred = [Link](X_test)
y_pred = (y_pred > 0.5)
print([Link]((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
TASK-10
Implement the Genetic Algorithm code

import numpy as np
# Define the objective function (minimization problem)
def objective_function(x):
return [Link](x**2)
# Define the genetic algorithm function
def genetic_algorithm(objective_function, bounds, population_size=50, max_generations=100,
mutation_rate=0.1):
# Generate initial population
population = [Link](bounds[0], bounds[1], size=(population_size, len(bounds))
for generation in range(max_generations):
# Evaluate fitness for each individual in the population
fitness = [Link]([objective_function(individual) for individual in population])

# Select parents for mating using tournament selection


selected_indices = [Link](population_size, size=(population_size // 2, 2), replace=False)
parents = population[selected_indices]
# Perform crossover (single point crossover)
crossover_point = [Link](1, [Link][1])
offspring = [Link]([parents[:, 0, :crossover_point], parents[:, 1, crossover_point:]], axis=1)

# Perform mutation
mutation_mask = [Link](*[Link]) < mutation_rate
mutation_values = [Link](bounds[0], bounds[1], size=[Link])

offspring = [Link](mutation_mask, mutation_values, offspring


# Replace old population with offspring
population[:population_size // 2] = offspring

# Print the best individual in the current generation


best_individual_index = [Link](fitness)
best_individual = population[best_individual_index]
print(f"Generation {generation + 1}, Best Fitness: {fitness[best_individual_index]}, Best Individual:
{best_individual}")

# Return the best solution found


best_individual_index = [Link](fitness)

best_individual = population[best_individual_index]
best_fitness = fitness[best_individual_index]
return best_individual, best_fitness
# Define the search space bounds
bounds = (-10, 10)
# Run the genetic algorithm
best_solution, best_fitness = genetic_algorithm(objective_function, bounds)
print(f"Best solution found: {best_solution}, Best fitness: {best_fitness}")

You might also like