ax1.scatter(data[:, 0], data[:, 1], c=y)
时间: 2023-12-24 09:02:48 浏览: 30
这段代码使用 matplotlib 库中的 scatter 函数在二维坐标系上绘制散点图。其中,data[:, 0] 是数据中所有样本的第一个特征,data[:, 1] 是数据中所有样本的第二个特征,c=y 表示按照样本标签 y 对数据点进行颜色编码。具体来说,每个样本的第一个特征对应 x 轴,第二个特征对应 y 轴,样本标签 y 对应颜色。可以通过调整函数的参数来改变绘图效果,比如点的大小、颜色映射等。
相关问题
fig, ax1 = plt.subplots() color = 'tab:red' ax1.set_xlabel('Index') ax1.set_ylabel('MEDV', color=color) ax1.plot(data['MEDV'], color=color) ax1.tick_params(axis='y', labelcolor=color) ax2 = ax1.twinx() color = 'tab:blue' ax2.set_ylabel('CRIM', color=color) ax2.plot(data['CRIM'], color=color) ax2.tick_params(axis='y', labelcolor=color) fig.tight_layout() plt.title('Line plot of CRIM and MEDV') plt.show() # 绘制CRIM和MEDV的散点图 plt.scatter(data.index, data['CRIM'], s=5) plt.scatter(data.index, data['MEDV'], s=5) plt.legend() plt.ylabel('Value') plt.xlabel('Index') plt.show()
这是一个使用matplotlib库在Python中绘制线图和散点图的代码示例。第一段代码绘制了'MEDV'和'CRIM'之间的线图。通过调用subplots函数创建一个包含两个子图的图表,其中ax1是左侧子图,ax2是右侧子图。在左侧子图中,'MEDV'被设置为纵坐标,'CRIM'被设置为右侧子图的纵坐标。在右侧子图中,'CRIM'被设置为纵坐标。通过设置tick_params函数中的labelcolor参数,使得左右两个子图的刻度标签颜色不同。最后,使用tight_layout函数调整图表布局,并使用show函数显示图表。
第二段代码绘制了'MEDV'和'CRIM'之间的散点图。使用scatter函数绘制散点图,其中x轴被设置为数据的索引,y轴被设置为'MEDV'和'CRIM'。使用legend函数添加图例,使得可以区分'MEDV'和'CRIM'的散点图。最后,使用ylabel和xlabel函数设置横纵坐标的标签,并使用show函数显示图表。
import numpy as np import pandas as pd import matplotlib.pyplot as plt df=pd.read_csv('C:\\Users\ASUS\Desktop\AI\实训\汽车销量数据new.csv',sep=',',header=0) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.figure(figsize=(10,4)) ax1=plt.subplot(121) ax1.scatter(df['price'],df['quantity'],c='b') df=(df-df.min())/(df.max()-df.min()) df.to_csv('quantity.txt',sep='\t',index=False) train_data=df.sample(frac=0.8,replace=False) test_data=df.drop(train_data.index) x_train=train_data['price'].values.reshape(-1, 1) y_train=train_data['quantity'].values x_test=test_data['price'].values.reshape(-1, 1) y_test=test_data['quantity'].values from sklearn.linear_model import LinearRegression import joblib #model=SGDRegressor(max_iter=500,learning_rate='constant',eta0=0.01) model = LinearRegression() #训练模型 model.fit(x_train,y_train) #输出训练结果 pre_score=model.score(x_train,y_train) print('训练集准确性得分=',pre_score) print('coef=',model.coef_,'intercept=',model.intercept_) #保存训练后的模型 joblib.dump(model,'LinearRegression.model') ax2=plt.subplot(122) ax2.scatter(x_train,y_train,label='测试集') ax2.plot(x_train,model.predict(x_train),color='blue') ax2.set_xlabel('工龄') ax2.set_ylabel('工资') plt.legend(loc='upper left') model=joblib.load('LinearRegression.model') y_pred=model.predict(x_test)#得到预测值 print('测试集准确性得分=%.5f'%model.score(x_test,y_test)) #计算测试集的损失(用均方差) MSE=np.mean((y_test - y_pred)**2) print('损失MSE={:.5f}'.format(MSE)) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.figure(figsize=(10,4)) ax1=plt.subplot(121) plt.scatter(x_test,y_test,label='测试集') plt.plot(x_test,y_pred,'r',label='预测回归线') ax1.set_xlabel('工龄') ax1.set_ylabel('工资') plt.legend(loc='upper left') ax2=plt.subplot(122) x=range(0,len(y_test)) plt.plot(x,y_test,'g',label='真实值') plt.plot(x,y_pred,'r',label='预测值') ax2.set_xlabel('样本序号') ax2.set_ylabel('工资') plt.legend(loc='upper right') plt.show()怎么预测价格为15万时的销量
要预测价格为15万时的销量,可以使用训练好的线性回归模型进行预测。首先需要将15万的价格转换为模型可接受的输入格式,即将其转换为一个形状为(1,1)的二维数组:
```python
price = np.array([[15]])
```
然后使用训练好的模型进行预测:
```python
quantity = model.predict(price)
print('价格为15万时的销量预测值为:', quantity)
```
该代码将输出一个预测值,即价格为15万时的销量预测值。