Irisのデータプロット

# -*- coding: utf-8 -*- impo

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
#データの取得
df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",header=None)
df.tail()
#1-100行目の目的変数の抽出
y = df.iloc[0:100,4].values
#Iris-setosaを-1,Iris-virginicaを1に変換
y = np.where(y=="Iris-setosa",-1,1)
#1-100行目の1,3列目の抽出
X = df.iloc[0:100,[0,2]].values
#品種setosaのプロット(赤の○)
plt.scatter(X[:50,0],X[:50,1],color='red',marker='o',label='setosa')
#品種versicolorのプロット(青のx)
plt.scatter(X[50:100,0],X[50:100,1],color='blue',marker='x',label='varsicolor')
#軸のラベルの設定
plt.xlabel('sepal length [cm]')
plt.ylabel('pepal length [cm]')
#判例の設定
plt.legend(loc='upper left')
#図の表示
plt.show()

ラベルの変換方法
名前(文字列)を数値に変換

y = np.where(y == 'Iris-setosa',-1,1)

1-100行目の1,3列目の抽出をする.

X = df.iloc[0:100,[0,2]].values

f:id:umashika5555:20170411073916p:plain

1-50行目の1つ目の要素をX軸に2つ目の要素をY軸とし青xをプロット

plt.scatter(X[:50,0],X[:50,1],color='blue',marker='x',label='varsicolor')

f:id:umashika5555:20170411075055p:plain