import numpy as np
Project description
'''import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_blobs from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn.tree import DecisionTreeClassifier from sklearn.svm import SVC from sklearn.ensemble import RandomForestClassifier from sklearn.cluster import KMeans from sklearn.linear_model import LinearRegression from sklearn.metrics import accuracy_score, mean_squared_error
#----------------------------------------------------------------------------------------------------------#
X, y = make_blobs(n_samples=1000, centers=4, n_features=2, random_state=42) plt.scatter(X[:, 0], X[:, 1], c=y, cmap='viridis') plt.title('Synthetic Dataset') plt.xlabel('Feature 1') plt.ylabel('Feature 2') plt.show()
#----------------------------------------------------------------------------------------------------------#
Pre-processing
scaler = StandardScaler() X_scaled = scaler.fit_transform(X)
#----------------------------------------------------------------------------------------------------------#
Train-test split
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
#----------------------------------------------------------------------------------------------------------#
KNN Classification
knn = KNeighborsClassifier(n_neighbors=5) knn.fit(X_train, y_train) knn_pred = knn.predict(X_test) knn_accuracy = accuracy_score(y_test, knn_pred)
#----------------------------------------------------------------------------------------------------------#
Decision Tree
dt = DecisionTreeClassifier() dt.fit(X_train, y_train) dt_pred = dt.predict(X_test) dt_accuracy = accuracy_score(y_test, dt_pred)
#----------------------------------------------------------------------------------------------------------#
SVM
svm = SVC(kernel='linear') svm.fit(X_train, y_train) svm_pred = svm.predict(X_test) svm_accuracy = accuracy_score(y_test, svm_pred)
#----------------------------------------------------------------------------------------------------------#
Random Forest
rf = RandomForestClassifier(n_estimators=100) rf.fit(X_train, y_train) rf_pred = rf.predict(X_test) rf_accuracy = accuracy_score(y_test, rf_pred)
#----------------------------------------------------------------------------------------------------------#
K-means Clustering
kmeans = KMeans(n_clusters=4) kmeans.fit(X_scaled) cluster_centers = kmeans.cluster_centers_
#----------------------------------------------------------------------------------------------------------#
Linear Regression
lr = LinearRegression() lr.fit(X_train, y_train) lr_pred = lr.predict(X_test) lr_rmse = mean_squared_error(y_test, lr_pred, squared=False)
classifiers = ['KNN', 'Decision Tree', 'SVM', 'Random Forest', 'Linear Regression'] accuracies = [knn_accuracy, dt_accuracy, svm_accuracy, rf_accuracy, lr_rmse]
plt.bar(classifiers, accuracies) plt.xlabel('Classifiers') plt.ylabel('Accuracy') plt.title('Accuracy of Different Classifiers') plt.show()
#----------------------------------------------------------------------------------------------------------#
#Desion Tree from sklearn.tree import plot_tree
Visualize decision tree
plt.figure(figsize=(12, 8)) plot_tree(dt, filled=True, feature_names=['Feature 1', 'Feature 2'], class_names=['Class 0', 'Class 1', 'Class 2', 'Class 3']) plt.title('Decision Tree Visualization') plt.show()
Visualize one decision tree from random forest (change index to visualize different trees)
plt.figure(figsize=(12, 8)) plot_tree(rf.estimators_[0], filled=True, feature_names=['Feature 1', 'Feature 2'], class_names=['Class 0', 'Class 1', 'Class 2', 'Class 3']) plt.title('Decision Tree from Random Forest') plt.show()
#----------------------------------------------------------------------------------------------------------#
Visualize SVM decision boundaries
plt.figure(figsize=(12, 8)) h = .02 # step size in the mesh x_min, x_max = X_scaled[:, 0].min() - 1, X_scaled[:, 0].max() + 1 y_min, y_max = X_scaled[:, 1].min() - 1, X_scaled[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = svm.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z, cmap=plt.cm.viridis, alpha=0.8)
Plot the dataset
plt.scatter(X_scaled[:, 0], X_scaled[:, 1], c=y, cmap='viridis') plt.title('SVM Decision Boundaries') plt.xlabel('Feature 1') plt.ylabel('Feature 2') plt.show()
#----------------------------------------------------------------------------------------------------------#
Visualize KNN decision boundaries
plt.figure(figsize=(12, 8)) h = .02 # step size in the mesh x_min, x_max = X_scaled[:, 0].min() - 1, X_scaled[:, 0].max() + 1 y_min, y_max = X_scaled[:, 1].min() - 1, X_scaled[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = knn.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z, cmap=plt.cm.viridis, alpha=0.8)
Plot the dataset
plt.scatter(X_scaled[:, 0], X_scaled[:, 1], c=y, cmap='viridis') plt.title('KNN Decision Boundaries') plt.xlabel('Feature 1') plt.ylabel('Feature 2') plt.show()
#----------------------------------------------------------------------------------------------------------#
Visualize K-means clustering
plt.figure(figsize=(12, 8))
Plot the dataset
plt.scatter(X_scaled[:, 0], X_scaled[:, 1], c=y, cmap='viridis', alpha=0.5)
Plot cluster centroids
plt.scatter(cluster_centers[:, 0], cluster_centers[:, 1], c='red', marker='x', s=100, label='Cluster Centroids')
plt.title('K-means Clustering') plt.xlabel('Feature 1') plt.ylabel('Feature 2') plt.legend() plt.show()
Visualize Linear Regression
plt.figure(figsize=(12, 8))
Plot the training data
plt.scatter(X_train[:, 0], y_train, color='blue', label='Training Data')
Plot the test data
plt.scatter(X_test[:, 0], y_test, color='green', label='Test Data')
Plot the regression line
plt.plot(X_test[:, 0], lr.predict(X_test), color='red', linewidth=2, label='Linear Regression')
plt.title('Linear Regression') plt.xlabel('Feature 1') plt.ylabel('Target Variable') plt.legend() plt.show()
#----------------------------------------------------------------------------------------------------------#
#K-fold
import matplotlib.pyplot as plt from sklearn.model_selection import KFold from sklearn.model_selection import cross_val_score import numpy as np
model = SVC(kernel='linear')
kfold = KFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(model, X_train, y_train, cv=kfold)
plt.figure(figsize=(8, 6)) plt.plot(np.arange(1, 6), scores, marker='o', linestyle='-') plt.xlabel('Fold') plt.ylabel('Accuracy') plt.title('K-Fold Cross-Validation Scores') plt.grid(True) plt.show()
#----------------------------------------------------------------------------------------------------------#
Visualize SVM decision boundaries with polynomial kernel
svm_poly = SVC(kernel='poly', degree=3) # Polynomial kernel with degree 3 svm_poly.fit(X_train, y_train)
plt.figure(figsize=(12, 8)) h = .02 # step size in the mesh x_min, x_max = X_scaled[:, 0].min() - 1, X_scaled[:, 0].max() + 1 y_min, y_max = X_scaled[:, 1].min() - 1, X_scaled[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = svm_poly.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z, cmap=plt.cm.viridis, alpha=0.8)
Plot the dataset
plt.scatter(X_scaled[:, 0], X_scaled[:, 1], c=y, cmap='viridis') plt.title('SVM Decision Boundaries with Polynomial Kernel') plt.xlabel('Feature 1') plt.ylabel('Feature 2') plt.show() '''
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file shimpiproductions-3.0-0.1.tar.gz.
File metadata
- Download URL: shimpiproductions-3.0-0.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11ab517f5cd5140725183c40c737f9a25dd897474bd9ca8f3c7e3cdec9519542
|
|
| MD5 |
1627408c50154b261a5480727e346333
|
|
| BLAKE2b-256 |
54814eb495391f36f0c5364dfb3511009dec8ed481f56595cbf14957164fe905
|
File details
Details for the file shimpiproductions_3.0-0.1-py3-none-any.whl.
File metadata
- Download URL: shimpiproductions_3.0-0.1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6a2afb50e5ad24fa15c24e57ac4bd9faf0d50536bfe79c64774e1656db83e54
|
|
| MD5 |
a9cb1f2d64e0580ac4e55fbaf1ad0285
|
|
| BLAKE2b-256 |
bdf8e81f072de2569891774d7ef13386bb05591043903b72ecc02b1ee5de2950
|