In [1]:
import pandas as pd

from sklearn.datasets import load_iris

colnames = ['sepallength', 'sepalwidth', 'petallength', 'petalwidth']

iris = load_iris()

x = iris.data    # take independent variables in x
y = iris.target  # take target variable in y

x = pd.DataFrame(x, columns=colnames) # make a dataframe of independent variables
y = pd.Series(y, name='class')        # make a pandas series of target variable

iris_data = pd.concat([x, y], axis=1) # concatenate both x & y to get complete dataset

Let's see data's first 5 rows

In [2]:
iris_data.head()
Out[2]:
sepallength sepalwidth petallength petalwidth class
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
2 4.7 3.2 1.3 0.2 0
3 4.6 3.1 1.5 0.2 0
4 5.0 3.6 1.4 0.2 0

Let's see the shape of data

In [3]:
iris_data.shape
Out[3]:
(150, 5)
In [4]:
from sklearn.model_selection import train_test_split

train_test_split shuffles the rows befor splitting data.

Splitting data into train & test dataset in the proportion of 70 : 30 respectively.

In [5]:
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3,random_state=9)

Printing shape of training data below

In [6]:
print('shapes of training data:')
print('shape of x_train : ', x_train.shape)
print('shape of y_train : ', len(y_train))
shapes of training data:
shape of x_train :  (105, 4)
shape of y_train :  105

Printing shape of test data below

In [7]:
print('shapes of test data:')
print('shape of x_test : ', x_test.shape)
print('shape of y_test : ', len(y_test))
shapes of test data:
shape of x_test :  (45, 4)
shape of y_test :  45