Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| from sklearn.linear_model import LinearRegression | |
| import matplotlib.pyplot as plt | |
| import io | |
| def linear_regression(input_csv, x_column, y_column): | |
| # Load dataset | |
| df = pd.read_csv(input_csv) | |
| # Prepare data for regression | |
| X = df[[x_column]].values | |
| y = df[y_column].values | |
| # Perform linear regression | |
| model = LinearRegression() | |
| model.fit(X, y) | |
| # Make predictions | |
| y_pred = model.predict(X) | |
| # Plotting | |
| plt.figure(figsize=(10, 6)) | |
| plt.scatter(X, y, color='blue') | |
| plt.plot(X, y_pred, color='red') | |
| plt.xlabel(x_column) | |
| plt.ylabel(y_column) | |
| plt.title('Linear Regression') | |
| # Save plot to a buffer | |
| buf = io.BytesIO() | |
| plt.savefig(buf, format='png') | |
| buf.seek(0) | |
| # Regression info | |
| coef_info = f"Coefficient: {model.coef_[0]}\nIntercept: {model.intercept_}" | |
| return buf, coef_info | |
| # Gradio interface | |
| iface = gr.Interface( | |
| fn=linear_regression, | |
| inputs=[ | |
| gr.inputs.File(type="csv"), | |
| gr.inputs.Textbox(label="X Column Name"), | |
| gr.inputs.Textbox(label="Y Column Name"), | |
| ], | |
| outputs=[ | |
| gr.outputs.Image(type="plot"), | |
| gr.outputs.Textbox(label="Regression Info") | |
| ], | |
| title="Automatic Linear Regression Modeling", | |
| description="Upload a CSV file and specify the columns for performing linear regression." | |
| ) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| iface.launch() | |