autofuzzts 0.1.2__py3-none-any.whl → 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,107 +1,107 @@
1
- ## Functions for fuzzy clustering
2
- import numpy as np
3
- import pandas as pd
4
-
5
-
6
- def fuzzy_partition_cosine(X: pd.Series, n: int):
7
- """
8
- Midsteps of the calculation:
9
-
10
- D - distance vector (D) represents the relative position of each data point within the partition
11
- h - height, spread of the fuzzy sets
12
- """
13
-
14
- n_rows = len(X)
15
- x_min = X.min()
16
- x_max = X.max()
17
-
18
- D = np.linspace(x_min, x_max, n)
19
- h = (D[-1] - D[0]) / (n - 1)
20
-
21
- A = np.zeros((n_rows, n))
22
-
23
- for k in range(n_rows):
24
- # First column
25
- if (D[0] <= X[k]) and (X[k] <= D[1]):
26
- A[k, 0] = 0.5 * (np.cos(np.pi * (X[k] - D[0]) / h) + 1)
27
-
28
- # Last column
29
- elif (D[n - 2] <= X[k]) and (X[k] <= D[n - 1]):
30
- A[k, n - 1] = 0.5 * (np.cos(np.pi * (X[k] - D[n - 1]) / h) + 1)
31
-
32
- # All other columns
33
- for j in range(1, n - 1):
34
- if (D[j - 1] <= X[k]) and (X[k] <= D[j + 1]):
35
- A[k, j] = 0.5 * (np.cos(np.pi * (X[k] - D[j]) / h) + 1)
36
-
37
- return D, A
38
-
39
-
40
- def fuzzy_partition_triangle(X: pd.Series, n: int):
41
- """
42
- Midsteps of the calculation:
43
-
44
- D - distance vector (D) represents the relative position of each data point within the partition
45
- h - height, spread of the fuzzy sets
46
- """
47
-
48
- n_rows = len(X)
49
- x_min = X.min()
50
- x_max = X.max()
51
-
52
- D = np.linspace(x_min, x_max, n)
53
- h = (D[-1] - D[0]) / (n - 1)
54
-
55
- A = np.zeros((n_rows, n))
56
-
57
- for k in range(n_rows):
58
- # First column
59
- if (D[0] <= X[k]) and (X[k] <= D[1]):
60
- A[k, 0] = (D[1] - X[k]) / h
61
-
62
- # Last column
63
- elif (D[n - 2] <= X[k]) and (X[k] <= D[n - 1]):
64
- A[k, n - 1] = (X[k] - D[n - 2]) / h
65
-
66
- # All other columns
67
- for j in range(1, n - 1):
68
- if (D[j - 1] <= X[k]) and (X[k] <= D[j]):
69
- A[k, j] = (X[k] - D[j - 1]) / h
70
-
71
- if (D[j] <= X[k]) and (X[k] <= D[j + 1]):
72
- A[k, j] = (D[j + 1] - X[k]) / h
73
-
74
- return D, A
75
-
76
-
77
- def fuzzy_partition_gauss(X: pd.Series, n: int, sigma: float = 1):
78
- """
79
- Midsteps of the calculation:
80
-
81
- D - distance vector (D) represents the relative position of each data point within the partition
82
- h - height, spread of the fuzzy sets
83
- sigma - standard deviation of the Gaussian function
84
- """
85
-
86
- n_rows = len(X)
87
- x_min = X.min()
88
- x_max = X.max()
89
-
90
- D = np.linspace(x_min, x_max, n)
91
- A = np.zeros((n_rows, n))
92
-
93
- for k in range(n_rows):
94
- # First column
95
- if (D[0] <= X[k]) and (X[k] <= D[1]):
96
- A[k, 0] = np.exp(-((X[k] - D[0]) ** 2) / (2 * sigma**2))
97
-
98
- # Last column
99
- elif (D[n - 2] <= X[k]) and (X[k] <= D[n - 1]):
100
- A[k, n - 1] = np.exp(-((X[k] - D[n - 1]) ** 2) / (2 * sigma**2))
101
-
102
- # All other columns
103
- for j in range(1, n - 1):
104
- if (D[j - 1] <= X[k]) and (X[k] <= D[j + 1]):
105
- A[k, j] = np.exp(-((X[k] - D[j]) ** 2) / (2 * sigma**2))
106
-
107
- return D, A
1
+ ## Functions for fuzzy partitioning
2
+ import numpy as np
3
+ import pandas as pd
4
+
5
+
6
+ def fuzzy_partition_cosine(X: pd.Series, n: int):
7
+ """
8
+ Midsteps of the calculation:
9
+
10
+ D - distance vector (D) represents the relative position of each data point within the partition
11
+ h - height, spread of the fuzzy sets
12
+ """
13
+
14
+ n_rows = len(X)
15
+ x_min = X.min()
16
+ x_max = X.max()
17
+
18
+ D = np.linspace(x_min, x_max, n)
19
+ h = (D[-1] - D[0]) / (n - 1)
20
+
21
+ A = np.zeros((n_rows, n))
22
+
23
+ for k in range(n_rows):
24
+ # First column
25
+ if (D[0] <= X[k]) and (X[k] <= D[1]):
26
+ A[k, 0] = 0.5 * (np.cos(np.pi * (X[k] - D[0]) / h) + 1)
27
+
28
+ # Last column
29
+ elif (D[n - 2] <= X[k]) and (X[k] <= D[n - 1]):
30
+ A[k, n - 1] = 0.5 * (np.cos(np.pi * (X[k] - D[n - 1]) / h) + 1)
31
+
32
+ # All other columns
33
+ for j in range(1, n - 1):
34
+ if (D[j - 1] <= X[k]) and (X[k] <= D[j + 1]):
35
+ A[k, j] = 0.5 * (np.cos(np.pi * (X[k] - D[j]) / h) + 1)
36
+
37
+ return D, A
38
+
39
+
40
+ def fuzzy_partition_triangle(X: pd.Series, n: int):
41
+ """
42
+ Midsteps of the calculation:
43
+
44
+ D - distance vector (D) represents the relative position of each data point within the partition
45
+ h - height, spread of the fuzzy sets
46
+ """
47
+
48
+ n_rows = len(X)
49
+ x_min = X.min()
50
+ x_max = X.max()
51
+
52
+ D = np.linspace(x_min, x_max, n)
53
+ h = (D[-1] - D[0]) / (n - 1)
54
+
55
+ A = np.zeros((n_rows, n))
56
+
57
+ for k in range(n_rows):
58
+ # First column
59
+ if (D[0] <= X[k]) and (X[k] <= D[1]):
60
+ A[k, 0] = (D[1] - X[k]) / h
61
+
62
+ # Last column
63
+ elif (D[n - 2] <= X[k]) and (X[k] <= D[n - 1]):
64
+ A[k, n - 1] = (X[k] - D[n - 2]) / h
65
+
66
+ # All other columns
67
+ for j in range(1, n - 1):
68
+ if (D[j - 1] <= X[k]) and (X[k] <= D[j]):
69
+ A[k, j] = (X[k] - D[j - 1]) / h
70
+
71
+ if (D[j] <= X[k]) and (X[k] <= D[j + 1]):
72
+ A[k, j] = (D[j + 1] - X[k]) / h
73
+
74
+ return D, A
75
+
76
+
77
+ def fuzzy_partition_gauss(X: pd.Series, n: int, sigma: float = 1):
78
+ """
79
+ Midsteps of the calculation:
80
+
81
+ D - distance vector (D) represents the relative position of each data point within the partition
82
+ h - height, spread of the fuzzy sets
83
+ sigma - standard deviation of the Gaussian function
84
+ """
85
+
86
+ n_rows = len(X)
87
+ x_min = X.min()
88
+ x_max = X.max()
89
+
90
+ D = np.linspace(x_min, x_max, n)
91
+ A = np.zeros((n_rows, n))
92
+
93
+ for k in range(n_rows):
94
+ # First column
95
+ if (D[0] <= X[k]) and (X[k] <= D[1]):
96
+ A[k, 0] = np.exp(-((X[k] - D[0]) ** 2) / (2 * sigma**2))
97
+
98
+ # Last column
99
+ elif (D[n - 2] <= X[k]) and (X[k] <= D[n - 1]):
100
+ A[k, n - 1] = np.exp(-((X[k] - D[n - 1]) ** 2) / (2 * sigma**2))
101
+
102
+ # All other columns
103
+ for j in range(1, n - 1):
104
+ if (D[j - 1] <= X[k]) and (X[k] <= D[j + 1]):
105
+ A[k, j] = np.exp(-((X[k] - D[j]) ** 2) / (2 * sigma**2))
106
+
107
+ return D, A
@@ -1,110 +1,110 @@
1
- import numpy as np
2
- import pandas as pd
3
- from typing import Union, Literal
4
- import warnings
5
- from sklearn.preprocessing import MinMaxScaler
6
-
7
- from autofuzzts.partition.fuzzy_clust_fun import (
8
- fuzzy_partition_cosine,
9
- fuzzy_partition_triangle,
10
- fuzzy_partition_gauss,
11
- )
12
-
13
- class FuzzyPartition:
14
- def __init__(self, fuzzy_function: Literal["cosine", "triangle", "gauss"], n_clusters: int, sigma: float, scaler: MinMaxScaler, verbosity: bool = False):
15
- self.fuzzy_function = self._get_fuzzy_partition_func(fuzzy_function)
16
- self.n_clusters = n_clusters
17
- self.sigma = sigma
18
- self.verbosity = verbosity
19
- self.scaler = scaler
20
-
21
- if scaler is None: # Check if scaler is None
22
- warnings.warn("Scaler must be provided for inverse transformation.")
23
-
24
- def _get_fuzzy_partition_func(self, fuzzy_part_func: Union[str, None]):
25
- if fuzzy_part_func == "cosine":
26
- return fuzzy_partition_cosine # Replace with actual function
27
- elif fuzzy_part_func == "triangle":
28
- return fuzzy_partition_triangle # Replace with actual function
29
- elif fuzzy_part_func == "gauss":
30
- return fuzzy_partition_gauss # Replace with actual function
31
- else:
32
- return fuzzy_partition_cosine # Default function
33
-
34
- def fuzzy_partition(self, X: np.ndarray) -> pd.DataFrame:
35
- """
36
- Perform fuzzy partitioning on the target variable X.
37
-
38
- Parameters:
39
- X (np.ndarray): Input data to be partitioned.
40
-
41
- Returns:
42
- pd.DataFrame: DataFrame containing partition results.
43
- """
44
- # Perform fuzzy partitioning using the selected function
45
- if self.fuzzy_function.__name__ == "fuzzy_partition_gauss":
46
- D, A = self.fuzzy_function(X=X, n=self.n_clusters, sigma=self.sigma)
47
- else:
48
- D, A = self.fuzzy_function(X=X, n=self.n_clusters)
49
-
50
- center_points = list(D.flatten())
51
- center_points = [round(i, 2) for i in center_points]
52
- center_points = np.array(center_points)
53
-
54
- if self.verbosity:
55
- print("Cluster center points:", center_points)
56
-
57
- # Unscaled center points
58
- center_points_unscaled = self.scaler.inverse_transform(
59
- center_points.reshape(-1, 1)
60
- )
61
- self.center_points_unscaled = center_points_unscaled.flatten()
62
- if self.verbosity:
63
- print("Cluster center points unscaled:", self.center_points_unscaled.flatten())
64
-
65
- # Create a DataFrame for membership values
66
- A_df = pd.DataFrame(A)
67
- A_df.columns = ["set_" + str(i) for i in range(A_df.shape[1])]
68
-
69
- # Prepare the fuzzy partition DataFrame
70
- fp_df = A_df.copy()
71
- fp_df.insert(0, "X_value", X)
72
- fp_df["membership_value"] = fp_df.iloc[:, 1:].max(axis=1)
73
- fp_df["cluster"] = fp_df.iloc[:, 1:].idxmax(axis=1)
74
-
75
- # Initialize 'left' and 'right' columns
76
- fp_df["left"] = 0
77
- fp_df["right"] = 0
78
-
79
- # Define sets for left and right logic
80
- set_min = "set_0"
81
- set_max = "set_" + str(len(center_points) - 1)
82
-
83
- # Set left and right for min and max sets
84
- fp_df.loc[fp_df["cluster"] == set_min, "right"] = 1
85
- fp_df.loc[fp_df["cluster"] == set_max, "left"] = 1
86
-
87
- fp_df["center_point"] = ""
88
- fp_df.loc[fp_df["cluster"] == set_min, "center_point"] = 0
89
- fp_df.loc[fp_df["cluster"] == set_max, "center_point"] = 1
90
-
91
- # Logic for intermediate clusters
92
- for i in range(1, len(center_points) - 1):
93
- set_i = "set_" + str(i)
94
- fp_df.loc[fp_df["cluster"] == set_i, "center_point"] = center_points[i]
95
- fp_df.loc[
96
- (fp_df["cluster"] == set_i) & (fp_df["X_value"] >= center_points[i]),
97
- "right",
98
- ] = 1
99
- fp_df.loc[
100
- (fp_df["cluster"] == set_i) & (fp_df["X_value"] < center_points[i]),
101
- "left",
102
- ] = 1
103
-
104
- # Ensure membership values are non-negative
105
- fp_df.loc[fp_df["membership_value"] < 0, "membership_value"] = 0
106
-
107
- # Keep only relevant columns
108
- fp_df = fp_df.loc[:, ["X_value", "membership_value", "cluster", "left"]]
109
-
1
+ import numpy as np
2
+ import pandas as pd
3
+ from typing import Union, Literal
4
+ import warnings
5
+ from sklearn.preprocessing import MinMaxScaler
6
+
7
+ from autofuzzts.partition.fuzzy_part_fun import (
8
+ fuzzy_partition_cosine,
9
+ fuzzy_partition_triangle,
10
+ fuzzy_partition_gauss,
11
+ )
12
+
13
+ class FuzzyPartition:
14
+ def __init__(self, fuzzy_function: Literal["cosine", "triangle", "gauss"], n_fuzzy_sets: int, sigma: float, scaler: MinMaxScaler, verbosity: bool = False):
15
+ self.fuzzy_function = self._get_fuzzy_partition_func(fuzzy_function)
16
+ self.n_fuzzy_sets = n_fuzzy_sets
17
+ self.sigma = sigma
18
+ self.verbosity = verbosity
19
+ self.scaler = scaler
20
+
21
+ if scaler is None: # Check if scaler is None
22
+ warnings.warn("Scaler must be provided for inverse transformation.")
23
+
24
+ def _get_fuzzy_partition_func(self, fuzzy_part_func: Union[str, None]):
25
+ if fuzzy_part_func == "cosine":
26
+ return fuzzy_partition_cosine # Replace with actual function
27
+ elif fuzzy_part_func == "triangle":
28
+ return fuzzy_partition_triangle # Replace with actual function
29
+ elif fuzzy_part_func == "gauss":
30
+ return fuzzy_partition_gauss # Replace with actual function
31
+ else:
32
+ return fuzzy_partition_cosine # Default function
33
+
34
+ def fuzzy_partition(self, X: np.ndarray) -> pd.DataFrame:
35
+ """
36
+ Perform fuzzy partitioning on the target variable X.
37
+
38
+ Parameters:
39
+ X (np.ndarray): Input data to be partitioned.
40
+
41
+ Returns:
42
+ pd.DataFrame: DataFrame containing partition results.
43
+ """
44
+ # Perform fuzzy partitioning using the selected function
45
+ if self.fuzzy_function.__name__ == "fuzzy_partition_gauss":
46
+ D, A = self.fuzzy_function(X=X, n=self.n_fuzzy_sets, sigma=self.sigma)
47
+ else:
48
+ D, A = self.fuzzy_function(X=X, n=self.n_fuzzy_sets)
49
+
50
+ center_points = list(D.flatten())
51
+ center_points = [round(i, 2) for i in center_points]
52
+ center_points = np.array(center_points)
53
+
54
+ if self.verbosity:
55
+ print("Fuzzy set center points:", center_points)
56
+
57
+ # Unscaled center points
58
+ center_points_unscaled = self.scaler.inverse_transform(
59
+ center_points.reshape(-1, 1)
60
+ )
61
+ self.center_points_unscaled = center_points_unscaled.flatten()
62
+ if self.verbosity:
63
+ print("fuzzy_set center points unscaled:", self.center_points_unscaled.flatten())
64
+
65
+ # Create a DataFrame for membership values
66
+ A_df = pd.DataFrame(A)
67
+ A_df.columns = ["set_" + str(i) for i in range(A_df.shape[1])]
68
+
69
+ # Prepare the fuzzy partition DataFrame
70
+ fp_df = A_df.copy()
71
+ fp_df.insert(0, "X_value", X)
72
+ fp_df["membership_value"] = fp_df.iloc[:, 1:].max(axis=1)
73
+ fp_df["fuzzy_set"] = fp_df.iloc[:, 1:].idxmax(axis=1)
74
+
75
+ # Initialize 'left' and 'right' columns
76
+ fp_df["left"] = 0
77
+ fp_df["right"] = 0
78
+
79
+ # Define sets for left and right logic
80
+ set_min = "set_0"
81
+ set_max = "set_" + str(len(center_points) - 1)
82
+
83
+ # Set left and right for min and max sets
84
+ fp_df.loc[fp_df["fuzzy_set"] == set_min, "right"] = 1
85
+ fp_df.loc[fp_df["fuzzy_set"] == set_max, "left"] = 1
86
+
87
+ fp_df["center_point"] = ""
88
+ fp_df.loc[fp_df["fuzzy_set"] == set_min, "center_point"] = 0
89
+ fp_df.loc[fp_df["fuzzy_set"] == set_max, "center_point"] = 1
90
+
91
+ # Logic for intermediate fuzzy_sets
92
+ for i in range(1, len(center_points) - 1):
93
+ set_i = "set_" + str(i)
94
+ fp_df.loc[fp_df["fuzzy_set"] == set_i, "center_point"] = center_points[i]
95
+ fp_df.loc[
96
+ (fp_df["fuzzy_set"] == set_i) & (fp_df["X_value"] >= center_points[i]),
97
+ "right",
98
+ ] = 1
99
+ fp_df.loc[
100
+ (fp_df["fuzzy_set"] == set_i) & (fp_df["X_value"] < center_points[i]),
101
+ "left",
102
+ ] = 1
103
+
104
+ # Ensure membership values are non-negative
105
+ fp_df.loc[fp_df["membership_value"] < 0, "membership_value"] = 0
106
+
107
+ # Keep only relevant columns
108
+ fp_df = fp_df.loc[:, ["X_value", "membership_value", "fuzzy_set", "left"]]
109
+
110
110
  return fp_df, center_points, center_points_unscaled.flatten()
