| | import gradio as gr |
| | import pandas as pd |
| | import numpy as np |
| | from sklearn.linear_model import LinearRegression |
| | from sklearn.preprocessing import PolynomialFeatures |
| | from sklearn.pipeline import make_pipeline |
| | import plotly.express as px |
| |
|
| | |
| | np.random.seed(42) |
| | n_samples = 100 |
| | income = np.random.randint(30000, 200000, n_samples) |
| | investment = 0.000001 * (income ** 2) - 0.05 * income + 10000 + np.random.normal(0, 5000, n_samples) |
| |
|
| | df = pd.DataFrame({'Monthly_Income': income, 'MF_Investment': investment}) |
| |
|
| | |
| | X = df[['Monthly_Income']] |
| | y = df['MF_Investment'] |
| | model = make_pipeline(PolynomialFeatures(degree=2), LinearRegression()) |
| | model.fit(X, y) |
| |
|
| | |
| | df['Predicted_Investment'] = model.predict(X).round(0).astype(int) |
| |
|
| | |
| | df = df.sort_values(by='Monthly_Income') |
| |
|
| | |
| | def investment_predictor(min_income, max_income): |
| | filtered_df = df[(df['Monthly_Income'] >= min_income) & |
| | (df['Monthly_Income'] <= max_income)] |
| |
|
| | table = filtered_df[['Monthly_Income', 'Predicted_Investment']].head(20) |
| |
|
| | fig = px.scatter(filtered_df, x='Monthly_Income', y='MF_Investment', |
| | title="π Actual vs Predicted Investment", |
| | labels={'MF_Investment': 'Actual Investment'}, |
| | color_discrete_sequence=["blue"]) |
| | fig.add_traces(px.line(filtered_df, x='Monthly_Income', y='Predicted_Investment').data) |
| | fig.update_traces(name='Predicted Investment', selector=dict(mode='lines')) |
| |
|
| | return table, fig |
| |
|
| | |
| | min_income_slider = gr.Slider(minimum=int(df['Monthly_Income'].min()), |
| | maximum=int(df['Monthly_Income'].max()), |
| | step=1000, |
| | value=int(df['Monthly_Income'].min()), |
| | label="Minimum Monthly Income (βΉ)") |
| |
|
| | max_income_slider = gr.Slider(minimum=int(df['Monthly_Income'].min()), |
| | maximum=int(df['Monthly_Income'].max()), |
| | step=1000, |
| | value=int(df['Monthly_Income'].max()), |
| | label="Maximum Monthly Income (βΉ)") |
| |
|
| | |
| | demo = gr.Interface( |
| | fn=investment_predictor, |
| | inputs=[min_income_slider, max_income_slider], |
| | outputs=[ |
| | gr.Dataframe(label="π Prediction Table (Top 20)"), |
| | gr.Plot(label="π Actual vs Predicted Investment") |
| | ], |
| | title="π Mutual Fund Investment Predictor ", |
| | description="This dashboard uses Polynomial Regression to predict mutual fund investments based on monthly income." |
| | ) |
| |
|
| | demo.launch() |
| |
|