안녕하세요. (주)씨앤텍시스템즈입니다.
이번에 Spark 2.x의 주요 데이터처리 타입인 DataFrame과 널리 알려진 타이타닉 데이터를 Spark Dataframe으로 처리하는 예제를 수행해보겠습니다.
본 포스팅 앞의 글에서 2020/01/09 - [Bigdata] - Apache Spark란? 을 통해 Spark란 무엇인지,
Apache Spark란?
안녕하세요 씨앤텍시스템즈입니다. 이번 포스팅은 빅데이터 소프트웨어이자 가장 화두인 Apache Spark에 대해서 살펴보겠습니다. 1. Apache Spark이란? Apache Spark는 인-메모리 기반 통합 컴퓨팅 엔진이며, 빅데..
cntechsystems.tistory.com
와 2020/02/13 - [Bigdata] - Apache Spark 기능에 대해서 알아 보았습니다.
Apache Spark 기능
안녕하세요 씨앤텍시스템즈입니다. 이번 포스팅은 이전 포스팅인 Apache Spark란?에 이어서 Spark 기능에 대해서 살펴보겠습니다. Apache Spark의 다양한 기능 중 메인 기능인 아래 3가지를 주로 살펴보겠습니다...
cntechsystems.tistory.com
이번엔 Spark 기능 중 DataFrame을 이용하여 타이타닉을 분석해보는 시간을 가지고 데이터는 kaggle에서 참고하였습니다.
Titanic Meta¶
- Passengerid : 승객번호
- Survived : 생존여부(1:생존, 0:사망)
- Pclass : 승선 등급(1: 1st, 2:2nd, 3:3rd)
- Name : 승객 이름
- Sex : 성별
- Age : 나이
- SibSp : 동행자 수(형제)
- Patch : 동행자 수(부모)
- Ticket : 티켓번호
- Fare : 티켓요금
- Cabin : 객실번호
- Embarked : 승선 항구명(C: Cherbourg, Q: Queenstown, S: Southampton)
In [29]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set()
데이터로드¶
In [12]:
titanic = spark.read.csv("data/kaggle-titanic-train.csv", inferSchema=True, header=True)
In [13]:
titanic.printSchema()
In [14]:
titanic.show()
In [15]:
titanic.count()
Out[15]:
In [16]:
titanic.summary().show()
In [17]:
titanic_pandas = titanic.toPandas()
In [19]:
titanic_pandas.describe()
Out[19]:
In [20]:
sns.countplot(x='Survived', data = titanic_pandas)
Out[20]:
In [24]:
print(titanic_pandas.Survived.sum()/titanic_pandas.Survived.count())
In [21]:
titanic_pandas.groupby(['Survived', 'Sex'])['Survived'].count()
Out[21]:
In [25]:
sns.catplot(x='Sex', col='Survived', kind='count', data=titanic_pandas)
Out[25]:
In [26]:
titanic_pandas[titanic_pandas.Sex == 'female'].Survived.sum()/titanic_pandas[titanic_pandas.Sex == 'female'].Survived.count()
Out[26]:
In [27]:
titanic_pandas[titanic_pandas.Sex == 'male'].Survived.sum()/titanic_pandas[titanic_pandas.Sex == 'male'].Survived.count()
Out[27]:
In [32]:
#성별에 따른 생존율
f, ax = plt.subplots(1, 2, figsize=(16,7))
titanic_pandas['Survived'][titanic_pandas['Sex'] == 'male'].value_counts().plot.pie(explode=[0,0.2], autopct='%1.1f%%', ax=ax[0], shadow=True)
titanic_pandas['Survived'][titanic_pandas['Sex'] == 'female'].value_counts().plot.pie(explode=[0,0.2], autopct='%1.1f%%', ax=ax[1], shadow=True)
ax[0].set_title('Survived Male')
ax[1].set_title('Survived Female')
plt.show()
In [33]:
#등급별 생존율
pd.crosstab(titanic_pandas.Pclass, titanic_pandas.Survived, margins=True)
Out[33]:
In [34]:
titanic_pandas.Survived[titanic_pandas.Pclass == 1].sum()/titanic_pandas.Survived[titanic_pandas.Pclass == 1].count()
Out[34]:
In [35]:
titanic_pandas.Survived[titanic_pandas.Pclass == 2].sum()/titanic_pandas.Survived[titanic_pandas.Pclass == 2].count()
Out[35]:
In [36]:
titanic_pandas.Survived[titanic_pandas.Pclass == 3].sum()/titanic_pandas.Survived[titanic_pandas.Pclass == 3].count()
Out[36]:
In [37]:
sns.catplot('Pclass', 'Survived', kind='point', data=titanic_pandas)
Out[37]:
In [39]:
pd.crosstab([titanic_pandas.Sex, titanic_pandas.Survived], [titanic_pandas.Pclass])
Out[39]:
In [40]:
sns.catplot('Pclass', 'Survived', hue='Sex', kind='point', data=titanic_pandas)
Out[40]:
In [41]:
sns.catplot(x='Survived', col='Embarked', kind='count', data=titanic_pandas)
Out[41]:
In [ ]:
728x90
'Data > Bigdata' 카테고리의 다른 글
Spark을 이용한 Deeplearning (0) | 2020.06.11 |
---|---|
Spark SQL(Pyspark) (0) | 2020.05.26 |
R을 이용한 Bioinformatics (Bioconductor) (1) | 2020.04.20 |
Apache Spark 기능 (0) | 2020.02.13 |
Elastic Search란? (0) | 2020.01.20 |