Spaces:
Sleeping
Sleeping
File size: 1,648 Bytes
1014ac5 5bfd5e8 1014ac5 5bfd5e8 dc71877 5bfd5e8 a6fb37c 5bfd5e8 a6fb37c 5bfd5e8 dc71877 5bfd5e8 dc71877 5bfd5e8 dc71877 5bfd5e8 1014ac5 5bfd5e8 a6fb37c 5bfd5e8 dc71877 4548114 5bfd5e8 a6fb37c 1014ac5 5bfd5e8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import gradio as gr
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import io
from PIL import Image
def linear_regression(x_values, y_values):
# Convert string inputs to numpy arrays
X = np.array([float(x) for x in x_values.split(',')]).reshape(-1, 1)
y = np.array([float(y) for y in y_values.split(',')])
# 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 Values")
plt.ylabel("Y Values")
plt.title('Linear Regression')
# Save plot to a buffer and convert to PIL Image
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
image = Image.open(buf)
# Regression info
coef_info = f"Coefficient: {model.coef_[0]}\nIntercept: {model.intercept_}"
return image, coef_info
# Gradio interface
iface = gr.Interface(
fn=linear_regression,
inputs=[
gr.components.Textbox(placeholder="Enter X values separated by commas (e.g., 1,2,3)", label="X Values"),
gr.components.Textbox(placeholder="Enter Y values separated by commas (e.g., 2,4,6)", label="Y Values")
],
outputs=[
gr.components.Image(type="pil"),
gr.components.Textbox(label="Regression Info")
],
title="Automatic Linear Regression Modeling",
description="Enter X and Y values as comma-separated lists to perform linear regression."
)
# Launch the app
if __name__ == "__main__":
iface.launch()
|