import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
a = [0,1,2,3,4,5,6,7,8,9]
a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a = [i for i in range(10)]
a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 각각의 요소에 순서를 나타내는 인덱스를 함께 확인하기
char = ['a', 'b', 'c', 'd', 'e']
for n, i in enumerate(char):
print(n,i)
0 a 1 b 2 c 3 d 4 e
dic_n = {i:n for n, i in enumerate(char)}
dic_n
{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}
### 여러개의 변수 넘겨받기. 단, 숫자는 큰 의미 없음.
for n, i in enumerate(char):
_, n2, i = n, n*n, i
print(n2, i)
0 a 1 b 4 c 9 d 16 e
# x의 값을 이용하여 y값 (sin) 구하기
x = np.linspace(0,14,100)
y = np.sin(x)
# 그래프 그리기
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x18f7da35760>]
n = np.array([1,2,3,4,5])
print(type(n))
n
<class 'numpy.ndarray'>
array([1, 2, 3, 4, 5])
a = [1,2,3]
a*2
[1, 2, 3, 1, 2, 3]
## 벡터 연산 예
a = np.array([1,2,3])
a * 2
array([2, 4, 6])
a = np.arange(1000000)
%time a2 = a**2
CPU times: total: 0 ns Wall time: 0 ns
L = range(1000000)
%time L2 = [i**2 for i in L]
CPU times: total: 219 ms Wall time: 215 ms
### 다차원 생성하기
a1 = [1,2,3]
a2 = [ [11,12,13],
[21,22,23],
[31,32,33]]
a2
[[11, 12, 13], [21, 22, 23], [31, 32, 33]]
b1 = np.array([1,2,3])
b2 = np.array([ [11,12,13],
[21,22,23],
[31,32,33]])
b2
array([[11, 12, 13], [21, 22, 23], [31, 32, 33]])
print("행렬의 차원 : ", b1.ndim, b2.ndim)
print("행렬의 크기 : ", b1.shape, b2.shape)
행렬의 차원 : 1 2 행렬의 크기 : (3,) (3, 3)
a2[0]
[11, 12, 13]
a2[0][1]
12
print(b2[0])
print(b2[0][1])
print(b2[0,1])
[11 12 13] 12 12
### 전체 데이터 중의 짝수만 뽑기
b2[ b2 % 2 ==0]
array([12, 22, 32])
# x의 값을 이용하여 y값 (sin) 구하기
x = np.linspace(0,14,100)
y = np.sin(x)
# 그래프 그리기
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x18f008113d0>]
y1 = np.sin(x)
y2 = np.sin(x) * 2
y3 = np.sin(x) * 4
plt.figure(figsize=(8,6))
plt.plot(x, y1, label="sin(x)")
plt.plot(x, y2, label="sin(x)*2")
plt.plot(x, y3, label="sin(x)*4")
plt.legend()
plt.title("sin plot")
Text(0.5, 1.0, 'sin plot')
plt.figure(figsize=(10,15))
plt.subplot(3, 1, 1)
plt.title("sin plot")
plt.plot(x, y1, 'go-', label='sin(x)')
plt.legend()
plt.subplot(3, 1, 2)
plt.title("sin plot")
plt.plot(x, y2, 'b^-', label='sin(x) * 2')
plt.legend()
plt.subplot(3, 1, 3)
plt.title("sin plot")
plt.plot(x, y3, 'r.-', label='sin(x) * 4')
plt.legend()
plt.show()
fig, ax = plt.subplots(3, 1, figsize=(10,15))
ax[0].plot(x, y1, 'o', label='sin(x)')
ax[0].legend()
ax[1].plot(x, y2, 'o', label='sin(x)')
ax[2].plot(x, y3, 'o', label='sin(x)')
plt.show()
import urllib.request
url = "https://pythonstart.github.io/web/dog01.jpg"
savename = 'dog.jpg'
urllib.request.urlretrieve(url,savename)
print("저장완료")
저장완료
!dir dog*
D 드라이브의 볼륨: wjv_backup 볼륨 일련 번호: D856-9720 D:\Github\DeepLearning_Basic_Class 디렉터리 2022-11-16 오후 06:09 6,847,926 dog.jpg 1개 파일 6,847,926 바이트 0개 디렉터리 8,123,031,552 바이트 남음
fig, ax = plt.subplots()
image = plt.imread("dog.jpg") # 이미지 읽기
print(image)
ax.imshow(image)
[[[117 123 119] [118 124 120] [118 124 120] ... [116 113 108] [118 113 109] [117 112 108]] [[117 123 119] [118 124 120] [119 125 121] ... [117 114 109] [117 114 109] [117 114 109]] [[117 122 118] [118 123 119] [118 124 120] ... [116 113 108] [115 112 107] [116 113 108]] ... [[186 186 188] [186 186 188] [187 187 189] ... [ 83 80 75] [ 82 79 74] [ 80 77 72]] [[184 184 184] [184 184 184] [186 186 186] ... [ 81 78 73] [ 81 78 73] [ 80 77 72]] [[181 181 179] [181 181 179] [184 184 182] ... [ 80 77 70] [ 81 78 71] [ 79 76 69]]]
<matplotlib.image.AxesImage at 0x18f00b98dc0>
### set_axis_off() 이해 - 축을 없애기
### savefig() 이해 - 파일로 저장
fig, ax = plt.subplots()
ax.set_axis_off()
ax.imshow(image)
fig.savefig('Plot_without_axes.png')
!dir Plot_*
D 드라이브의 볼륨: wjv_backup 볼륨 일련 번호: D856-9720 D:\Github\DeepLearning_Basic_Class 디렉터리 2022-11-16 오후 06:09 109,365 Plot_without_axes.png 1개 파일 109,365 바이트 0개 디렉터리 8,123,031,552 바이트 남음
import seaborn as sns
import pandas as pd
tips = sns.load_dataset("tips") ## tips의 데이터 셋 불러오기
tips.head() ## 앞의 데이터만 살펴보기 tips.tail() - 뒤의 데이터 셋
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
### 요일별 식사 금액은 얼마나 될까?
plt.figure(figsize=(8,6))
sns.barplot(x="day", y="total_bill", data=tips)
plt.show()
# 요일별 tip은 남성과 여성은 어떠할까?
plt.figure(figsize=(8,6))
sns.barplot(x="day", y="tip", hue="sex", data=tips) # hue를 이용하여 구분.
plt.title("Tips by day")
plt.show()
iris = sns.load_dataset("iris")
iris.head()
sepal_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
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 |
## csv 데이터 셋, excel 데이터 셋 만들기
iris.to_csv("iris.csv", index=False)
iris.to_excel("iris.xlsx", index=False)
!dir iris*
D 드라이브의 볼륨: wjv_backup 볼륨 일련 번호: D856-9720 D:\Github\DeepLearning_Basic_Class 디렉터리 2022-11-16 오후 06:09 4,009 iris.csv 2022-11-16 오후 06:09 8,750 iris.xlsx 2개 파일 12,759 바이트 0개 디렉터리 8,123,031,552 바이트 남음
## 산점도 그래프 확인
sns.pairplot(iris, hue="species")
<seaborn.axisgrid.PairGrid at 0x18f00c32910>
# 리스트
myfood = ['banana', 'apple', 'candy']
print(myfood[0])
print(myfood[1])
print(myfood[2])
print(myfood[1:3])
banana apple candy ['apple', 'candy']
for item in myfood:
print(item)
banana apple candy
# 딕셔너리
dict1 = {'one':'하나', 'two':"둘", 'three':'셋'}
dict2 = {1:"하나", 2:"둘", 3:"셋"}
dict3 = {'col1':[1,2,3], 'col2':['a','b','c']}
print(dict1)
print(dict2)
print(dict3)
{'one': '하나', 'two': '둘', 'three': '셋'} {1: '하나', 2: '둘', 3: '셋'} {'col1': [1, 2, 3], 'col2': ['a', 'b', 'c']}
print(dict1['one'])
print(dict2[2])
print(dict3['col2'])
하나 둘 ['a', 'b', 'c']
dat = pd.read_csv("iris.csv")
dat.shape # 데이터의 행열 (크기)
(150, 5)
dat.head() ## 앞의 데이터 보기
sepal_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
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 |
dat.tail() ## 뒤의 데이터 보기
sepal_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
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 |
dat.columns ## 데이터의 컬럼보기
Index(['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'], dtype='object')
dat.describe() ## 데이터의 요약값 보기
sepal_length | sepal_width | petal_length | petal_width | |
---|---|---|---|---|
count | 150.000000 | 150.000000 | 150.000000 | 150.000000 |
mean | 5.843333 | 3.057333 | 3.758000 | 1.199333 |
std | 0.828066 | 0.435866 | 1.765298 | 0.762238 |
min | 4.300000 | 2.000000 | 1.000000 | 0.100000 |
25% | 5.100000 | 2.800000 | 1.600000 | 0.300000 |
50% | 5.800000 | 3.000000 | 4.350000 | 1.300000 |
75% | 6.400000 | 3.300000 | 5.100000 | 1.800000 |
max | 7.900000 | 4.400000 | 6.900000 | 2.500000 |
dat.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 150 non-null float64 1 sepal_width 150 non-null float64 2 petal_length 150 non-null float64 3 petal_width 150 non-null float64 4 species 150 non-null object dtypes: float64(4), object(1) memory usage: 6.0+ KB
dat.isnull().sum() # 데이터의 결측치 보기
sepal_length 0 sepal_width 0 petal_length 0 petal_width 0 species 0 dtype: int64