Edit Template
Edit Template
Edit Template

Scikit Learn Exercise

Fit a linear regression model to predict a target variable
from sklearn.linear_model import LinearRegression
import numpy as np

# Dummy dataset
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([1.2, 1.9, 3.1, 4.2, 5.1])

# Model fitting
model = LinearRegression()
model.fit(X, y)

# Prediction
prediction = model.predict(np.array([[6]]))
print("Prediction for input 6:", prediction)
from sklearn.metrics import confusion_matrix

# True labels and predicted labels
y_true = [0, 1, 0, 1, 0, 1]
y_pred = [0, 1, 0, 0, 1, 1]

# Confusion Matrix
matrix = confusion_matrix(y_true, y_pred)
print("Confusion Matrix:\n", matrix)
from sklearn.linear_model import LogisticRegression
import numpy as np

# Dummy dataset
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([0, 0, 0, 1, 1])  # 0 for ≤ 3, 1 for > 3

# Model fitting
model = LogisticRegression()
model.fit(X, y)

# Prediction
prediction = model.predict([[2], [4]])
print("Predictions:", prediction)
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification

# Generate dataset
X, y = make_classification(n_samples=100, n_features=4, random_state=42)

# Random Forest Classifier
clf = RandomForestClassifier()
clf.fit(X, y)

# Prediction
print("Prediction for first data point:", clf.predict([X[0]]))
from sklearn.neighbors import KNeighborsClassifier

# Dummy data
X = [[0], [1], [2], [3], [4], [5]]
y = [0, 0, 1, 1, 0, 0]  # 0 or 1 classes

# Model training
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X, y)

# Prediction
prediction = knn.predict([[2]])
print("Prediction for input 2:", prediction)
from sklearn.cluster import KMeans
import numpy as np

# Dummy data
X = np.array([[1, 2], [1, 4], [1, 0],
              [4, 2], [4, 4], [4, 0]])

# K-means clustering
kmeans = KMeans(n_clusters=3, random_state=0)
kmeans.fit(X)
print("Cluster centers:\n", kmeans.cluster_centers_)
print("Labels for input points:", kmeans.labels_)
from sklearn.tree import DecisionTreeClassifier

# Dummy data
X = [[1], [2], [3], [4], [5], [6]]
y = [0, 0, 1, 1, 1, 0]

# Model training
tree = DecisionTreeClassifier()
tree.fit(X, y)

# Prediction
prediction = tree.predict([[3]])
print("Prediction for input 3:", prediction)
from sklearn.decomposition import PCA
from sklearn.datasets import load_iris

# Load dataset
iris = load_iris()
X = iris.data

# PCA
pca = PCA(n_components=2)
X_reduced = pca.fit_transform(X)
print("Reduced feature data:\n", X_reduced[:5])
from sklearn.preprocessing import StandardScaler
import numpy as np

X = np.array([[1], [10], [100], [1000]])

# Scaling
scaler = StandardScaler()
scaled_X = scaler.fit_transform(X)
print("Scaled Data:\n", scaled_X)
from sklearn import datasets
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score

# Load dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target

# SVM Classifier
svm = SVC()

# Cross-validation
scores = cross_val_score(svm, X, y, cv=5)
print("Cross-validation scores:", scores)
  • All Posts
  • Artificial Intelligence
  • Computer Fundamentals
  • Computer Networks
  • Data Analytics
  • Data Science
  • DBMS
  • Deep Learning
  • Digital Fundamentals
  • DSA with Python
  • Excel
  • Exercise
  • Git & Github
  • Machine Learning
  • Matplotlib
  • Natural Language Processing
  • NumPy
  • Operating System
  • Pandas-s
  • Power BI
  • Python Tutorial
  • Scikit-learn
  • Seaborn
  • SQL & MySQL
Python Loops

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Hamburger Toggle...

Conditional Statements

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Hamburger Toggle...

Python Operators

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Hamburger Toggle...

Python Data Type

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Hamburger Toggle...

Scikit-learn

Scikit-learn Tutorial Scikit-learn Hamburger Toggle Menu Edit Template Scikit learn What is Scikit-learn Scikit learn एक python library है जिसका...

Pandas

Pandas Tutorial Pandas Hamburger Toggle Menu Edit Template Pandas What is Pandas pandas  एक python library है, जिसका use हम...

NumPy

NumPy Tutorial NumPy Hamburger Toggle Menu Edit Template NumPy What is NumPy matplotlib का use हम data visualization के लिए...

Matplotlib

Matplotlib Tutorial Matplotlib Hamburger Toggle Menu Edit Template Matplotlib What is Matplotlib matplotlib का use हम data visualization के लिए...

Seaborn

Seaborn Tutorial Seaborn Hamburger Toggle Menu Edit Template Seaborn What is Seaborn seaborn  एक python library है जो Matplotlib पर...

Edit Template
Scroll to Top