所以我一直在摆弄Plotly,似乎不知道如何将线性函数的所有结果合并到Plotly中的一个图形中。对于我的数据集,我有一个例子,其中自变量称为IV,因变量为字母。下面是一个数据集示例。
现在我已经解决了如何使用Sklearn为每个因变量拟合线性模型,但它只显示在单独的图中
#Data processing import
import numpy as np
import pandas as pd
#import csv
#Visualisation import
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression # Regression linear model
import plotly.express as px
import plotly.graph_objects as go
sns.set()
#===================Main function==============================================
#------------------------------------------------------------------------------
plt.close("all")
#Reading the data-------------------------------------------------------------
filename="test_dataset.xls"
#Reading the input file-------------------------------------------------------
Data0=pd.read_excel (filename)
#Read number of data, number of column and column names-----------------------
number_of_data=Data0.shape[0]
number_of_I_columns=Data0.shape[1]-1 #The first column is for the X-axis=IV, to access, eg. Data0.iloc[0,0]
column_names=Data0.columns #to access, eg. Data0.columns[1]
x = Data0.IV.values.reshape(-1, 1)
# print(x)
#fit least square for each letter data---------------------------------------------
for i in range(number_of_I_columns): #for each letter data
y=Data0[column_names[i+1]].values
y=y.astype('float64')
#Least square fitting-----------------------------------------------------
model = LinearRegression(fit_intercept=True)
model.fit(x,y)
#Predict the letter with the fitted model----------------------------
x_range = np.linspace(x.min(), x.max())
y_range = model.predict(x_range.reshape(-1, 1))
fig = go.Figure([
go.Scatter(name = column_names[i+1], x=Data0['IV'], y=Data0[column_names[i+1]], mode='markers'),
go.Scatter(name='Regression Fit', x=x_range, y=y_range, mode='lines')
])
fig.show()
我将所有回归拟合组合到Matplotlib中的一个图中,在这里给出了所需的结果:
您可能会问,如果Matplotlib上已经有了它,为什么还要Plotly?好的,在Plotly中,我知道我可以解开并勾选我想在图表上显示的数据,这在比较某些字母的梯度时很有用。 希望有人能帮我一把。谢谢大家!
-编辑 所以我尝试将线性函数与下面的代码结合起来。然而,结果不是在同一原点上彼此重叠。相反,它在每个结果结束后加入,显示这个图,这不是我想要的
#Reading the data-------------------------------------------------------------
filename="test_dataset.xls"
#Reading the input file-------------------------------------------------------
Data0=pd.read_excel (filename)
#Read number of data, number of column and column names-----------------------
number_of_data=Data0.shape[0]
number_of_I_columns=Data0.shape[1]-1 #The first column is for the X-axis=IV, to access, eg. Data0.iloc[0,0]
column_names=Data0.columns #to access, eg. Data0.columns[1]
x = Data0.IV.values.reshape(-1, 1)
ys = []
# print(x)
#fit least square for each letter data---------------------------------------------
for i in range(number_of_I_columns): #for each letter data
y=Data0[column_names[i+1]].values
y=y.astype('float64')
# ys.append(y)
#Least square fitting-----------------------------------------------------
model = LinearRegression(fit_intercept=True)
model.fit(x,y)
#Predict the letter with the fitted model----------------------------
x_range = np.linspace(x.min(), x.max())
y_range = model.predict(x_range.reshape(-1, 1))
ys.append(y_range)
### MY ATTEMPT OF COMBINING LINEAR FUNCTIONS
ys = np.array(ys)
colnames = list(column_names)
for i in range(ys.shape[0]):
# print(ys[:,i])
fig = go.Figure()
fig.add_trace(go.Scatter(x = x[:,0], y=ys[:,i], name= colnames[i+1]))
fig.show()
问题是您正在用
fig=go.Figure()
重置图形将这条线移到循环之外应该可以解决您的问题
相关问题 更多 >
编程相关推荐