1. Import module¶
In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns# seaborn 그래프 라이브러리
import sklearn.datasets
from sklearn.svm import SVC # SVC 함수의 SVM 라이브 러리
from sklearn.model_selection import train_test_split # 트레이닝과 테스트의 데이터또는 컬럼을 나눠주는 라이브러리
from sklearn.metrics import accuracy_score # 머신러닝에서 결과점수를 연산해주는 라이브러리
import warnings# 에러를 제거하는 라이브러리
warnings.filterwarnings('ignore')
2. Load Data¶
In [2]:
iris = sklearn.datasets.load_iris() # sklearn.datasets에 저장된 데이터 불러옴
In [3]:
# 데이터를 출력해보면 아래와 같이 2중배열로 저장되있다.
# data, target, frame, target_names, DESCR, feature_names, filename 의 딕셔너리형태의
# 키 값으로 저장되있는 것을 알 수 있다.
iris
Out[3]:
{'data': array([[5.1, 3.5, 1.4, 0.2],
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5. , 3.6, 1.4, 0.2],
[5.4, 3.9, 1.7, 0.4],
[4.6, 3.4, 1.4, 0.3],
[5. , 3.4, 1.5, 0.2],
[4.4, 2.9, 1.4, 0.2],
[4.9, 3.1, 1.5, 0.1],
[5.4, 3.7, 1.5, 0.2],
[4.8, 3.4, 1.6, 0.2],
[4.8, 3. , 1.4, 0.1],
[4.3, 3. , 1.1, 0.1],
[5.8, 4. , 1.2, 0.2],
[5.7, 4.4, 1.5, 0.4],
[5.4, 3.9, 1.3, 0.4],
[5.1, 3.5, 1.4, 0.3],
[5.7, 3.8, 1.7, 0.3],
[5.1, 3.8, 1.5, 0.3],
[5.4, 3.4, 1.7, 0.2],
[5.1, 3.7, 1.5, 0.4],
[4.6, 3.6, 1. , 0.2],
[5.1, 3.3, 1.7, 0.5],
[4.8, 3.4, 1.9, 0.2],
[5. , 3. , 1.6, 0.2],
[5. , 3.4, 1.6, 0.4],
[5.2, 3.5, 1.5, 0.2],
[5.2, 3.4, 1.4, 0.2],
[4.7, 3.2, 1.6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.2, 3. , 5.8, 1.6],
[7.4, 2.8, 6.1, 1.9],
[7.9, 3.8, 6.4, 2. ],
[6.4, 2.8, 5.6, 2.2],
[6.3, 2.8, 5.1, 1.5],
[6.1, 2.6, 5.6, 1.4],
[7.7, 3. , 6.1, 2.3],
[6.3, 3.4, 5.6, 2.4],
[6.4, 3.1, 5.5, 1.8],
[6. , 3. , 4.8, 1.8],
[6.9, 3.1, 5.4, 2.1],
[6.7, 3.1, 5.6, 2.4],
[6.9, 3.1, 5.1, 2.3],
[5.8, 2.7, 5.1, 1.9],
[6.8, 3.2, 5.9, 2.3],
[6.7, 3.3, 5.7, 2.5],
[6.7, 3. , 5.2, 2.3],
[6.3, 2.5, 5. , 1.9],
[6.5, 3. , 5.2, 2. ],
[6.2, 3.4, 5.4, 2.3],
[5.9, 3. , 5.1, 1.8]]),
'target': 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]),
'frame': None,
'target_names': array(['setosa', 'versicolor', 'virginica'], dtype='<U10'),
'DESCR': '.. _iris_dataset:\n\nIris plants dataset\n--------------------\n\n**Data Set Characteristics:**\n\n :Number of Instances: 150 (50 in each of three classes)\n :Number of Attributes: 4 numeric, predictive attributes and the class\n :Attribute Information:\n - sepal length in cm\n - sepal width in cm\n - petal length in cm\n - petal width in cm\n - class:\n - Iris-Setosa\n - Iris-Versicolour\n - Iris-Virginica\n \n :Summary Statistics:\n\n ============== ==== ==== ======= ===== ====================\n Min Max Mean SD Class Correlation\n ============== ==== ==== ======= ===== ====================\n sepal length: 4.3 7.9 5.84 0.83 0.7826\n sepal width: 2.0 4.4 3.05 0.43 -0.4194\n petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)\n petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)\n ============== ==== ==== ======= ===== ====================\n\n :Missing Attribute Values: None\n :Class Distribution: 33.3% for each of 3 classes.\n :Creator: R.A. Fisher\n :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n :Date: July, 1988\n\nThe famous Iris database, first used by Sir R.A. Fisher. The dataset is taken\nfrom Fisher\'s paper. Note that it\'s the same as in R, but not as in the UCI\nMachine Learning Repository, which has two wrong data points.\n\nThis is perhaps the best known database to be found in the\npattern recognition literature. Fisher\'s paper is a classic in the field and\nis referenced frequently to this day. (See Duda & Hart, for example.) The\ndata set contains 3 classes of 50 instances each, where each class refers to a\ntype of iris plant. One class is linearly separable from the other 2; the\nlatter are NOT linearly separable from each other.\n\n.. topic:: References\n\n - Fisher, R.A. "The use of multiple measurements in taxonomic problems"\n Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to\n Mathematical Statistics" (John Wiley, NY, 1950).\n - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.\n (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.\n - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System\n Structure and Classification Rule for Recognition in Partially Exposed\n Environments". IEEE Transactions on Pattern Analysis and Machine\n Intelligence, Vol. PAMI-2, No. 1, 67-71.\n - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions\n on Information Theory, May 1972, 431-433.\n - See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II\n conceptual clustering system finds 3 classes in the data.\n - Many, many more ...',
'feature_names': ['sepal length (cm)',
'sepal width (cm)',
'petal length (cm)',
'petal width (cm)'],
'filename': 'C:\\anaconda3\\lib\\site-packages\\sklearn\\datasets\\data\\iris.csv'}
In [4]:
iris.data
Out[4]:
array([[5.1, 3.5, 1.4, 0.2],
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5. , 3.6, 1.4, 0.2],
[5.4, 3.9, 1.7, 0.4],
[4.6, 3.4, 1.4, 0.3],
[5. , 3.4, 1.5, 0.2],
[4.4, 2.9, 1.4, 0.2],
[4.9, 3.1, 1.5, 0.1],
[5.4, 3.7, 1.5, 0.2],
[4.8, 3.4, 1.6, 0.2],
[4.8, 3. , 1.4, 0.1],
[4.3, 3. , 1.1, 0.1],
[5.8, 4. , 1.2, 0.2],
[5.7, 4.4, 1.5, 0.4],
[5.4, 3.9, 1.3, 0.4],
[5.1, 3.5, 1.4, 0.3],
[5.7, 3.8, 1.7, 0.3],
[5.1, 3.8, 1.5, 0.3],
[5.4, 3.4, 1.7, 0.2],
[5.1, 3.7, 1.5, 0.4],
[4.6, 3.6, 1. , 0.2],
[5.1, 3.3, 1.7, 0.5],
[4.8, 3.4, 1.9, 0.2],
[5. , 3. , 1.6, 0.2],
[5. , 3.4, 1.6, 0.4],
[5.2, 3.5, 1.5, 0.2],
[5.2, 3.4, 1.4, 0.2],
[4.7, 3.2, 1.6, 0.2],
[4.8, 3.1, 1.6, 0.2],
[5.4, 3.4, 1.5, 0.4],
[5.2, 4.1, 1.5, 0.1],
[5.5, 4.2, 1.4, 0.2],
[4.9, 3.1, 1.5, 0.2],
[5. , 3.2, 1.2, 0.2],
[5.5, 3.5, 1.3, 0.2],
[4.9, 3.6, 1.4, 0.1],
[4.4, 3. , 1.3, 0.2],
[5.1, 3.4, 1.5, 0.2],
[5. , 3.5, 1.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.7, 3.1, 5.6, 2.4],
[6.9, 3.1, 5.1, 2.3],
[5.8, 2.7, 5.1, 1.9],
[6.8, 3.2, 5.9, 2.3],
[6.7, 3.3, 5.7, 2.5],
[6.7, 3. , 5.2, 2.3],
[6.3, 2.5, 5. , 1.9],
[6.5, 3. , 5.2, 2. ],
[6.2, 3.4, 5.4, 2.3],
[5.9, 3. , 5.1, 1.8]])
In [5]:
iris.feature_names
Out[5]:
['sepal length (cm)',
'sepal width (cm)',
'petal length (cm)',
'petal width (cm)']
In [6]:
# df의 변수에 DataFrame형태로 저장한다.
# 이때 데이터는 iris.data로 넣고 컬럼은 iris.feature_names 저장한다.
# 이유는 data와 feature_names의 컬럼갯수가 동일하기 때문.
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
# 데이터를 출력해보면 아래 결과와 같이 각 컬럼에 해당하는 데이터가
# 들어간것을 볼 수 있다.
df.head(1)
Out[6]:
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | |
---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 |
In [7]:
# iris에 target의 키값이 있는데 target에 어떤 값이 있는지 확인 한다.
# 확인 결과 0 ~ 2의 값이 들어가 있다.
iris.target
Out[7]:
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])
In [8]:
# target이라는 컬럼을 만들어 주고 그 안에 해당 값들을 넣어준다.
df['target'] = iris.target
df.head()
Out[8]:
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | target | |
---|---|---|---|---|---|
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 |
In [9]:
# map을 사용해서 target컬럼의 데이터를 보기좋게 바꿔준다.
# 이때 map이라는 함수를 사용하는데 딕셔너리 형태로
# 컬럼의 벨류값을 지정해서 아래와같이 문자열로 넣어주면 해당 벨류값에 맞게
# 벨류값이 변경되는 것을 알 수 있다.
df['target'] = df['target'].map({0:'setosa',1:'versicolor',2:'virginica'})
df
Out[9]:
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | target | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
... | ... | ... | ... | ... | ... |
145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
150 rows × 5 columns
In [10]:
# 컬럼값을 인덱스기준 0에서 n-1까지 출력해준다.
df.iloc[:,:4].head()
Out[10]:
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | |
---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 |
1 | 4.9 | 3.0 | 1.4 | 0.2 |
2 | 4.7 | 3.2 | 1.3 | 0.2 |
3 | 4.6 | 3.1 | 1.5 | 0.2 |
4 | 5.0 | 3.6 | 1.4 | 0.2 |
In [11]:
# 컬럼명기준 sepal length (cm)에서 petal width (cm)까지 출력해준다.
df.loc[:,'sepal length (cm)':'petal width (cm)'].head()
Out[11]:
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | |
---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 |
1 | 4.9 | 3.0 | 1.4 | 0.2 |
2 | 4.7 | 3.2 | 1.3 | 0.2 |
3 | 4.6 | 3.1 | 1.5 | 0.2 |
4 | 5.0 | 3.6 | 1.4 | 0.2 |
In [12]:
# 컬럼값을 인덱스기준으로 4번째의컬럼의 전체를 가져옴
df.iloc[:,4].head()
Out[12]:
0 setosa
1 setosa
2 setosa
3 setosa
4 setosa
Name: target, dtype: object
In [13]:
# 컬럼명에 해당하는 컬럼의 전체 데이터를 가져옴
df.loc[:,'target'].head()
Out[13]:
0 setosa
1 setosa
2 setosa
3 setosa
4 setosa
Name: target, dtype: object
3. Data Colection¶
In [14]:
# train_test_split() 함수를 사용해서 train데이터와 test데이터를 각각 나눠준다.
# test_size=0.1 으로 설정 하므로써,
# X_train에는 각 속성들의 데이터를 0.9만큼 저장
# X_test 에는 각 속성들의 데이터를 0.1만큼 저장.
# y_train 에는 라벨의 데이터를 0.9만큼 저장
# y_test 에는 라벨의 데이터를 0.1만큼 저장
# shuffle은 데이터들의 섞임을 지정해줄수 있다.
X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, :4], df.iloc[:,4],test_size=0.1,shuffle=False )
각각의 데이터가 변수로 저장이되고,
train에서는 90%, test에서는 10%의 데이터만 저장된것을 확인 할 수있다.
In [15]:
X_train.tail(1)
Out[15]:
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | |
---|---|---|---|---|
134 | 6.1 | 2.6 | 5.6 | 1.4 |
In [16]:
X_test.tail(1)
Out[16]:
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | |
---|---|---|---|---|
149 | 5.9 | 3.0 | 5.1 | 1.8 |
In [17]:
y_train.tail(1)
Out[17]:
134 virginica
Name: target, dtype: object
In [18]:
y_test.tail(1)
Out[18]:
149 virginica
Name: target, dtype: object
4. Model Generate and Training¶
In [19]:
# 모델의 SVC()함수를 model변수에 저장한다.
model = SVC()
# 저장된 변수에 fit()함수를 사용해서 (데이터=라벨)을 학습 시켜준다.
model.fit(X_train, y_train)
Out[19]:
SVC()
5. Prediction¶
In [20]:
# predict의 변수에 학습된 모델에 임의의 값을 넣어서 연산된 결과를 저장 시킨다.
predict = model.predict(X_test)
print(predict)
['virginica' 'virginica' 'virginica' 'versicolor' 'virginica' 'virginica'
'virginica' 'virginica' 'virginica' 'virginica' 'virginica' 'virginica'
'virginica' 'virginica' 'virginica']
In [21]:
# 데이터가 얼마나 정확 했는지 확인해 봅니다.
# y_test 데이터 인덱스번호가 135부터 있는걸 감안해 더해줍니다.
sum = 0
for i in range(len(predict)):
if predict[i] == y_test[i+135]:
sum+=1
print(f'정확도 : {round(sum/len(predict)*100,2)}%')
정확도 : 93.33%
In [22]:
# accuracy_score 함수를 사용해서
#accuracy_score(테스트 데이터의 라벨과, 연산된 테스트 결과)를 매칭시켜봅니다
print(f'정확도 : {accuracy_score(y_test,predict).round(4)*100}%')
정확도 : 93.33%
6. Visualization¶
In [23]:
# iris.target_names 의 키값에는 어떤 값들이 있는지 확인 해봅니다.
print(iris.target_names)
['setosa' 'versicolor' 'virginica']
추가로 markers의 값들이 어떤게 있는지 확인해 봅니다.¶
In [24]:
# set the size
plt.rcParams['figure.figsize'] = [10, 8]
# remove ticks and values of axis
plt.xticks([])
plt.yticks([])
# markers' shape
all_shape=['.','o','v','^','>','<','s','p','*','h','H','D', 'd', '', '', '']
num = 0
for x in range(1, 5):
for y in range(1, 5):
num += 1
plt.plot(x, y,
marker = all_shape[num-1],
markerfacecolor='green',
markersize=20,
markeredgecolor='black')
plt.text(x+0.1, y,
all_shape[num-1],
horizontalalignment='left',
size='medium',
color='black',
weight='semibold')
plt.title('Markers', fontsize=20)
plt.show()
In [25]:
# 사용할 마커들을 markers라는 이름으로 저장합니다.
markers=['H','D','*']
# 반복문을 사용해 ['setosa' 'versicolor' 'virginica'] 에서 벨류값들을 target변수에 저장합니다.
for i,target in enumerate(iris.target_names):
# target_cluster변수에 데이터프레임의 모든 target컬럼의 데이터를 가져와 반복문의 target변수와 비교하여 저장시켜줍니다.
target_cluster = df[df['target'] == target]
# plt.scatter를 사용해 target_cluster의 결과값들을
# x축엔 'petal length (cm)'
# y축엔 'petal width (cm)' 을 넣어 데이터들을 비교해 봅니다.
plt.scatter(x=target_cluster['petal length (cm)'], y=target_cluster['petal width (cm)'], marker=markers[i])
In [26]:
# 마지막으로 target_cluster에 어떤 값들이 저장되어 있었는지 확인해 봅니다.
target_cluster
Out[26]:
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | target | |
---|---|---|---|---|---|
100 | 6.3 | 3.3 | 6.0 | 2.5 | virginica |
101 | 5.8 | 2.7 | 5.1 | 1.9 | virginica |
102 | 7.1 | 3.0 | 5.9 | 2.1 | virginica |
103 | 6.3 | 2.9 | 5.6 | 1.8 | virginica |
104 | 6.5 | 3.0 | 5.8 | 2.2 | virginica |
105 | 7.6 | 3.0 | 6.6 | 2.1 | virginica |
106 | 4.9 | 2.5 | 4.5 | 1.7 | virginica |
107 | 7.3 | 2.9 | 6.3 | 1.8 | virginica |
108 | 6.7 | 2.5 | 5.8 | 1.8 | virginica |
109 | 7.2 | 3.6 | 6.1 | 2.5 | virginica |
110 | 6.5 | 3.2 | 5.1 | 2.0 | virginica |
111 | 6.4 | 2.7 | 5.3 | 1.9 | virginica |
112 | 6.8 | 3.0 | 5.5 | 2.1 | virginica |
113 | 5.7 | 2.5 | 5.0 | 2.0 | virginica |
114 | 5.8 | 2.8 | 5.1 | 2.4 | virginica |
115 | 6.4 | 3.2 | 5.3 | 2.3 | virginica |
116 | 6.5 | 3.0 | 5.5 | 1.8 | virginica |
117 | 7.7 | 3.8 | 6.7 | 2.2 | virginica |
118 | 7.7 | 2.6 | 6.9 | 2.3 | virginica |
119 | 6.0 | 2.2 | 5.0 | 1.5 | virginica |
120 | 6.9 | 3.2 | 5.7 | 2.3 | virginica |
121 | 5.6 | 2.8 | 4.9 | 2.0 | virginica |
122 | 7.7 | 2.8 | 6.7 | 2.0 | virginica |
123 | 6.3 | 2.7 | 4.9 | 1.8 | virginica |
124 | 6.7 | 3.3 | 5.7 | 2.1 | virginica |
125 | 7.2 | 3.2 | 6.0 | 1.8 | virginica |
126 | 6.2 | 2.8 | 4.8 | 1.8 | virginica |
127 | 6.1 | 3.0 | 4.9 | 1.8 | virginica |
128 | 6.4 | 2.8 | 5.6 | 2.1 | virginica |
129 | 7.2 | 3.0 | 5.8 | 1.6 | virginica |
130 | 7.4 | 2.8 | 6.1 | 1.9 | virginica |
131 | 7.9 | 3.8 | 6.4 | 2.0 | virginica |
132 | 6.4 | 2.8 | 5.6 | 2.2 | virginica |
133 | 6.3 | 2.8 | 5.1 | 1.5 | virginica |
134 | 6.1 | 2.6 | 5.6 | 1.4 | virginica |
135 | 7.7 | 3.0 | 6.1 | 2.3 | virginica |
136 | 6.3 | 3.4 | 5.6 | 2.4 | virginica |
137 | 6.4 | 3.1 | 5.5 | 1.8 | virginica |
138 | 6.0 | 3.0 | 4.8 | 1.8 | virginica |
139 | 6.9 | 3.1 | 5.4 | 2.1 | virginica |
140 | 6.7 | 3.1 | 5.6 | 2.4 | virginica |
141 | 6.9 | 3.1 | 5.1 | 2.3 | virginica |
142 | 5.8 | 2.7 | 5.1 | 1.9 | virginica |
143 | 6.8 | 3.2 | 5.9 | 2.3 | virginica |
144 | 6.7 | 3.3 | 5.7 | 2.5 | virginica |
145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
'workSpace > PYTHON' 카테고리의 다른 글
[DL] CNN 모델 구조 (0) | 2021.01.19 |
---|---|
[DL] Pytorch 연구 모델 (0) | 2021.01.18 |
[seaborn] seaborn을 이용한 시각화 (0) | 2021.01.18 |
[Matplot] Matplot Lib를 이용한 시각화 (0) | 2021.01.18 |
[Pandas][Numpy][seaborn] 2019년 서울 특별시 아파트 실거래 분석 및 표 만들기 (0) | 2021.01.18 |