본문 바로가기

Study59

[Python] Numpy 배열 Numpy 라이브러리 불러오기 In [1]: import numpy as np 1차원 배열(ndarray) 만들기 In [2]: list1 = [1, 2, 3, 4, 5] # np의 배열 생성 함수 arr = np.array(list1) arr Out[2]: array([1, 2, 3, 4, 5]) 2차원 배열(array) 만들기 In [3]: list2 = ([1, 2, 3],[4, 5, 6]) #2차원 리스트 생성 arr2 = np.array(list2) #2차원 리스트로 2차원 배열 생성 arr2 Out[3]: array([[1, 2, 3], [4, 5, 6]]) 배열의 크기 확인 In [4]: # shape : 배열의 크기 print(arr.shape) print(arr2.shape) (5,) (.. 2023. 10. 17.
[Python] Matplotlib In [1]: import matplotlib.pyplot as plt In [2]: y = [2, 4, 6] # plot - 선 그래프(Line plot) plt.plot(y) plt.show() In [3]: y = [2, 4, 6] x = [1, 2, 3] # plot - 선 그래프(Line plot) plt.plot(x, y) # x 와 y의 갯수가 맞지 않으면 error plt.show() In [4]: # line style -> ls plt.plot(x,y, ls = '--') plt.show() In [5]: # Marker plt.plot(x, y, marker='o') plt.show() In [6]: x = [2, 4, 6, 8] y = [10, 13, 16, 20] y2 = [10,.. 2023. 10. 16.
[Python] pandas라이브러리 CrimeData 실습 import pandas as pd In [2]: c15 = pd.read_csv('crime/2015.csv', encoding = 'euc-kr', index_col='관서명') c16 = pd.read_csv('crime/2016.csv', encoding = 'euc-kr', index_col='관서명') c17 = pd.read_csv('crime/2017.csv', encoding = 'euc-kr', index_col='관서명') c17 Out[2]: In [3]: total15 = c15.loc[c15['구분'] == '발생건수','살인':].sum(axis=1) total15 Out[3]: 관서명 광주지방경찰청계 18830 광주동부경찰서 2355 광주서부경찰서 4720 광주남부경찰서 21.. 2023. 10. 13.
[Python] 함수 함수 def 함수명(매개변수):실행문장return 반환변수 -> 호출했던 곳으로 감 In [1]: def number_sum(num1, num2): r = num1 + num2 return r # 함수 정의 후 실행 시켰는지 확인하기 In [2]: print(number_sum(2,3)) 5 # 실습문제 In [3]: def cal(num1, num2, op='+') : # op='+' 매개변수 op의 기본값을 + 로 설정 """덧셈과 뺄셈을 계산하는 함수""" # docstring 추가하기 if op == '+' : result = num1 + num2 return result elif op == '-' : result = num1 - num2 return result else : return '연산기.. 2023. 10. 12.