|
|
|
from sklearn.linear_model import LinearRegression |
|
from sklearn.neural_network import MLPRegressor |
|
import lightgbm as lgb |
|
from xgboost import XGBRegressor |
|
from sklearn.ensemble import RandomForestRegressor |
|
from sklearn.preprocessing import StandardScaler |
|
from sklearn.model_selection import train_test_split |
|
import pandas as pd |
|
from fastapi.middleware.cors import CORSMiddleware |
|
from fastapi import FastAPI, Response, BackgroundTasks |
|
import json |
|
import matplotlib.pyplot as plt |
|
import io |
|
import matplotlib |
|
matplotlib.use('AGG') |
|
|
|
app = FastAPI() |
|
|
|
|
|
origins = [ |
|
"http://127.0.0.1:5500" |
|
] |
|
|
|
|
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=origins, |
|
allow_credentials=True, |
|
allow_methods=["POST"], |
|
allow_headers=["*"], |
|
) |
|
|
|
def create_img(): |
|
plt.rcParams['figure.figsize'] = [7.50, 3.50] |
|
plt.rcParams['figure.autolayout'] = True |
|
plt.plot([1, 2]) |
|
img_buf = io.BytesIO() |
|
plt.savefig(img_buf, format='png') |
|
plt.close() |
|
return img_buf |
|
|
|
@app.get('/png') |
|
async def get_img(background_tasks: BackgroundTasks): |
|
img_buf = create_img() |
|
|
|
|
|
bufContents: bytes = img_buf.getvalue() |
|
background_tasks.add_task(img_buf.close) |
|
headers = {'Content-Disposition': 'inline; filename="out.png"'} |
|
return Response(bufContents, headers=headers, media_type='image/png') |
|
|
|
|
|
|
|
|
|
@app.post("/mlr") |
|
async def mlr(): |
|
|
|
|
|
df = pd.read_csv("./1.csv").dropna(axis=0) |
|
|
|
x = df.iloc[:, :12] |
|
|
|
y = df.loc[:, "SBR"] |
|
|
|
x_train, x_test, y_train, y_test = train_test_split( |
|
x, y, test_size=0.3, random_state=0) |
|
|
|
|
|
standardscaler = StandardScaler() |
|
standardscaler.fit(x_train) |
|
x_train = standardscaler.transform(x_train) |
|
x_test = standardscaler.transform(x_test) |
|
|
|
|
|
model = LinearRegression() |
|
model.fit(x_train, y_train) |
|
|
|
|
|
y_pred = model.predict(x_test) |
|
return json.dumps(y_pred.tolist()) |
|
|
|
|
|
@app.post("/rf") |
|
async def rf(): |
|
|
|
|
|
df = pd.read_csv("./1.csv").dropna(axis=0) |
|
|
|
x = df.iloc[:, :12] |
|
|
|
y = df.loc[:, "SBR"] |
|
|
|
x_train, x_test, y_train, y_test = train_test_split( |
|
x, y, test_size=0.3, random_state=0) |
|
|
|
|
|
standardscaler = StandardScaler() |
|
standardscaler.fit(x_train) |
|
x_train = standardscaler.transform(x_train) |
|
x_test = standardscaler.transform(x_test) |
|
|
|
|
|
model = RandomForestRegressor() |
|
model.fit(x_train, y_train) |
|
|
|
|
|
y_pred = model.predict(x_test) |
|
return json.dumps(y_pred.tolist()) |
|
|
|
|
|
@app.post("/bpn") |
|
async def rf(): |
|
|
|
|
|
df = pd.read_csv("./1.csv").dropna(axis=0) |
|
|
|
x = df.iloc[:, :12] |
|
|
|
y = df.loc[:, "SBR"] |
|
|
|
x_train, x_test, y_train, y_test = train_test_split( |
|
x, y, test_size=0.3, random_state=0) |
|
|
|
|
|
standardscaler = StandardScaler() |
|
standardscaler.fit(x_train) |
|
x_train = standardscaler.transform(x_train) |
|
x_test = standardscaler.transform(x_test) |
|
|
|
|
|
model = MLPRegressor(hidden_layer_sizes=(10,), random_state=10,learning_rate_init=0.1) |
|
model.fit(x_train,y_train) |
|
|
|
|
|
y_pred = model.predict(x_test) |
|
return json.dumps(y_pred.tolist()) |
|
|
|
|
|
@app.post("/xgboost") |
|
async def rf(): |
|
|
|
|
|
df = pd.read_csv("./1.csv").dropna(axis=0) |
|
|
|
x = df.iloc[:, :12] |
|
|
|
y = df.loc[:, "SBR"] |
|
|
|
x_train, x_test, y_train, y_test = train_test_split( |
|
x, y, test_size=0.3, random_state=0) |
|
|
|
|
|
standardscaler = StandardScaler() |
|
standardscaler.fit(x_train) |
|
x_train = standardscaler.transform(x_train) |
|
x_test = standardscaler.transform(x_test) |
|
|
|
|
|
model = XGBRegressor(max_depth=5, learning_rate=0.1, n_estimators=160, objective='reg:gamma') |
|
model.fit(x_train, y_train) |
|
|
|
|
|
y_pred = model.predict(x_test) |
|
return json.dumps(y_pred.tolist()) |
|
|
|
|
|
@app.post("/lightgbm") |
|
async def rf(): |
|
|
|
|
|
df = pd.read_csv("./1.csv").dropna(axis=0) |
|
|
|
x = df.iloc[:, :12] |
|
|
|
y = df.loc[:, "SBR"] |
|
|
|
x_train, x_test, y_train, y_test = train_test_split( |
|
x, y, test_size=0.3, random_state=0) |
|
|
|
|
|
standardscaler = StandardScaler() |
|
standardscaler.fit(x_train) |
|
x_train = standardscaler.transform(x_train) |
|
x_test = standardscaler.transform(x_test) |
|
|
|
|
|
model = lgb.LGBMRegressor(objective='regression',num_leaves=31,learning_rate=0.05,n_estimators=20) |
|
model.fit(x_train, y_train) |
|
|
|
|
|
y_pred = model.predict(x_test) |
|
return json.dumps(y_pred.tolist()) |
|
|
|
|