0% found this document useful (0 votes)
2 views4 pages

ML Algorithms Updated

The document provides a comprehensive guide for exam preparation on various machine learning algorithms, including Multiple Linear Regression, Polynomial Regression, KNN Classification, Decision Tree Classification, Random Forest Classification, and Support Vector Machine (SVM). Each section outlines the necessary steps, from importing libraries and loading datasets to training models and evaluating performance using metrics like Mean Squared Error (MSE) and accuracy. The document serves as a practical reference for implementing these algorithms using Python and the scikit-learn library.

Uploaded by

vrajpatel4277
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)
2 views4 pages

ML Algorithms Updated

The document provides a comprehensive guide for exam preparation on various machine learning algorithms, including Multiple Linear Regression, Polynomial Regression, KNN Classification, Decision Tree Classification, Random Forest Classification, and Support Vector Machine (SVM). Each section outlines the necessary steps, from importing libraries and loading datasets to training models and evaluating performance using metrics like Mean Squared Error (MSE) and accuracy. The document serves as a practical reference for implementing these algorithms using Python and the scikit-learn library.

Uploaded by

vrajpatel4277
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

Machine Learning Algorithms - Exam Preparation

Multiple Linear Regression


Type: Regression
# Step 1: Import libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score

# Step 2: Load dataset


df = pd.read_csv("[Link]")

# Step 3: Define x and y


x = df[["feature1","feature2"]]
y = df["target"]

# Step 4: Split dataset


x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=0.3, random_state=42)

# Step 5: Create model


model = LinearRegression()

# Step 6: Train model


[Link](x_train, y_train)

# Step 7: Prediction
y_pred = [Link](x_test)

# Step 8: Find MSE and R2 Score


mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print("MSE:", mse)
print("R2 Score:", r2)

Polynomial Regression
Type: Regression
ir

# Step 1: Import libraries


import pandas as pd
ls

from sklearn.model_selection import train_test_split


from [Link] import PolynomialFeatures
from sklearn.linear_model import LinearRegression
u

from [Link] import mean_squared_error, r2_score


eh

# Step 2: Load dataset


df = pd.read_csv("[Link]")
M

# Step 3: Define x and y


x = df[["feature1","feature2"]]
y = df["target"]

# Step 4: Convert into polynomial features


poly = PolynomialFeatures(degree=2)
x_poly = poly.fit_transform(x)

# Step 5: Split dataset


x_train, x_test, y_train, y_test = train_test_split(
x_poly, y, test_size=0.3, random_state=42)

# Step 6: Create model


model = LinearRegression()
# Step 7: Train model
[Link](x_train, y_train)

# Step 8: Prediction
y_pred = [Link](x_test)

# Step 9: Find MSE and R2 Score


mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print("MSE:", mse)
print("R2 Score:", r2)

KNN Classification
Type: Classification
# Step 1: Import libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score

# Step 2: Load dataset


df = pd.read_csv("[Link]")

# Step 3: Define x and y


x = df[["feature1","feature2"]]
y = df["target"]

# Step 4: Split dataset


x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=0.3, random_state=42)

# Step 5: Create model


model = KNeighborsClassifier(n_neighbors=5)

# Step 6: Train model


[Link](x_train, y_train)

# Step 7: Prediction
y_pred = [Link](x_test)

# Step 8: Find Accuracy


acc = accuracy_score(y_test, y_pred)

print("Accuracy:", acc)

Decision Tree Classification


ir
ls

Type: Classification
u

# Step 1: Import libraries


import pandas as pd
eh

from sklearn.model_selection import train_test_split


from [Link] import accuracy_score
M

# Step 2: Load dataset


df = pd.read_csv("[Link]")

# Step 3: Define x and y


x = df[["feature1","feature2"]]
y = df["target"]

# Step 4: Split dataset


x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=0.3, random_state=42)

# Step 5: Create model


model = DecisionTreeClassifier(random_state=42)

# Step 6: Train model


criterion=“entropy”
Max_depth=10
[Link](x_train, y_train)

# Step 7: Prediction
y_pred = [Link](x_test)

# Step 8: Find Accuracy


acc = accuracy_score(y_test, y_pred)

print("Accuracy:", acc)

Random Forest Classification


Type: Classification
# Step 1: Import libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score

# Step 2: Load dataset


df = pd.read_csv("[Link]")

# Step 3: Define x and y


x = df[["feature1","feature2"]]
y = df["target"]

# Step 4: Split dataset


x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=0.3, random_state=42)

# Step 5: Create model


model = RandomForestClassifier(random_state=42)

# Step 6: Train model


[Link](x_train, y_train) criterion=“entropy”
# Step 7: Prediction
y_pred = [Link](x_test) Max_depth=10
# Step 8: Find Accuracy
acc = accuracy_score(y_test, y_pred)

print("Accuracy:", acc)

Support Vector Machine (SVM)


ir

Type: Classification
ls

# Step 1: Import libraries


import pandas as pd
u

from sklearn.model_selection import train_test_split


from [Link] import accuracy_score
eh

# Step 2: Load dataset


df = pd.read_csv("[Link]")
M

# Step 3: Define x and y


x = df[["feature1","feature2"]]
y = df["target"]

# Step 4: Split dataset


x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=0.3, random_state=42)

# Step 5: Create model


model = SVC(C=1)

# Step 6: Train model


[Link](x_train, y_train)
# Step 7: Prediction
y_pred = [Link](x_test)

# Step 8: Find Accuracy


acc = accuracy_score(y_test, y_pred)

print("Accuracy:", acc)

ir
u ls
eh
M

You might also like