本文共 3308 字,大约阅读时间需要 11 分钟。
数据可以通过我的github工程下载
运行示例
# 加载数据原料import pandas as pdfile = "D:/EclipseProject/PythonStudyBySu/su/iris.data.csv"# 无文件头df = pd.read_csv(file, header=None)# 读取前面 10 行数据print(df.head(10))
显示结果
0 1 2 3 40 5.1 3.5 1.4 0.2 Iris-setosa1 4.9 3.0 1.4 0.2 Iris-setosa2 4.7 3.2 1.3 0.2 Iris-setosa3 4.6 3.1 1.5 0.2 Iris-setosa4 5.0 3.6 1.4 0.2 Iris-setosa5 5.4 3.9 1.7 0.4 Iris-setosa6 4.6 3.4 1.4 0.3 Iris-setosa7 5.0 3.4 1.5 0.2 Iris-setosa8 4.4 2.9 1.4 0.2 Iris-setosa9 4.9 3.1 1.5 0.1 Iris-setosa
运行示例
# 数据可视化展示import matplotlib.pyplot as pltimport numpy as npy = df.loc[0:100, 4].valuesprint("显示第四列前100条数据", y)
显示结果
显示第四列前100条数据 ['Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-virginica']
示例
y = np.where(y == "Iris-setosa", -1, 1)print("对数据进行分类", y)
运行结果
对数据进行分类 [-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
示例
x = df.iloc[0:100, [0, 2]].valuesprint("抽取出第0和2列的数据", x)
类似一个二维数组
抽取出第0和2列的数据 [[ 5.1 1.4] [ 4.9 1.4] [ 4.7 1.3] [ 4.6 1.5] [ 5. 1.4] ........... [ 6.1 4.6] [ 5.8 4. ] [ 5. 3.3] [ 5.6 4.2] [ 5.7 4.2] [ 5.7 4.2] [ 6.2 4.3] [ 5.1 3. ] [ 5.7 4.1]]
这里的可视化的目的是区分分界线
# 画出图形# x 的第一列为x轴,第二列为y轴plt.scatter(x[:50, 0], x[:50, 1], color='red', marker='o', label='setosa')plt.scatter(x[50:100, 0], x[50:100, 1], color='blue', marker='x', label='versicolor')plt.xlabel("花瓣长度")plt.ylabel("花茎长度")plt.legend(loc='upper left')plt.show()
慕课网视频