data2 = pd.read_csv('/home/w123/Documents/2.txt') y2 = data.iloc[:, :-1].values.reshape(-1, 1) X2 = data.iloc[:, -1].values.reshape(-1, 1) regressor2 = LinearRegression() regressor2.fit(X2, y2) y2_pred = regressor2.predict(X2) print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor2.intercept_[0], regressor2.coef_[0][0])) plt.scatter(X2, y2, color='green') plt.plot(X2, y2_pred, color='orange') plt.legend(['Regression Line 2', 'Observations 2']) #3 data3 = pd.read_csv('/home/w123/Documents/3.txt') y3 = data.iloc[:, :-1].values.reshape(-1, 1) X3 = data.iloc[:, -1].values.reshape(-1, 1) regressor3 = LinearRegression() regressor3.fit(X, y) y3_pred = regressor3.predict(X) print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor3.intercept_[0], regressor.coef_[0][0])) plt.scatter(X3, y3, color='purple') plt.plot(X3, y3_pred, color='yellow') plt.title('Linear Regression') plt.xlabel('Independent Variable') plt.ylabel('Dependent Variable') plt.legend(['Regression Line 1', 'Observations 1', 'Regression Line 2', 'Observations 2', 'Regression Line 3', 'Observations 3']) plt.show()哪里不对

时间: 2023-06-19 18:05:54 浏览: 72
首先,第一行中的文件路径 '/home/w123/Documents/2.txt' 和第三行中的文件路径 '/home/w123/Documents/3.txt' 并不一致,需要保持一致才能正确读取数据。 其次,在第二段代码中,变量名使用了 'y2' 和 'X2',但是在第四行中却使用了 'y' 和 'X',这也需要保持一致性才能正确执行代码。 修改后的代码如下所示: ```python import pandas as pd from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt # read data from files data1 = pd.read_csv('/home/w123/Documents/1.txt') data2 = pd.read_csv('/home/w123/Documents/2.txt') data3 = pd.read_csv('/home/w123/Documents/3.txt') # extract data for first regression y1 = data1.iloc[:, :-1].values.reshape(-1, 1) X1 = data1.iloc[:, -1].values.reshape(-1, 1) # train first regression model regressor1 = LinearRegression() regressor1.fit(X1, y1) # predict values and print regression equation for first regression y1_pred = regressor1.predict(X1) print("Regression Function 1: y = {:.2f} + {:.2f}x".format(regressor1.intercept_[0], regressor1.coef_[0][0])) # extract data for second regression y2 = data2.iloc[:, :-1].values.reshape(-1, 1) X2 = data2.iloc[:, -1].values.reshape(-1, 1) # train second regression model regressor2 = LinearRegression() regressor2.fit(X2, y2) # predict values and print regression equation for second regression y2_pred = regressor2.predict(X2) print("Regression Function 2: y = {:.2f} + {:.2f}x".format(regressor2.intercept_[0], regressor2.coef_[0][0])) # extract data for third regression y3 = data3.iloc[:, :-1].values.reshape(-1, 1) X3 = data3.iloc[:, -1].values.reshape(-1, 1) # train third regression model regressor3 = LinearRegression() regressor3.fit(X3, y3) # predict values and print regression equation for third regression y3_pred = regressor3.predict(X3) print("Regression Function 3: y = {:.2f} + {:.2f}x".format(regressor3.intercept_[0], regressor3.coef_[0][0])) # plot all observations and regression lines plt.scatter(X1, y1, color='blue') plt.plot(X1, y1_pred, color='red') plt.scatter(X2, y2, color='green') plt.plot(X2, y2_pred, color='orange') plt.scatter(X3, y3, color='purple') plt.plot(X3, y3_pred, color='yellow') plt.title('Linear Regression') plt.xlabel('Independent Variable') plt.ylabel('Dependent Variable') plt.legend(['Regression Line 1', 'Observations 1', 'Regression Line 2', 'Observations 2', 'Regression Line 3', 'Observations 3']) plt.show() ```

相关推荐

新数据前面多了一列无用的,每列用逗号隔开,改代码data = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-0-ratio.txt') y = data.iloc[:, :-1].values.reshape(-1, 1) X = data.iloc[:, -1].values.reshape(-1, 1) regressor = LinearRegression() regressor.fit(X, y) y_pred = regressor.predict(X) print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor.intercept_[0], regressor.coef_[0][0])) plt.scatter(X, y, color='blue') plt.plot(X, y_pred, color='red') data2 = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-5-ratio.txt') y2 = data2.iloc[:, :-1].values.reshape(-1, 1) X2 = data2.iloc[:, -1].values.reshape(-1, 1) regressor2 = LinearRegression() regressor2.fit(X2, y2) y2_pred = regressor2.predict(X2) print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor2.intercept_[0], regressor2.coef_[0][0])) plt.scatter(X2, y2, color='green') plt.plot(X2, y2_pred, color='orange') plt.legend(['Regression Line 2', 'Observations 2']) #3 data3 = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-10-ratio.txt') y3 = data3.iloc[:, :-1].values.reshape(-1, 1) X3 = data3.iloc[:, -1].values.reshape(-1, 1) regressor3 = LinearRegression() regressor3.fit(X3, y3) y3_pred = regressor3.predict(X3) print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor3.intercept_[0], regressor.coef_[0][0])) plt.scatter(X3, y3, color='purple') plt.plot(X3, y3_pred, color='yellow') plt.title('Linear Regression') plt.xlabel('Independent Variable') plt.ylabel('Dependent Variable') plt.legend(['Regression Line 1', 'Observations 1', 'Regression Line 2', 'Observations 2', 'Regression Line 3', 'Observations 3']) plt.show()

最新推荐

recommend-type

海明纠错码与CRC循环冗余校验

循环码编码通过模2运算来建立信息位和校验位之间的约束关系。首先将所传数据看成高次多项式, 把此多项式除以预先给定的生成多项式, 其余 数作为校验位附加在所传数据的尾部一并传送, 即在一个长度为n 的码组中有k 个...
recommend-type

在FPGA上设计汉明码的编码器和解码器

本文用Verilog语言实现了汉明码的编码和译码。在介绍汉明码编码和译码原理的基础上,设计出了汉明码的编码器和译码器,写出了基于Verilog实现的源程序,并通过modelsim软件的仿真。
recommend-type

六首页数字藏品NFT交易网React NextJS网站模板 六首页数字藏品nft交易网反应NextJS网站模板

六首页数字藏品NFT交易网React NextJS网站模板 六首页数字藏品nft交易网反应NextJS网站模板
recommend-type

wireshark安装教程入门

wireshark安装教程入门
recommend-type

基于C++负数据库的隐私保护在线医疗诊断系统

【作品名称】:基于C++负数据库的隐私保护在线医疗诊断系统 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】: 基于负数据库的隐私保护在线医疗诊断系统 NDBMedicalSystem 客户端及服务器端 本项目是在保护用户隐私的前提下,完成了对新冠肺炎、乳腺癌、眼疾等多种疾病的智能诊断。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。