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

Programs

The document provides an overview of various supervised and unsupervised learning algorithms using Python's scikit-learn library, including Linear Regression, Logistic Regression, Decision Trees, K-Nearest Neighbors, Random Forests, K-Means Clustering, Neural Networks, and Hierarchical Clustering. It also includes code snippets for loading datasets, training models, and making predictions. Additionally, it covers model evaluation techniques such as cross-validation to assess model accuracy.

Uploaded by

nehapatil6369
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)
4 views4 pages

Programs

The document provides an overview of various supervised and unsupervised learning algorithms using Python's scikit-learn library, including Linear Regression, Logistic Regression, Decision Trees, K-Nearest Neighbors, Random Forests, K-Means Clustering, Neural Networks, and Hierarchical Clustering. It also includes code snippets for loading datasets, training models, and making predictions. Additionally, it covers model evaluation techniques such as cross-validation to assess model accuracy.

Uploaded by

nehapatil6369
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

I.

Supervised Learning Algorithms


1. Linear Regression (Regression)

from sklearn.linear_model import LinearRegression


from sklearn.model_selection import train_test_split
from [Link] import load_diabetes

# 1. Load data
X, y = load_diabetes(return_X_y=True)
# 2. Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 3. Initialize model
model = LinearRegression()
# 4. Train model
[Link](X_train, y_train)
# 5. Predict
print([Link](X_test[:5]))

2. Logistic Regression (Classification)

from sklearn.linear_model import LogisticRegression


from sklearn.model_selection import train_test_split
from [Link] import load_iris

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = LogisticRegression(max_iter=200)
[Link](X_train, y_train)
print([Link](X_test[:5]))

3. Decision Tree

from [Link] import DecisionTreeClassifier


from sklearn.model_selection import train_test_split
from [Link] import load_iris

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = DecisionTreeClassifier()
[Link](X_train, y_train)
print([Link](X_test[:5]))
4. K-Nearest Neighbors (KNN)

from [Link] import KNeighborsClassifier


from sklearn.model_selection import train_test_split
from [Link] import load_iris

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = KNeighborsClassifier(n_neighbors=3)
[Link](X_train, y_train)
print([Link](X_test[:5]))

5. Random Forest

from [Link] import RandomForestClassifier


from sklearn.model_selection import train_test_split
from [Link] import load_iris

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = RandomForestClassifier(n_estimators=100)
[Link](X_train, y_train)
print([Link](X_test[:5]))
II. Unsupervised Learning Algorithms
1. K-Means Clustering

from [Link] import KMeans


from [Link] import load_iris

# Only features (X) are used, no labels (y)


X, _ = load_iris(return_X_y=True)

model = KMeans(n_clusters=3)
[Link](X)
print(model.labels_[:5])

2. Neural Network (MLP Classifier)

from sklearn.neural_network import MLPClassifier


from sklearn.model_selection import train_test_split
from [Link] import load_iris

# Load and Split


X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Train and Predict (hidden_layer_sizes defines the neurons)


model = MLPClassifier(hidden_layer_sizes=(10, 10), max_iter=500)
[Link](X_train, y_train)
print([Link](X_test[:5]))

3. Hierarchical Clustering (Agglomerative)

from [Link] import AgglomerativeClustering


from [Link] import load_iris

# Load data (Features only)


X, _ = load_iris(return_X_y=True)

# Initialize model (n_clusters defines how many groups to form)


model = AgglomerativeClustering(n_clusters=3)

# Train and Predict (returns cluster labels for each data point)
labels = model.fit_predict(X)
print(labels[:5])
III. Model Evaluation
1. Cross-Validation

from sklearn.model_selection import cross_val_score


from sklearn.linear_model import LogisticRegression
from [Link] import load_iris

X, y = load_iris(return_X_y=True)
model = LogisticRegression(max_iter=200)

# Splits data into 5 folds and calculates accuracy for each


scores = cross_val_score(model, X, y, cv=5)
print("Mean Accuracy:", [Link]())

You might also like