@@ -1,32 +1,32 @@
1
- import numpy as np
2
- import matplotlib.pyplot as plt
3
-
4
-
5
- def visualize_partition(fp_df, center_points):
6
- plt.figure(figsize=(6, 3))
7
-
8
- # Scatter plot with size based on membership value
9
- plt.scatter(
10
- fp_df["X_value"],
11
- fp_df["membership_value"],
12
- c=fp_df["cluster"].astype("category").cat.codes,
13
- cmap="viridis",
14
- s=50,
15
- )
16
- plt.xlabel("X")
17
- plt.ylabel("Membership Value")
18
- plt.title("Fuzzy Partition")
19
-
20
- # Plot center points with horizontal line at y=0.5
21
- plt.plot(center_points, np.ones_like(center_points) * 0.5, "x", markersize=10)
22
-
23
- # Add labels for center points with slight vertical offset
24
- for i, txt in enumerate(center_points):
25
- plt.annotate(
26
- txt,
27
- (center_points[i], 0.5 + 0.015),
28
- horizontalalignment="center",
29
- fontsize=8,
30
- )
31
-
32
- plt.show()
1
+ import numpy as np
2
+ import matplotlib.pyplot as plt
3
+
4
+
5
+ def visualize_partition(fp_df, center_points):
6
+ plt.figure(figsize=(6, 3))
7
+
8
+ # Scatter plot with size based on membership value
9
+ plt.scatter(
10
+ fp_df["X_value"],
11
+ fp_df["membership_value"],
12
+ c=fp_df["fuzzy_set"].astype("category").cat.codes,
13
+ cmap="viridis",
14
+ s=50,
15
+ )
16
+ plt.xlabel("X")
17
+ plt.ylabel("Membership Value")
18
+ plt.title("Fuzzy Partition")
19
+
20
+ # Plot center points with horizontal line at y=0.5
21
+ plt.plot(center_points, np.ones_like(center_points) * 0.5, "x", markersize=10)
22
+
23
+ # Add labels for center points with slight vertical offset
24
+ for i, txt in enumerate(center_points):
25
+ plt.annotate(
26
+ txt,
27
+ (center_points[i], 0.5 + 0.015),
28
+ horizontalalignment="center",
29
+ fontsize=8,
30
+ )
31
+
32
+ plt.show()