[Python] 14. NN : XOR문제, MLPClassifier
import numpy as np import pandas as pd import seaborn as sb import matplotlib.pyplot as plt import mglearn from sklearn.model_selection import train_test_split from sklearn.neural_network import MLPClassifier from sklearn.linear_model import LogisticRegression import warnings warnings.simplefilter('ignore') x_data = np.array( [[0,0],[0,1],[1,0],[1,1]]) y_data = np.array( [[0],[1],[1],[0]]) Logis..
[Python] 24. pandas DataFrame 데이터 분석 실습 종합 문제 : sort(), pd.cut(),apply(lambda), sr.quantile(), value_counts(), histogram(), pie chart 예제
import pandas as pd import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rcParams['font.family']='Malgun Gothic' matplotlib.rcParams['axes.unicode_minus'] = False 국어점수 데이터 sr = pd.Series( [90,90,55,60,76,80,76,88,30,25], name="국어점수" ) sr.index=['홍길동','이순신','임꺽정','정난정', '이이','이황','정도전','김철수1', '김철수2','김철수3'] sr [OUT] : 홍길동 90 이순신 90 임꺽정 55 정난정 60 이이 76 이황 80 정도전 76 김철수1..
[Python] 23. pandas DataFrame MultiIndex column : levels, get_level_values(),pd.MultiIndex.from_product()예제
import pandas as pd import numpy as np import matplotlib.pyplot as plt from matplotlib import rc import matplotlib rc('font', family='AppleGothic') plt.rcParams['axes.unicode_minus'] = False data = np.random.randint(1,10, size=4) data array([2, 9, 5, 7]) data = np.random.randint(30,101, size=(4,6) ) data array([[94, 42, 91, 67, 54, 94], [87, 30, 83, 31, 92, 65], [83, 43, 99, 43, 37, 59], [82, 88..
[Python] 17. pandas DataFrame groupby : groupby(), sum(), max(), min(), count(), agg()예제
import pandas as pd import numpy as np import matplotlib.pyplot as plt from matplotlib import rc import matplotlib rc('font', family='AppleGothic') plt.rcParams['axes.unicode_minus'] = False 데이터 불러오기 (pd.read_csv) python 파일 경로에 data폴더 만든 후 다음의 hotel.xlsx 파일 넣어놓기 df = pd.read_excel('data/hotel.xlsx',index_col=0) df.head() df['grade'].unique() [OUT] : array([10, 8, 6, 9, 7, 5]) df['grade'].value_c..
[Python] 15. pandas DataFrame 통계&시각화 : sum(), unstack(), mean(), values, min(), idxmax(), median(), quantile(), count(), describe()예제
import pandas as pd import numpy as np import matplotlib.pyplot as plt from matplotlib import rc import matplotlib rc('font', family='AppleGothic') plt.rcParams['axes.unicode_minus'] = False data = {'eng':[10,30,50,70], 'kor':[20,40,60,80], 'math':[90,50,20,70]} df = pd.DataFrame(data, index=['a','b','c','d'] ) df def fn( v ): print("=========") print("v=>",v) print("=========") return v.sum() d..
[Python] 13. pandas DataFrame 추가 수정 삭제 검색 정렬 : dropna(subset=[]),fillna(),isna(),sort_values(),sort_index()예제
import pandas as pd import numpy as np data = {'eng':[10,30,50,70], 'kor':[20,40,60,80], 'math':[90,50,20,70]} df = pd.DataFrame(data, index=['a','b','c','d'] ) df 추가 컬럼 추가 df['my1'] =[1,2,3,4] #컬럼이 있으면 수정, 없으면 추가 df['my2'] = df['kor'] + df['eng'] df 행 추가 data = {'eng':[10,30,50,70], 'kor':[20,40,60,80], 'math':[90,50,20,70]} df = pd.DataFrame(data, index=['a','b','c','d'] ) df df.loc['e'] =[1,2..
[Python] 11. pandas DataFrame 인덱싱(Indexing), 슬라이싱(Slicing) 예제
import pandas as pd import numpy as np data = {'eng':[10,30,50,70], 'kor':[20,40,60,80], 'math':[90,50,20,70]} df = pd.DataFrame(data, index=['a','b','c','d'] ) df # df['a'] # df['컬럼명'] df['eng'] [OUT] : a 10 b 30 c 50 d 70 Name: eng, dtype: int64 df[['eng','math']] df['eng']['a':'c'] [OUT] : a 10 b 30 c 50 Name: eng, dtype: int64 df[['eng']] # df[슬라이싱(row)] df[1:3] df df['b':'c'] iloc 인덱싱 슬라이싱(..
[Python] 10. pandas DataFrame 속성 : ndim, shape, len(), size, T, index, keys(), columns, values, dtypes, info() 예제
import pandas as pd import numpy as np data = {'eng':[10,30,50,70], 'kor':[20,40,60,80], 'math':[90,50,20,70]} df = pd.DataFrame(data, index=['a','b','c','d'] ) df df.ndim #몇차원 [OUT] : 2 df.shape #(행, 열) [OUT] : (4, 3) df.shape[0] #행의 갯수 [OUT] : 4 len(df) #행의 갯수 [OUT] : 4 df.size #데이터의 갯수 [OUT] : 4 df.T df.index [OUT] : Index(['a', 'b', 'c', 'd'], dtype='object') df.keys() [OUT] : Index(['eng', ..
[Python] 09. pandas DataFrame
import pandas as pd import numpy as np d1 = [[1,2],[3,4],[5,6]] d2 = [(1,2),(3,4),(5,6)] d3 = [{'kor':1,'eng':2}, {'kor':3,'eng':4}, {'kor':5,'eng':6}, ] d4 = {'kor':[1,3,5],'eng':[2,4,6]} df1 = pd.DataFrame(d1, index=['a','b','c'],columns=['eng','kor']) df1 df2 = pd.DataFrame(d2) df2 df3 = pd.DataFrame(d3) df3 df4 = pd.DataFrame(d4) df4 review - DataFrame 만들기
[Python] 04. pandas Series 연산 : 산술관계논리(element wise),isin(),between()예제
import pandas as pd import numpy as np data = {'aa':10,'bb':20,'cc':30,'dd':40, 'ee':50} sr = pd.Series(data, name='국어점수') sr [OUT] : aa 10 bb 20 cc 30 dd 40 ee 50 Name: 국어점수, dtype: int64 sr.index [OUT] : Index(['aa', 'bb', 'cc', 'dd', 'ee'], dtype='object') 산술관계논리(element wise) sr+1 [OUT] : aa 11 bb 21 cc 31 dd 41 ee 51 Name: 국어점수, dtype: int64 sr*2 [OUT] : aa 20 bb 40 cc 60 dd 80 ee 100 Name:..
[Python] 07. numpy 종합 예제 : matplotlib,시각화,np.loadtxt(),plt.show(),np.array(),zip(),np.quantile()
import numpy as np 데이터 불러오기 (np.loadtxt) python 파일 경로에 data폴더 만든 후 다음의 births.txt 파일 넣어놓기 birth = np.loadtxt( 'data/births.txt', delimiter=',', dtype=np.int64) # data폴더 안의 births.txt파일 ','로 구분하여 load하기 birth.shape # 132행 3열 [OUT] : (132, 3) birth # 연도, 남아수, 여아수로 구성되어있음 [OUT] : array([[ 1880, 90993, 110491], [ 1881, 91954, 100745], [ 1882, 107850, 113688], [ 1883, 112321, 104629], [ 1884, 129022,..
[Python] 06. numpy 2차원 데이터 : boolean indexing,vstack(),hstack(),axis=0,axis=1예제
import numpy as np data = np.array( [ [1,2,3],[7,5,2],[10,1,8],[6,3,4] ] ) data [OUT] : array([[ 1, 2, 3], [ 7, 5, 2], [10, 1, 8], [ 6, 3, 4]]) 2차원 데이터 인덱싱(indexing), 슬라이싱(slicing) 예제 data.shape [OUT] : (4, 3) data.reshape(-1,2,2) # [면,행,열], -1은 알아서 설정하라는 의미 [OUT] : array([[[ 1, 2], [ 3, 7]], [[ 5, 2], [10, 1]], [[ 8, 6], [ 3, 4]]]) data # 위에서 reshape을 했지만 data에 담지 않았으므로 바뀌지 않음 [OUT] : array([[ ..