0% found this document useful (0 votes)
10 views6 pages

Python

The document outlines a complete machine learning workflow in Python, covering data loading, preprocessing, model training, and evaluation. It includes examples of various algorithms such as Linear Regression, Logistic Regression, k-Nearest Neighbors, Decision Trees, Random Forests, Support Vector Machines, and K-Means Clustering. Additionally, it discusses model evaluation techniques and overfitting control methods.

Uploaded by

kumarshwetank11
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)
10 views6 pages

Python

The document outlines a complete machine learning workflow in Python, covering data loading, preprocessing, model training, and evaluation. It includes examples of various algorithms such as Linear Regression, Logistic Regression, k-Nearest Neighbors, Decision Trees, Random Forests, Support Vector Machines, and K-Means Clustering. Additionally, it discusses model evaluation techniques and overfitting control methods.

Uploaded by

kumarshwetank11
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

1.

Required Python Libraries

import numpy as np

import pandas as pd

import [Link] as plt

import seaborn as sns

from sklearn.model_selection import train_test_split

from [Link] import StandardScaler

from [Link] import accuracy_score, mean_squared_error

2. Loading and Exploring Data

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

print([Link]())

print([Link]())

print([Link]())

Check missing values

[Link]().sum()

3. Data Preprocessing

Handle missing values

[Link]([Link](), inplace=True)

Encode categorical data

data = pd.get_dummies(data, drop_first=True)

Feature & label split

X = [Link]("target", axis=1)

y = data["target"]
4. Train–Test Split

X_train, X_test, y_train, y_test = train_test_split(

X, y, test_size=0.2, random_state=42

5. Feature Scaling

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)

X_test = [Link](X_test)

6. Linear Regression (Regression Problem)

from sklearn.linear_model import LinearRegression

model = LinearRegression()

[Link](X_train, y_train)

predictions = [Link](X_test)

mse = mean_squared_error(y_test, predictions)

print("MSE:", mse)

7. Logistic Regression (Classification)

from sklearn.linear_model import LogisticRegression

model = LogisticRegression()

[Link](X_train, y_train)
y_pred = [Link](X_test)

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

8. k-Nearest Neighbors

from [Link] import KNeighborsClassifier

model = KNeighborsClassifier(n_neighbors=5)

[Link](X_train, y_train)

y_pred = [Link](X_test)

print("Accuracy:", accuracy_score(y_test, y_pred))

9. Decision Tree

from [Link] import DecisionTreeClassifier

model = DecisionTreeClassifier(criterion="gini")

[Link](X_train, y_train)

y_pred = [Link](X_test)

print("Accuracy:", accuracy_score(y_test, y_pred))

10. Random Forest

from [Link] import RandomForestClassifier

model = RandomForestClassifier(n_estimators=100)

[Link](X_train, y_train)
y_pred = [Link](X_test)

print("Accuracy:", accuracy_score(y_test, y_pred))

11. Support Vector Machine (SVM)

from [Link] import SVC

model = SVC(kernel="rbf")

[Link](X_train, y_train)

y_pred = [Link](X_test)

print("Accuracy:", accuracy_score(y_test, y_pred))

12. K-Means Clustering (Unsupervised)

from [Link] import KMeans

kmeans = KMeans(n_clusters=3)

[Link](X)

labels = kmeans.labels_

13. PCA (Dimensionality Reduction)

from [Link] import PCA

pca = PCA(n_components=2)

X_reduced = pca.fit_transform(X)

14. Model Evaluation


Confusion Matrix

from [Link] import confusion_matrix

confusion_matrix(y_test, y_pred)

Classification Report

from [Link] import classification_report

print(classification_report(y_test, y_pred))

15. Overfitting Control

Regularization

from sklearn.linear_model import Ridge, Lasso

ridge = Ridge(alpha=1.0)

lasso = Lasso(alpha=0.1)

16. Simple Neural Network (Python)

from sklearn.neural_network import MLPClassifier

model = MLPClassifier(hidden_layer_sizes=(100, 50), max_iter=500)

[Link](X_train, y_train)

y_pred = [Link](X_test)

print("Accuracy:", accuracy_score(y_test, y_pred))

17. Complete ML Workflow in Python

# 1. Load data

# 2. Clean data
# 3. Feature engineering

# 4. Split

# 5. Scale

# 6. Train

# 7. Evaluate

# 8. Tune

# 9. Deploy

You might also like