inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[퇴근후딴짓] 빅데이터 분석기사 실기 (작업형1,2,3)

작업형2 모의문제2

세션 종료

해결된 질문

186

세린

작성한 질문수 12

0

import pandas as pd

from sklearn.ensemble import RandomForestRegressor

from sklearn.model_selection import train_test_split

from sklearn.metrics import mean_squared_error


df1 = pd.read_csv('/content/drive/MyDrive/bigdata(빅분기 놀이터)/작업형2 모의문제/모의문제 2번/train.csv')


# print(df1.head())

# print(df1.info())

# print(df1.shape)


df2 = pd.read_csv('/content/drive/MyDrive/bigdata(빅분기 놀이터)/작업형2 모의문제/모의문제 2번/test.csv')


# print(df2.head())

# print(df2.info())

# print(df2.shape)


df1['name'] = df1['name'].fillna(df1['name'].mode()[0])

df1['host_name'] = df1['host_name'].fillna(df1['name'].mode()[0])

df1['last_review'] = df1['last_review'].fillna(df1['name'].mode()[0])

df1['reviews_per_month'] = df1['reviews_per_month'].fillna(df1['reviews_per_month'].median())


df2['name'] = df2['name'].fillna(df2['name'].mode()[0])

df2['host_name'] = df2['host_name'].fillna(df2['name'].mode()[0])

df2['last_review'] = df2['last_review'].fillna(df2['name'].mode()[0])

df2['reviews_per_month'] = df2['reviews_per_month'].fillna(df2['reviews_per_month'].median())


x = df1.drop('price', axis=1)

y = df1['price']


x_encoded = pd.get_dummies(x)


x_train, x_valid, y_train, y_valid = train_test_split(x_encoded.drop('id', axis=1), y, test_size=0.25)


md = RandomForestRegressor(n_estimators=300)

md.fit(x_train, y_train)


pred = md.predict(x_valid)


print(mean_squared_error(y_valid, pred)) # MSE

print(mean_squared_error(y_valid, pred, squared=False)) # RMSE


x_test = df2

x_test_encoded = pd.get_dummies(x_test)


md = RandomForestRegressor(n_estimators=300)

md.fit(x_encoded.drop('id', axis=1), y)


pred = md.predict_proba(x_test_encoded.drop('id', axis=1))


print(pred)

print(pred.shape)


result = pd.DataFrame({'id' : df2['id'], 'price' : pred })


result.to_csv('작업형2 모의문제 2번.csv', index=False)

python 머신러닝 빅데이터 pandas 빅데이터분석기사

답변 1

0

퇴근후딴짓

회귀인데 predict_proba는 잘못된 방법입니다.

0

세린

코드를 수정했는데,

애초에

md.fit(x_train, y_train)

이 부분에서 계속 실행되다가 세션이 다운됩니다. 도대체 왜 이런 걸까요 ㅠㅠ

0

퇴근후딴짓

x_encoded = pd.get_dummies(x)

이 작업 이후 shape을 확인해보셨을까요?

너무 데이터가 커지면 램초과로 세션이 종료됩니다.

범주형데이터 카테고리가 많다면 라벨인코딩 추천합니다.

질문 드립니다.

0

38

2

강의 내용 관련 질문드립니다~

0

35

2

수강 연장 문의

0

33

1

강의자료 일괄 다운로드

0

43

2

수강기간 연장 문의드립니다

0

29

1

list 문제 질문드립니다~

0

31

2

빅분기 실기 12회 재도전

0

40

1

강의 기간 연장 가능여부 검토 요청건

0

30

1

수강기간 연장 문의 드립니다

0

36

2

수강기간 연장 문의드립니다

0

47

2

질문이요

0

51

2

수강기간 연장 문의드립니다.

0

50

2

문제 3-2 질문드립니다

0

44

2

수강기간 연장 문의 드립니다.

0

58

2

변수, 칼럼 , df 구분

0

50

2

수강기간 연장 문의드립니다.

0

50

2

수강기간 연장 문의

0

48

2

수강기간 연장 문의드립니다.

0

42

2

수강기한 연장 문의

0

80

2

수강기간 연장 문의드립니다

0

57

2

결정트리에서 적절한 깊이 선택 후 시각화 과정에서 학습 데이터만 사용하는 이유

0

45

2

수강기간 연장 문의드립니다.

0

71

2

수강연장 문의

0

77

2

수강연장문의

0

53

2