본문 바로가기

728x90
반응형
SMALL

데이터핸들링

(9)
[Python] 11. softmax import numpy as np def fn(x): print(x/x.sum()) a = np.array([2.0,1.0,0.1]) fn(a) [OUT]: [0.64516129 0.32258065 0.03225806] -> 전체 합에서 차지하는 비율 Softmax def softmax(x): e = np.exp(x) print(e) print( e/np.sum(e)) a = np.array([2.0,1.0,0.1]) softmax(a) [OUT]: [7.3890561 2.71828183 1.10517092] [0.65900114 0.24243297 0.09856589] -> e^x를 하여 확률이 높은곳에 가중치를 더 주는 형식 review - 다중분류 시 사용되는 softmax
[Python] 3. 다중선형회귀 import pandas as pd import numpy as np import seaborn as sns from sklearn.datasets import load_boston, load_iris from sklearn.datasets import fetch_california_housing from sklearn.linear_model import LinearRegression,Ridge, SGDRegressor from sklearn.neural_network import MLPRegressor from sklearn.model_selection import train_test_split from sklearn.model_selection import GridSearchCV from sklear..
[Python] 2. 대표값, 산포도, 정규화, 도수 import numpy as np import pandas as pd import matplotlib.pyplot as plt from scipy.stats import mode 데이터 불러오기 (pd.read_csv) python 파일 경로에 data3 폴더 만든 후 다음의 ch2_scores_em.csv파일 넣어놓기 대표값 평균값 중앙값 최빈값 df = pd.read_csv('data3/ch2_scores_em.csv', index_col='student number') df.head() scores = df['english'].values scores [OUT] : array([42, 69, 56, 41, 57, 48, 65, 49, 65, 58, 70, 47, 51, 64, 62, 70, 71, ..
[Python] 18. pandas DataFrame pivot_table : groupby(), sum(), max(), min(), count(), agg(), aggfunc=[] 예제 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_csv('data/grade.csv') df g = df.groupby('학년') g.mean() df.info() # pivot_table에서는 수치형 데이터들의 mean 계산(기본) [OUT] : RangeIndex..
[Python] 16. pandas DataFrame 상관관계 : corr(),heatmap()예제 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폴더 만든 후 다음의 crime_in_Seoul.csv 파일 넣어놓기 df = pd.read_csv('data/crime_in_Seoul.csv',index_col=0,encoding = 'euc-kr',thousands = ',') df.head(20) 상관관계 r이 -1.0과 -0.7 사이이면..
[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] 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] 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([[ ..
[Python] 03. numpy 인덱싱(Indexing),슬라이싱(Slicing) : boolean indexing, np.array()예제 import numpy as np arr = np.array([11,22,33,44,55]) arr [OUT] : array([11, 22, 33, 44, 55]) 인덱싱(indexing) arr[0] [OUT] : 11 arr[-1] [OUT] : 55 arr[[0,2,3]] # 복수개의 인덱스 선택 [OUT] : array([11, 33, 44]) Boolean indexing (element-wise) arr[[True,True,False,False,True]] [OUT] : array([11, 22, 55]) arr>30 [OUT] : array([False, False, True, True, True]) arr[arr>30] [OUT] : array([33, 44, 55]) 슬라이싱(slicin..

728x90
반응형
LIST