Built-in 데이터셋 활용

key-value형식의 dictionary 타입과 유사한 구조를 가진 datasets

데이터 종류

load_boston 보스톤 집값 데이터
load_iris 아이리스 붓꽃 데이터
load_diabetes 당뇨병 환자 데이터
load_digits 손글씨 데이터
load_linnerud multi-output regression 용 데이터
load_wine 와인 데이터
load_breast_cancer 위스콘신 유방암 환자 데이터\

공통 key

data 샘플 데이터 (Numpy 배열)
target Label 데이터(Y) (Numpy 배열)
feature_names Feature 데이터의 이름
target_names Label 데이터의 이름
DESCR 데이터 셋의 설명
filename 데이터 셋의 파일 저장 위치 (csv)

from sklearn.datasets import load_iris  # 아이리스 예시데이터 불러오기
iris=load_iris()
type(iris)
sklearn.utils._bunch.Bunch
features = iris['data']   # X값
feature_names = iris['feature_names']  # feature data의 이름
feature_names
['sepal length (cm)',
 'sepal width (cm)',
 'petal length (cm)',
 'petal width (cm)']
labels = iris['target']   # Y값
labels
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])

DataFrame으로 변환

import pandas as pd
df = pd.DataFrame(features, columns=feature_names)
df['target'] = iris['target']  # Y데이터 추가
df.head(5)  # 맨위 5개
df['sepal length (cm)'].head(5)  # 열 1개 
df[["sepal length (cm)", "petal width (cm)"]].head(5)  # 열 2개
sepal length (cm) petal width (cm)
0 5.1 0.2
1 4.9 0.2
2 4.7 0.2
3 4.6 0.2
4 5.0 0.2
df.tail(5)  # 맨아래 5개
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
145 6.7 3.0 5.2 2.3 2
146 6.3 2.5 5.0 1.9 2
147 6.5 3.0 5.2 2.0 2
148 6.2 3.4 5.4 2.3 2
149 5.9 3.0 5.1 1.8 2
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 5 columns):
 #   Column             Non-Null Count  Dtype  
---  ------             --------------  -----  
 0   sepal length (cm)  150 non-null    float64
 1   sepal width (cm)   150 non-null    float64
 2   petal length (cm)  150 non-null    float64
 3   petal width (cm)   150 non-null    float64
 4   target             150 non-null    int32  
dtypes: float64(4), int32(1)
memory usage: 5.4 KB

.describe()

수치 데이터 요약

df.describe()
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
count 150.000000 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.057333 3.758000 1.199333 1.000000
std 0.828066 0.435866 1.765298 0.762238 0.819232
min 4.300000 2.000000 1.000000 0.100000 0.000000
25% 5.100000 2.800000 1.600000 0.300000 0.000000
50% 5.800000 3.000000 4.350000 1.300000 1.000000
75% 6.400000 3.300000 5.100000 1.800000 2.000000
max 7.900000 4.400000 6.900000 2.500000 2.000000

.iloc[ : ]

디폴트 인덱스로 슬라이싱

df.iloc[5:10] # 5행~ 9행
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
5 5.4 3.9 1.7 0.4 0
6 4.6 3.4 1.4 0.3 0
7 5.0 3.4 1.5 0.2 0
8 4.4 2.9 1.4 0.2 0
9 4.9 3.1 1.5 0.1 0

.max() .min()

최대값 최소값

df["sepal length (cm)"].max()
7.9

df[df[c] > 6.0]

c열 값이 6.0보다 큰 observation만 표시

df[df["sepal length (cm)"] > 6.0]
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
50 7.0 3.2 4.7 1.4 1
51 6.4 3.2 4.5 1.5 1
52 6.9 3.1 4.9 1.5 1
54 6.5 2.8 4.6 1.5 1
56 6.3 3.3 4.7 1.6 1
... ... ... ... ... ...
144 6.7 3.3 5.7 2.5 2
145 6.7 3.0 5.2 2.3 2
146 6.3 2.5 5.0 1.9 2
147 6.5 3.0 5.2 2.0 2
148 6.2 3.4 5.4 2.3 2

61 rows × 5 columns

.unique()

고유값

df['target'].unique()
array([0, 1, 2])

.value_counts()

고유값별로 개수 세기

df['target'].value_counts()
0    50
1    50
2    50
Name: target, dtype: int64

.sort_values()

값 정렬

df.sort_values(by="sepal length (cm)", ascending=False).head(10)
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
131 7.9 3.8 6.4 2.0 2
135 7.7 3.0 6.1 2.3 2
122 7.7 2.8 6.7 2.0 2
117 7.7 3.8 6.7 2.2 2
118 7.7 2.6 6.9 2.3 2
105 7.6 3.0 6.6 2.1 2
130 7.4 2.8 6.1 1.9 2
107 7.3 2.9 6.3 1.8 2
125 7.2 3.2 6.0 1.8 2
109 7.2 3.6 6.1 2.5 2
# sepal length 내림차순 정렬 후 5행~9번째 행 
df.sort_values(by="sepal length (cm)", ascending=False).iloc[5:10]
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
105 7.6 3.0 6.6 2.1 2
130 7.4 2.8 6.1 1.9 2
107 7.3 2.9 6.3 1.8 2
125 7.2 3.2 6.0 1.8 2
109 7.2 3.6 6.1 2.5 2
# sepal length 내림차순 정렬 후 인덱스 5번~10번
df.sort_values(by="sepal length (cm)", ascending=False).loc[5:10]
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
5 5.4 3.9 1.7 0.4 0
10 5.4 3.7 1.5 0.2 0

열 추가/삭제, 이름 변경

df['x'] = 3.14
df.head(1)
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target x
0 5.1 3.5 1.4 0.2 0 3.14
del df['x']
df.head(1)
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
0 5.1 3.5 1.4 0.2 0
df.rename(columns={'target': 'type'}, inplace=True)
df.head(1)
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) type
0 5.1 3.5 1.4 0.2 0