Compare commits
3 Commits
csv-delimi
...
navigation
Author | SHA1 | Date | |
---|---|---|---|
6644d60fa2 | |||
![]() |
c190656165 | ||
![]() |
fd3d6e3b01 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +0,0 @@
|
|||||||
__pycache__
|
|
||||||
.venv
|
|
@@ -1,6 +1,5 @@
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import streamlit as st
|
import streamlit as st
|
||||||
import codecs
|
|
||||||
|
|
||||||
st.set_page_config(
|
st.set_page_config(
|
||||||
page_title="Project Miner",
|
page_title="Project Miner",
|
||||||
@@ -10,14 +9,10 @@ st.set_page_config(
|
|||||||
st.title("Home")
|
st.title("Home")
|
||||||
|
|
||||||
### Exploration
|
### Exploration
|
||||||
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv", "tsv"])
|
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
|
||||||
separator = st.selectbox("Separator", [",", ";", "\\t"])
|
|
||||||
separator = codecs.getdecoder("unicode_escape")(separator)[0]
|
|
||||||
has_header = st.checkbox("Has header", value=True)
|
|
||||||
|
|
||||||
if uploaded_file is not None:
|
if uploaded_file is not None:
|
||||||
st.session_state.data = pd.read_csv(uploaded_file, sep=separator, header=0 if has_header else 1)
|
st.session_state.data = pd.read_csv(uploaded_file)
|
||||||
st.session_state.original_data = st.session_state.data
|
|
||||||
st.success("File loaded successfully!")
|
st.success("File loaded successfully!")
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,179 +0,0 @@
|
|||||||
from abc import ABC, abstractmethod
|
|
||||||
from pandas import DataFrame, Series
|
|
||||||
from pandas.api.types import is_numeric_dtype
|
|
||||||
from sklearn.neighbors import KNeighborsClassifier
|
|
||||||
from typing import Any, Union
|
|
||||||
|
|
||||||
class DataFrameFunction(ABC):
|
|
||||||
"""A command that may be applied in-place to a dataframe."""
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def apply(self, df: DataFrame, label: str, series: Series) -> DataFrame:
|
|
||||||
"""Apply the current function to the given dataframe, in-place.
|
|
||||||
|
|
||||||
The series is described by its label and dataframe."""
|
|
||||||
return df
|
|
||||||
|
|
||||||
|
|
||||||
class MVStrategy(DataFrameFunction):
|
|
||||||
"""A way to handle missing values in a dataframe."""
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def list_available(df: DataFrame, label: str, series: Series) -> list['MVStrategy']:
|
|
||||||
"""Get all the strategies that can be used."""
|
|
||||||
choices = [DropStrategy(), ModeStrategy()]
|
|
||||||
if is_numeric_dtype(series):
|
|
||||||
choices.extend((MeanStrategy(), MedianStrategy(), LinearRegressionStrategy()))
|
|
||||||
other_columns = df.select_dtypes(include="number").drop(label, axis=1).columns.to_list()
|
|
||||||
if len(other_columns):
|
|
||||||
choices.append(KNNStrategy(other_columns))
|
|
||||||
return choices
|
|
||||||
|
|
||||||
|
|
||||||
class ScalingStrategy(DataFrameFunction):
|
|
||||||
"""A way to handle missing values in a dataframe."""
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def list_available(df: DataFrame, series: Series) -> list['MVStrategy']:
|
|
||||||
"""Get all the strategies that can be used."""
|
|
||||||
choices = [KeepStrategy()]
|
|
||||||
if is_numeric_dtype(series):
|
|
||||||
choices.extend((MinMaxStrategy(), ZScoreStrategy()))
|
|
||||||
if series.sum() != 0:
|
|
||||||
choices.append(UnitLengthStrategy())
|
|
||||||
return choices
|
|
||||||
|
|
||||||
|
|
||||||
class DropStrategy(MVStrategy):
|
|
||||||
#@typing.override
|
|
||||||
def apply(self, df: DataFrame, label: str, series: Series) -> DataFrame:
|
|
||||||
df.dropna(subset=label, inplace=True)
|
|
||||||
return df
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
|
||||||
return "Drop"
|
|
||||||
|
|
||||||
|
|
||||||
class PositionStrategy(MVStrategy):
|
|
||||||
#@typing.override
|
|
||||||
def apply(self, df: DataFrame, label: str, series: Series) -> DataFrame:
|
|
||||||
series.fillna(self.get_value(series), inplace=True)
|
|
||||||
return df
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def get_value(self, series: Series) -> Any:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class MeanStrategy(PositionStrategy):
|
|
||||||
#@typing.override
|
|
||||||
def get_value(self, series: Series) -> Union[int, float]:
|
|
||||||
return series.mean()
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
|
||||||
return "Use mean"
|
|
||||||
|
|
||||||
|
|
||||||
class MedianStrategy(PositionStrategy):
|
|
||||||
#@typing.override
|
|
||||||
def get_value(self, series: Series) -> Union[int, float]:
|
|
||||||
return series.median()
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
|
||||||
return "Use median"
|
|
||||||
|
|
||||||
|
|
||||||
class ModeStrategy(PositionStrategy):
|
|
||||||
#@typing.override
|
|
||||||
def get_value(self, series: Series) -> Any:
|
|
||||||
return series.mode()[0]
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
|
||||||
return "Use mode"
|
|
||||||
|
|
||||||
|
|
||||||
class LinearRegressionStrategy(MVStrategy):
|
|
||||||
def apply(self, df: DataFrame, label: str, series: Series) -> DataFrame:
|
|
||||||
series.interpolate(inplace=True)
|
|
||||||
return df
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
|
||||||
return "Use linear regression"
|
|
||||||
|
|
||||||
|
|
||||||
class KNNStrategy(MVStrategy):
|
|
||||||
def __init__(self, training_features: list[str]):
|
|
||||||
self.available_features = training_features
|
|
||||||
self.training_features = training_features
|
|
||||||
self.n_neighbors = 3
|
|
||||||
|
|
||||||
def apply(self, df: DataFrame, label: str, series: Series) -> DataFrame:
|
|
||||||
# Remove any training column that have any missing values
|
|
||||||
usable_data = df.dropna(subset=self.training_features)
|
|
||||||
# Select columns to impute from
|
|
||||||
train_data = usable_data.dropna(subset=label)
|
|
||||||
# Create train dataframe
|
|
||||||
x_train = train_data.drop(label, axis=1)
|
|
||||||
y_train = train_data[label]
|
|
||||||
|
|
||||||
reg = KNeighborsClassifier(self.n_neighbors).fit(x_train, y_train)
|
|
||||||
|
|
||||||
# Create test dataframe
|
|
||||||
test_data = usable_data[usable_data[label].isnull()]
|
|
||||||
if test_data.empty:
|
|
||||||
return df
|
|
||||||
x_test = test_data.drop(label, axis=1)
|
|
||||||
predicted = reg.predict(x_test)
|
|
||||||
|
|
||||||
# Fill with predicated values and patch the original data
|
|
||||||
usable_data[label].fillna(Series(predicted), inplace=True)
|
|
||||||
df.fillna(usable_data, inplace=True)
|
|
||||||
return df
|
|
||||||
|
|
||||||
def count_max(self, df: DataFrame, label: str) -> int:
|
|
||||||
usable_data = df.dropna(subset=self.training_features)
|
|
||||||
return usable_data[label].count()
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
|
||||||
return "kNN"
|
|
||||||
|
|
||||||
|
|
||||||
class KeepStrategy(ScalingStrategy):
|
|
||||||
#@typing.override
|
|
||||||
def apply(self, df: DataFrame, label: str, series: Series) -> DataFrame:
|
|
||||||
return df
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
|
||||||
return "No-op"
|
|
||||||
|
|
||||||
|
|
||||||
class MinMaxStrategy(ScalingStrategy):
|
|
||||||
#@typing.override
|
|
||||||
def apply(self, df: DataFrame, label: str, series: Series) -> DataFrame:
|
|
||||||
minimum = series.min()
|
|
||||||
maximum = series.max()
|
|
||||||
df[label] = (series - minimum) / (maximum - minimum)
|
|
||||||
return df
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
|
||||||
return "Min-max"
|
|
||||||
|
|
||||||
|
|
||||||
class ZScoreStrategy(ScalingStrategy):
|
|
||||||
#@typing.override
|
|
||||||
def apply(self, df: DataFrame, label: str, series: Series) -> DataFrame:
|
|
||||||
df[label] = (series - series.mean()) / series.std()
|
|
||||||
return df
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
|
||||||
return "Z-Score"
|
|
||||||
|
|
||||||
|
|
||||||
class UnitLengthStrategy(ScalingStrategy):
|
|
||||||
#@typing.override
|
|
||||||
def apply(self, df: DataFrame, label: str, series: Series) -> DataFrame:
|
|
||||||
df[label] = series / series.sum()
|
|
||||||
return df
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
|
||||||
return "Unit length"
|
|
@@ -1,35 +0,0 @@
|
|||||||
import streamlit as st
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
from sklearn.cluster import DBSCAN
|
|
||||||
|
|
||||||
st.header("Clustering: dbscan")
|
|
||||||
|
|
||||||
|
|
||||||
if "data" in st.session_state:
|
|
||||||
data = st.session_state.data
|
|
||||||
|
|
||||||
with st.form("my_form"):
|
|
||||||
data_name = st.multiselect("Data Name", data.select_dtypes(include="number").columns, max_selections=3)
|
|
||||||
eps = st.slider("eps", min_value=0.0, max_value=1.0, value=0.5, step=0.01)
|
|
||||||
min_samples = st.number_input("min_samples", step=1, min_value=1, value=5)
|
|
||||||
st.form_submit_button("launch")
|
|
||||||
|
|
||||||
if len(data_name) >= 2 and len(data_name) <=3:
|
|
||||||
x = data[data_name].to_numpy()
|
|
||||||
|
|
||||||
dbscan = DBSCAN(eps=eps, min_samples=min_samples)
|
|
||||||
y_dbscan = dbscan.fit_predict(x)
|
|
||||||
|
|
||||||
fig = plt.figure()
|
|
||||||
if len(data_name) == 2:
|
|
||||||
ax = fig.add_subplot(projection='rectilinear')
|
|
||||||
plt.scatter(x[:, 0], x[:, 1], c=y_dbscan, s=50, cmap="viridis")
|
|
||||||
else:
|
|
||||||
ax = fig.add_subplot(projection='3d')
|
|
||||||
ax.scatter(x[:, 0], x[:, 1],x[:, 2], c=y_dbscan, s=50, cmap="viridis")
|
|
||||||
st.pyplot(fig)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
else:
|
|
||||||
st.error("file not loaded")
|
|
@@ -1,44 +0,0 @@
|
|||||||
import streamlit as st
|
|
||||||
from sklearn.cluster import KMeans
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
|
|
||||||
st.header("Clustering: kmeans")
|
|
||||||
|
|
||||||
|
|
||||||
if "data" in st.session_state:
|
|
||||||
data = st.session_state.data
|
|
||||||
|
|
||||||
with st.form("my_form"):
|
|
||||||
row1 = st.columns([1,1,1])
|
|
||||||
n_clusters = row1[0].selectbox("Number of clusters", range(1,data.shape[0]))
|
|
||||||
data_name = row1[1].multiselect("Data Name",data.select_dtypes(include="number").columns, max_selections=3)
|
|
||||||
n_init = row1[2].number_input("n_init",step=1,min_value=1)
|
|
||||||
|
|
||||||
row2 = st.columns([1,1])
|
|
||||||
max_iter = row1[0].number_input("max_iter",step=1,min_value=1)
|
|
||||||
|
|
||||||
|
|
||||||
st.form_submit_button("launch")
|
|
||||||
|
|
||||||
if len(data_name) >= 2 and len(data_name) <=3:
|
|
||||||
x = data[data_name].to_numpy()
|
|
||||||
|
|
||||||
kmeans = KMeans(n_clusters=n_clusters, init="random", n_init=n_init, max_iter=max_iter, random_state=111)
|
|
||||||
y_kmeans = kmeans.fit_predict(x)
|
|
||||||
|
|
||||||
fig = plt.figure()
|
|
||||||
if len(data_name) == 2:
|
|
||||||
ax = fig.add_subplot(projection='rectilinear')
|
|
||||||
plt.scatter(x[:, 0], x[:, 1], c=y_kmeans, s=50, cmap="viridis")
|
|
||||||
centers = kmeans.cluster_centers_
|
|
||||||
plt.scatter(centers[:, 0], centers[:, 1], c="black", s=200, marker="X")
|
|
||||||
else:
|
|
||||||
ax = fig.add_subplot(projection='3d')
|
|
||||||
|
|
||||||
ax.scatter(x[:, 0], x[:, 1],x[:, 2], c=y_kmeans, s=50, cmap="viridis")
|
|
||||||
centers = kmeans.cluster_centers_
|
|
||||||
ax.scatter(centers[:, 0], centers[:, 1],centers[:, 2], c="black", s=200, marker="X")
|
|
||||||
st.pyplot(fig)
|
|
||||||
|
|
||||||
else:
|
|
||||||
st.error("file not loaded")
|
|
@@ -1,35 +0,0 @@
|
|||||||
import streamlit as st
|
|
||||||
from normstrategy import MVStrategy, ScalingStrategy, KNNStrategy
|
|
||||||
|
|
||||||
if "data" in st.session_state:
|
|
||||||
data = st.session_state.original_data
|
|
||||||
st.session_state.original_data = data.copy()
|
|
||||||
|
|
||||||
for column, series in data.items():
|
|
||||||
col1, col2 = st.columns(2)
|
|
||||||
missing_count = series.isna().sum()
|
|
||||||
choices = MVStrategy.list_available(data, column, series)
|
|
||||||
option = col1.selectbox(
|
|
||||||
f"Missing values of {column} ({missing_count})",
|
|
||||||
choices,
|
|
||||||
index=1,
|
|
||||||
key=f"mv-{column}",
|
|
||||||
)
|
|
||||||
if isinstance(option, KNNStrategy):
|
|
||||||
option.training_features = st.multiselect("Training columns", option.training_features, default=option.available_features, key=f"cols-{column}")
|
|
||||||
option.n_neighbors = st.number_input("Number of neighbors", min_value=1, max_value=option.count_max(data, column), value=option.n_neighbors, key=f"neighbors-{column}")
|
|
||||||
# Always re-get the series to avoid reusing an invalidated series pointer
|
|
||||||
data = option.apply(data, column, data[column])
|
|
||||||
|
|
||||||
choices = ScalingStrategy.list_available(data, series)
|
|
||||||
option = col2.selectbox(
|
|
||||||
"Scaling",
|
|
||||||
choices,
|
|
||||||
key=f"scaling-{column}",
|
|
||||||
)
|
|
||||||
data = option.apply(data, column, data[column])
|
|
||||||
|
|
||||||
st.write(data)
|
|
||||||
st.session_state.data = data
|
|
||||||
else:
|
|
||||||
st.error("file not loaded")
|
|
@@ -1,64 +0,0 @@
|
|||||||
import streamlit as st
|
|
||||||
from sklearn.linear_model import LogisticRegression
|
|
||||||
from sklearn.model_selection import train_test_split
|
|
||||||
from sklearn.metrics import accuracy_score
|
|
||||||
from sklearn.preprocessing import LabelEncoder
|
|
||||||
import pandas as pd
|
|
||||||
|
|
||||||
st.header("Prediction: Classification")
|
|
||||||
|
|
||||||
if "data" in st.session_state:
|
|
||||||
data = st.session_state.data
|
|
||||||
|
|
||||||
with st.form("classification_form"):
|
|
||||||
st.subheader("Classification Parameters")
|
|
||||||
data_name = st.multiselect("Features", data.columns)
|
|
||||||
target_name = st.selectbox("Target", data.columns)
|
|
||||||
test_size = st.slider("Test Size", min_value=0.1, max_value=0.5, value=0.2, step=0.1)
|
|
||||||
st.form_submit_button('Train and Predict')
|
|
||||||
|
|
||||||
if data_name and target_name:
|
|
||||||
X = data[data_name]
|
|
||||||
y = data[target_name]
|
|
||||||
|
|
||||||
label_encoders = {}
|
|
||||||
for column in X.select_dtypes(include=['object']).columns:
|
|
||||||
le = LabelEncoder()
|
|
||||||
X[column] = le.fit_transform(X[column])
|
|
||||||
label_encoders[column] = le
|
|
||||||
|
|
||||||
if y.dtype == 'object':
|
|
||||||
le = LabelEncoder()
|
|
||||||
y = le.fit_transform(y)
|
|
||||||
label_encoders[target_name] = le
|
|
||||||
|
|
||||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=42)
|
|
||||||
|
|
||||||
model = LogisticRegression()
|
|
||||||
model.fit(X_train, y_train)
|
|
||||||
y_pred = model.predict(X_test)
|
|
||||||
accuracy = accuracy_score(y_test, y_pred)
|
|
||||||
|
|
||||||
st.subheader("Model Accuracy")
|
|
||||||
st.write(f"Accuracy on test data: {accuracy:.2f}")
|
|
||||||
|
|
||||||
st.subheader("Enter values for prediction")
|
|
||||||
pred_values = []
|
|
||||||
for feature in data_name:
|
|
||||||
if feature in label_encoders:
|
|
||||||
values = list(label_encoders[feature].classes_)
|
|
||||||
value = st.selectbox(f"Value for {feature}", values)
|
|
||||||
value_encoded = label_encoders[feature].transform([value])[0]
|
|
||||||
pred_values.append(value_encoded)
|
|
||||||
else:
|
|
||||||
value = st.number_input(f"Value for {feature}", value=0.0)
|
|
||||||
pred_values.append(value)
|
|
||||||
|
|
||||||
prediction = model.predict(pd.DataFrame([pred_values], columns=data_name))
|
|
||||||
|
|
||||||
if target_name in label_encoders:
|
|
||||||
prediction = label_encoders[target_name].inverse_transform(prediction)
|
|
||||||
|
|
||||||
st.write("Prediction:", prediction[0])
|
|
||||||
else:
|
|
||||||
st.error("File not loaded")
|
|
@@ -1,29 +0,0 @@
|
|||||||
import streamlit as st
|
|
||||||
from sklearn.linear_model import LinearRegression
|
|
||||||
import pandas as pd
|
|
||||||
|
|
||||||
st.header("Prediction: Regression")
|
|
||||||
|
|
||||||
if "data" in st.session_state:
|
|
||||||
data = st.session_state.data
|
|
||||||
|
|
||||||
with st.form("regression_form"):
|
|
||||||
st.subheader("Linear Regression Parameters")
|
|
||||||
data_name = st.multiselect("Features", data.select_dtypes(include="number").columns)
|
|
||||||
target_name = st.selectbox("Target", data.select_dtypes(include="number").columns)
|
|
||||||
st.form_submit_button('Train and Predict')
|
|
||||||
|
|
||||||
if data_name and target_name:
|
|
||||||
X = data[data_name]
|
|
||||||
y = data[target_name]
|
|
||||||
|
|
||||||
model = LinearRegression()
|
|
||||||
model.fit(X, y)
|
|
||||||
|
|
||||||
st.subheader("Enter values for prediction")
|
|
||||||
pred_values = [st.number_input(f"Value for {feature}", value=0.0) for feature in data_name]
|
|
||||||
prediction = model.predict(pd.DataFrame([pred_values], columns=data_name))
|
|
||||||
|
|
||||||
st.write("Prediction:", prediction[0])
|
|
||||||
else:
|
|
||||||
st.error("File not loaded")
|
|
Reference in New Issue
Block a user