matplotlib


- 그래프를 그려주는 라이브러리


* 단순한 그래프 그리기

- pyplot 모듈 이용

import numpy as np
import matplotlib.pyplot as plt

# 데이터 준비
x = np.arrange(0, 6, 0.1) # 0에서 6까지 0.1간격으로 생성
y = np.sin(x)

# 그래프 그리기
plt.plot(x, y)
plt.show()


- pyplt의 기능

import numpy as np
import matplotlib.pyplot as plt

# 데이터 준비
x = np.arange(0, 6, 0.1) # 0에서 6까지 0.1간격으로 생성
y1 = np.sin(x)
y2 = np.cos(x)

# 그래프 그리기
plt.plot(x, y1, label="sin")
plt.plot(x, y2, linestyle="--", label="cos")
plt.xlabel("x") # x축 이름
plt.ylabel("y") # y축 이름
plt.title('sin & cos') # 제목
plt.legend()
plt.show()


- 이미지 표시하기

import matplotlib.pyplot as plt
from matplotlib.image import imread

img = imread('image.jpg')

plt.imshow(img)
plt.show()


'파이썬' 카테고리의 다른 글

numpy array, broadcast  (0) 2017.10.03
__init__  (0) 2017.10.03
Three way for counting  (0) 2017.08.30
konlpy 설치  (0) 2017.08.30
beautiful soup 4, lxml, requests 설치 방법  (1) 2017.08.30

numpy array(넘파이 배열)



* 코드

>>> import numpy as np

>>> x = np.array([1.0, 2.0, 3.0])

>>> print(x)

[1. 2. 3.]

>>> type(x)

<class 'numpy.ndarray'>

>>> y = np.array([2.0, 4.0, 6.0])

>>> x * y

array([2., 8., 18.])


* 표현

- 원소별: element-wise

- 원소별 곱셈: element-wise product


* n차원 배열

>>> A = np.array([[1, 2], [3, 4]])

>>> print(A)

[[1 2]

 [3 4]]

>>> A.ahpe

(2, 2)

>>> A.dtype

dtype('int64')


* 원소 접근

>>> X = np.array([[51, 55], [14, 19], [0, 4]])

>>> print(X)

[[51 55]

 [14 19]

 [ 0   4]]

>>> X[0]

array([51, 55])

>>> X[0][1])

55


>>> for row in X:

...        print(row)

...

[[51 55]

 [14 19]

 [ 0   4]]


>>> X = X.flatten()

>>> print(X)

[51 55 14 19 0 4]

>>> X[np.array([0, 2, 4])] # 인덱스가 0, 2, 4인 원소 얻기

array([51, 14, 0])


>>> X > 15

array([ True, True, False, True, False, False], dtype=bool)

>>> X( X>15)

array([51, 55, 19])




braodcast(브로드캐스트)



- 넘파이 배열과 수치 하나(스칼라값) 산술 연산도 가능


* 코드

>>> x = np.array([1.0, 2.0, 3.0])

>>> x / 2.0

array([0.5., 1., 1.5])


* 코드2

>>> A = np.array([[1, 2], [3, 4]])

>>> B = np.array([10, 20]])

>>> A * B

array([[ 10, 40],

        [ 30, 80]])



'파이썬' 카테고리의 다른 글

matplotlib  (0) 2017.10.03
__init__  (0) 2017.10.03
Three way for counting  (0) 2017.08.30
konlpy 설치  (0) 2017.08.30
beautiful soup 4, lxml, requests 설치 방법  (1) 2017.08.30

__init__



- 클래스 정의에서 사용되는 메서드

- 클래스를 초기화하는 방법을 정의

- 생성자(constructor)라고도 부름

- 클래스의 인스턴스가 만들어질 때 한번만 불림

- 매서드의 첫 번째 인수로 자신(자신의 인스턴스)을 나타내는 self를 명시적으로 쓰는 것이 특징


- 인스턴스 변수: 인스턴스별로 저장되는 변수




* 예제


class Man: 

def __init__(self, name):

self.name = name

print("Initialized!")


def hello(self):

print("Hello " + self.name + "!")


def goodbye(self):

print("Good-bye " + self.name + "!")


m = Man("David")

m.hello()

m.goodbye()



* 코드 설명


- m: 인스턴스(객체)

- name: 인수

- self.name: 인스턴스 변수


'파이썬' 카테고리의 다른 글

matplotlib  (0) 2017.10.03
numpy array, broadcast  (0) 2017.10.03
Three way for counting  (0) 2017.08.30
konlpy 설치  (0) 2017.08.30
beautiful soup 4, lxml, requests 설치 방법  (1) 2017.08.30

+ Recent posts