[데이터 사이언스] 5. Python

aliceshard·2023년 4월 12일
0

Pandas 문법

  • 꺽쇠 괄호 안에서의 Series에 대한 수식은 True/False 테이블로 같은 크기를 가지며 리턴된다.
s4[s4 > s4.median()]
  • DataFrame은 해시맵으로 정의한다.
d =  pd.DataFrame({ 'Name': pd.Series(['Alice','Bob','Chris']), 
                  'Age': pd.Series([ 21,25,23]) } )
  • DataFrame의 인덱싱 리턴 결과는 Series다.
  • loc과 iloc은 꺽쇠 괄호 안에 꺽쇠 괄호로 인수를 준다.
df_sub.loc[10:20,['rank','sex','salary']]
df_sub.iloc[10:20, [0,3,4,5]]
  • sort_values의 인수는 'ascending'이지 'descending'이 아니다.
  • sort_values의 복수 기준을 배열로 전달할 수 있다.
df_sorted = df.sort_values(by = ['service', 'salary'], ascending = [True,False])
  • isnull(), notnull()은 모든 엔트리를 돌아다니며 null 여부의 T/F를 리턴한다.
  • DataFrame에서 sum() 메소드는 컬럼 별로 합쳐서 보여준다. 또한 T/F = 1/0으로 간주한다.
flights[['dep_delay','arr_delay']].isnull().sum()
  • agg는 배열로 준다. 복수 기존일 경우 해시맵으로 준다.
flights[['dep_delay','arr_delay']].agg(['min','mean','max'])
flights.agg({'dep_delay':['min','mean',max], 'carrier':['nunique']})
  • value_counts()는 각 종류별로 발생 빈도(support count)를 리턴한다.

mlxtend 문법

  • TransactionEncoder는 mlxtend.preprocessing에서, apriori와 association_rules는 mlxtend.frequent_patterns에서 가져온다.
  • 원본 행렬 데이터에서 가공법은 te.fit( -> te_ary1.transform()
  • 가공된 teary2의 컬럼을 가져올 때는 te_columns를 사용한다.
  • apriori의 입력으로 받는 것은 가공이 다 되어서 T/F만 있는 데이터 프레임.
frequent_items = apriori(df1, min_support=0.05, use_colnames=True)
  • 패턴은 모두 해시맵으로 저장된다.
rules[rules['antecedents'] == {'Eggs', 'Kidney Beans'}]
  • pd에서는 get_dummies라는 메서드로 T/F인코딩을 바로 지원. 바로 Apriori에 넣어도 된다.
  • 복수의 조건으로 인덱싱 할 때는 괄호가 필요하다.
rules[(rules['lift'] >= 2) & (rules['confidence'] >= 0.6) & (rules['support'] >= 0.2)]

matplotlib 문법

  • 3가지 사용법이 있다.
    1) matplotlib 라이브러리 직접 사용
plt.hist(df['salary'],bins=8, density=1)

2) seaborn package와 연계해서 사용

sns.distplot(df['salary'])

3) pandas에 있는 plot(kind='') 사용

df.groupby(['rank'])['salary'].count().plot(kind='bar')
  • 대체로 seaborn이 유용하다. 그냥 바로바로 구현해주기 때문.

    barplot, violinplot, regplot, boxplot, swarmplot, catplot, pairplot

profile
안녕하세요.

0개의 댓글