mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Tighten up psf/black and flake8 (#2024)
* Tighten up psf/black and flake8
* Fix some tests
* Fix some E741
* Fix some E741
* updating DIRECTORY.md
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -11,9 +11,11 @@ Python:
|
||||
Inputs:
|
||||
- X , a 2D numpy array of features.
|
||||
- k , number of clusters to create.
|
||||
- initial_centroids , initial centroid values generated by utility function(mentioned in usage).
|
||||
- initial_centroids , initial centroid values generated by utility function(mentioned
|
||||
in usage).
|
||||
- maxiter , maximum number of iterations to process.
|
||||
- heterogeneity , empty list that will be filled with hetrogeneity values if passed to kmeans func.
|
||||
- heterogeneity , empty list that will be filled with hetrogeneity values if passed
|
||||
to kmeans func.
|
||||
|
||||
Usage:
|
||||
1. define 'k' value, 'X' features array and 'hetrogeneity' empty list
|
||||
@@ -22,7 +24,8 @@ Usage:
|
||||
initial_centroids = get_initial_centroids(
|
||||
X,
|
||||
k,
|
||||
seed=0 # seed value for initial centroid generation, None for randomness(default=None)
|
||||
seed=0 # seed value for initial centroid generation,
|
||||
# None for randomness(default=None)
|
||||
)
|
||||
|
||||
3. find centroids and clusters using kmeans function.
|
||||
@@ -37,7 +40,8 @@ Usage:
|
||||
)
|
||||
|
||||
|
||||
4. Plot the loss function, hetrogeneity values for every iteration saved in hetrogeneity list.
|
||||
4. Plot the loss function, hetrogeneity values for every iteration saved in
|
||||
hetrogeneity list.
|
||||
plot_heterogeneity(
|
||||
heterogeneity,
|
||||
k
|
||||
@@ -46,8 +50,9 @@ Usage:
|
||||
5. Have fun..
|
||||
|
||||
"""
|
||||
from sklearn.metrics import pairwise_distances
|
||||
import numpy as np
|
||||
from matplotlib import pyplot as plt
|
||||
from sklearn.metrics import pairwise_distances
|
||||
|
||||
TAG = "K-MEANS-CLUST/ "
|
||||
|
||||
@@ -118,9 +123,6 @@ def compute_heterogeneity(data, k, centroids, cluster_assignment):
|
||||
return heterogeneity
|
||||
|
||||
|
||||
from matplotlib import pyplot as plt
|
||||
|
||||
|
||||
def plot_heterogeneity(heterogeneity, k):
|
||||
plt.figure(figsize=(7, 4))
|
||||
plt.plot(heterogeneity, linewidth=4)
|
||||
@@ -136,9 +138,11 @@ def kmeans(
|
||||
):
|
||||
"""This function runs k-means on given data and initial set of centroids.
|
||||
maxiter: maximum number of iterations to run.(default=500)
|
||||
record_heterogeneity: (optional) a list, to store the history of heterogeneity as function of iterations
|
||||
record_heterogeneity: (optional) a list, to store the history of heterogeneity
|
||||
as function of iterations
|
||||
if None, do not store the history.
|
||||
verbose: if True, print how many data points changed their cluster labels in each iteration"""
|
||||
verbose: if True, print how many data points changed their cluster labels in
|
||||
each iteration"""
|
||||
centroids = initial_centroids[:]
|
||||
prev_cluster_assignment = None
|
||||
|
||||
@@ -149,7 +153,8 @@ def kmeans(
|
||||
# 1. Make cluster assignments using nearest centroids
|
||||
cluster_assignment = assign_clusters(data, centroids)
|
||||
|
||||
# 2. Compute a new centroid for each of the k clusters, averaging all data points assigned to that cluster.
|
||||
# 2. Compute a new centroid for each of the k clusters, averaging all data
|
||||
# points assigned to that cluster.
|
||||
centroids = revise_centroids(data, k, cluster_assignment)
|
||||
|
||||
# Check for convergence: if none of the assignments changed, stop
|
||||
|
||||
@@ -186,7 +186,8 @@ def predict_y_values(
|
||||
>>> means = [5.011267842911003, 10.011267842911003, 15.011267842911002]
|
||||
>>> variance = 0.9618530973487494
|
||||
>>> probabilities = [0.3333333333333333, 0.3333333333333333, 0.3333333333333333]
|
||||
>>> predict_y_values(x_items, means, variance, probabilities) # doctest: +NORMALIZE_WHITESPACE
|
||||
>>> predict_y_values(x_items, means, variance,
|
||||
... probabilities) # doctest: +NORMALIZE_WHITESPACE
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 2, 2, 2, 2, 2, 2]
|
||||
@@ -211,7 +212,7 @@ def predict_y_values(
|
||||
# appending discriminant values of each item to 'results' list
|
||||
results.append(temp)
|
||||
|
||||
return [l.index(max(l)) for l in results]
|
||||
return [result.index(max(result)) for result in results]
|
||||
|
||||
|
||||
# Calculating Accuracy
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
from sklearn.linear_model import LinearRegression
|
||||
|
||||
# Splitting the dataset into the Training set and Test set
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
# Fitting Polynomial Regression to the dataset
|
||||
from sklearn.preprocessing import PolynomialFeatures
|
||||
|
||||
# Importing the dataset
|
||||
dataset = pd.read_csv(
|
||||
@@ -9,16 +16,9 @@ X = dataset.iloc[:, 1:2].values
|
||||
y = dataset.iloc[:, 2].values
|
||||
|
||||
|
||||
# Splitting the dataset into the Training set and Test set
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
|
||||
|
||||
|
||||
# Fitting Polynomial Regression to the dataset
|
||||
from sklearn.preprocessing import PolynomialFeatures
|
||||
from sklearn.linear_model import LinearRegression
|
||||
|
||||
poly_reg = PolynomialFeatures(degree=4)
|
||||
X_poly = poly_reg.fit_transform(X)
|
||||
pol_reg = LinearRegression()
|
||||
|
||||
@@ -14,6 +14,7 @@ import numpy as np
|
||||
and types of data
|
||||
"""
|
||||
|
||||
|
||||
# Mean Absolute Error
|
||||
def mae(predict, actual):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user