我如何使用Matplot库在python中绘制数据集

时间:2019-07-03 02:42:03

标签: python matplotlib

我有一个如下程序,我想将数据绘制成图表,请告诉我该怎么做?

我已经搜索并尝试过,但是没有用。

import csv
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.neighbors       import KNeighborsClassifier
from sklearn.metrics         import confusion_matrix, f1_score, accuracy_score

def read_iris(fname):
    with open(fname, 'r') as f:
        data = list(csv.reader(f))[:-1]

    xs = [[float(s) for s in d[:-1]] for d in data]
    ys = [ d[-1]                     for d in data]

    # for d in data[:5]: print(d)
    # for x in   xs[:5]: print(x)
    # for y in   ys[:5]: print(y)

    return xs, ys


def read_bank(fname):
    with open('./data/output.csv', 'r') as f:
        data = list(csv.reader(f))

    labels = data[0]

    data = data[1:]

    xs = [d[1:-2] for d in data]
    ys = [d[-2:]  for d in data]

    xs = [[float(i) for i in x] for x in xs]
    ys = ['yes' if y[1] == '1' else 'no' for y in ys]


    return xs, ys

def main():

    xs, ys = read_bank('./data/output.csv')
    xs_train, xs_test, ys_train, ys_test = train_test_split(xs, ys, test_size=0.3)


    # train & predict labele
    knn = KNeighborsClassifier(n_neighbors=10)
    knn.fit(xs_train, ys_train)
    ps = knn.predict(xs_test)

    ac = accuracy_score(ps, ys_test)


    print(ac)


if __name__ == '__main__': main()

我使用了plt.plot函数,但是我不知道里面到底有什么参数(plt.plot(--------))。

感谢您对我的理解不好,因为我是新手。

0 个答案:

没有答案