weixin_57739617 2021-06-24 17:00 采纳率: 33.3%
浏览 34
已结题

图一直出不来,说是要把数组改一些

# -*- coding: utf-8 -*-
"""
Created on Mon Mar 22 11:43:37 2021

@author: foolinsky
"""
# %%
import numpy as np
import seaborn as sns
import pandas as pd

import matplotlib

matplotlib.use('tkagg')
import matplotlib.pyplot as plt

url = 'traindata.csv'
names = ['attr1', 'attr2', 'labelname']
iris = pd.read_csv(url, names=names)

# %%
from sklearn.preprocessing import LabelEncoder

X = iris[['attr1', 'attr2']]
y = iris['labelname']

# %%
encoder = LabelEncoder()
Y = encoder.fit_transform(y)
print(Y)

# %%
from sklearn.model_selection import train_test_split

trainX, testX, trainY, testY = \
    train_test_split(X, Y, test_size=0.2)
print(trainX.shape, trainY.shape,
      testX.shape, testY.shape)

# %%
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
#from sklearn import metrics

model = LinearDiscriminantAnalysis(n_components=None)  # 作业第一题的分类模型
model.fit(trainX, trainY)

# %%
testX = pd.read_csv('testdata.csv', names=['attr1', 'attr2'])  # 作业第二题的测试
prediction = model.predict(testX)  # 作业第二题的预测
prediction = encoder.inverse_transform(prediction)
print('The result of classification is{0}:'.format(prediction))  # 显示答案


X_2d = model.transform(X)
#对于二维数据,我们做个可视化
# 区域划分
model.fit(X_2d, Y)
print (X_2d.shape)
h = 0.02
x_min, x_max = X_2d[0:, 0].min() - 1, X_2d[0:, 0].max() + 1
y_min, y_max = X_2d[0:, 1].min() - 1, X_2d[0:, 1].max() + 1


xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                      np.arange(y_min, y_max, h))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)
#做出原来的散点图
class1_x = X_2d[Y == 0, 0]
class1_y = X_2d[Y == 0, 1]
l1 = plt.scatter(class1_x, class1_y,
                  color='b', label="Af")
class1_x = X_2d[Y == 1, 0]
class1_y = X_2d[Y == 1, 1]

l2 = plt.scatter(class1_x, class1_y,
                  color='y', label="Apf")
plt.legend(handles=[l1, l2], loc='best')
plt.grid(True)
plt.show()

 

  • 写回答

1条回答 默认 最新

  • CSDN专家-HGJ 2021-06-24 19:57
    关注

    通过增加taindata的labelname的属性值,就不会出现数组索引越界错误,比如将labelname中一个改为’A‘,可显示出图像。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月29日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)