Home 빅분기 실기 대비 이기적 4주차 예상 문제 풀이 2
Post
Cancel

빅분기 실기 대비 이기적 4주차 예상 문제 풀이 2

작업형 1유형

데이터 출처 :https://www.kaggle.com/adityakadiwal/water-potability

(참고, 데이터 수정)

데이터 설명 : 수질 음용성 여부 (Potablillity 컬럼 : 0 ,1 )

Data url :https://raw.githubusercontent.com/Datamanim/datarepo/main/waters/train.csv

데이터 상위 5개 컬럼

img1 daumcdn

1
2
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/Datamanim/datarepo/main/waters/train.csv")

Q1. ph값은 상당히 많은 결측치를 포함한다. 결측치를 제외한 나머지 데이터들 중 사분위값 기준 하위 25%의 값들의 평균값은?

1
2
target = df['ph'].dropna()
target.loc[target <= target.quantile(0.25)].mean()

아닌 것만 찾으면 되는데 순간 notnull()때문에 해맸네요 dropna를 통해서 결측치를 다 날려주시고 quantile을 이용하면 사분위값을 편리하게 찾을 수 있습니다. Pandas로 이상치 처리하기 quantile 사용

quantile을 가지고 IQR을 찾는 것은 위 링크를 참고해주시길 바랍니다.

작업형 2유형

데이터 출처 :[https://www.kaggle.com/adityakadiwal/water-potability](https://www.kaggle.com/adityakadiwal/water-potability(참고, 데이터 수정)

데이터 설명 : 수질 음용성 여부 (Potablillity 컬럼 : 0 ,1 )

문제타입 : 분류유형

평가지표 : accuracy

trainData url : https://raw.githubusercontent.com/Datamanim/datarepo/main/waters/train.csv

testData url : https://raw.githubusercontent.com/Datamanim/datarepo/main/waters/test.csv​

데이터 상위 5개 컬럼

img1 daumcdn

Potability에서 0과 1을 예측하는 문제입니다.

1
2
y = train['Potability']
train.drop(columns='Potability', inplace=True)

Potability부터 빼버리고 시작합시다.

1
train.isnull().sum()

img1 daumcdn

문제는 결측치가 좀 있습니다. 이번에는 전부다 표준화처리를 하고 결측치를 0으로 (평균으로) 대체하도록 하겠습니다. 표준화를 수행했을때 평균이 0이고 분산이 1인 것은 이전에도 여러번 말했으니 넘어가겠습니다.

1
2
3
4
5
6
7
8
all_feautre  = train.columns
from sklearn.preprocessing import StandardScaler
Scaler = StandardScaler()
Scaler.fit(train)
train[all_feautre] = Scaler.transform(train)
test[all_feautre] = Scaler.transform(test)
train = train.fillna(0)
test = test.fillna(0)

표준화 처리를 하고 fillna를 이용해서 결측치를 0으로 전부 채웠습니다.

1
2
3
4
5
6
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier()
model.fit(train,y)
summit = pd.DataFrame()
summit['Potability'] = model.predict(test)
summit.to_csv('Potability_pre.csv', index=False)

끝!

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.

빅분기 실기 대비 이기적 4주차 예상 문제 풀이 1

빅분기 실기 대비 이기적 5주차 예상 문제 풀이 1