inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

[작업형2] 연습문제 섹션 4

Section 4. 이직 여부 예측

49

합격

작성한 질문수 40

0

  1. 이 문제에서

     

    train과 test 합쳐서 원핫인코딩

combined = pd.concat([train, test])

combined_dummies = pd.get_dummies(combined)

n_train = len(train)

train = combined_dummies[:n_train]

test = combined_dummies[n_train:]

한 이유가

city 컬럼에서 트레인 유니크 개수>테스트 유니크 개수라서 사용했다고 이해했는데,, 맞을까요?

2. 제가 코드 한거는

print(train.shape, test.shape)

# print(train.isnull().sum())

# print(test.isnull().sum())

print(train.info())

print(test.info())

print(train.describe(include="O"))

print(test.describe(include="O"))

a=set(train['city'])

b=set(test['city'])

print(a-b)

print(b-a)

target=train.pop('target')

df=pd.concat([train, test])

df=pd.get_dummies(df)

train=train.iloc[:len(train)]

test=test.iloc[len(train):]

from sklearn.model_selection import train_test_split

X_tr, X_val, y_tr, y_val = train_test_split(target, train, test_size=0.2, random_state=0)

print(X_tr.shape, X_val.shape, y_tr.shape, y_val.shape)

from sklearn.ensemble import RandomForestClassifier

rf = RandomForestClassifier(random_state=0)

rf.fit(X_tr, y_tr)

pred = rf.predict_proba(X_val)

이렇게 하니까

 

 

~~~~~~

(12260,) (3066,) (12260, 13) (3066, 13)

---------------------------------------------------------------------------

ValueError Traceback (most recent call last)

/tmp/ipython-input-1337250417.py in <cell line: 0>()

24 from sklearn.ensemble import RandomForestClassifier

25 rf = RandomForestClassifier(random_state=0)

---> 26 rf.fit(X_tr, y_tr)

27 pred = rf.predict_proba(X_val)

4 frames

/usr/local/lib/python3.12/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_writeable, force_all_finite, ensure_all_finite, ensure_non_negative, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator, input_name)

1091 "if it contains a single sample."

1092 )

-> 1093 raise ValueError(msg)

1094

1095 if dtype_numeric and hasattr(array.dtype, "kind") and array.dtype.kind in "USV":

ValueError: Expected a 2-dimensional container but got <class 'pandas.core.series.Series'> instead. Pass a DataFrame containing a single row (i.e. single sample) or a single column (i.e. single feature) instead.

이런 에러가 나옵니다ㅠ

분할까지는 했는데,, 랜덤포레스트부터 오류가 뜹니다

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

답변 2

0

퇴근후딴짓

  1. 네 유니크 개수가 서로 달라서입니다.

  2. 아래 정현님이 말씀한대로 train_test_split(target, train)에서 train이랑target위치가 맞지 않습니다.

에러가 난다면 제가 작성한 모범 답안과 비교 부탁드리겠습니다.

0

문정현

split할때 train이랑target위치가 바뀐거같은데

0

퇴근후딴짓

정확합니다. 답변 감사합니다!!

기출 11회 작업형 2_전체 데이터 학습 여부

0

2

1

예측값 결과 소수점 차이

0

14

2

기출 문제와 실전챌린지 연습문제 무엇부터 푸는게 나은가요?

0

13

0

전처리 train() test([ ])

0

11

2

작업형 1 배경지식 질문

0

14

2

옳게 풀은건지 질문드립니다!

0

12

1

roc_auc_score

0

22

2

추가질문 합니다

0

13

2

시험환경 구름

0

15

2

2유형 질문드려요

0

12

2

RandomForest vs lgb

0

22

2

전처리 관련질문

0

21

3

작업형3 기출

0

15

2

유형2에서 데이터분할 생략 가능여부

0

26

2

9회 기출 유형3 질문

0

16

2

lgb 기초편

0

12

1

괄호 사용

0

20

2

작업형 2 데이터 전처리 질문

0

20

1

11회 기출 유형 작업형1 문제 3-1

0

17

1

예시문제 작업형2 (ver2023) 질문입니다

0

18

2

Data type에 따른 처리

0

18

2

데이터 전처리 관련

0

17

2

시험에서 문제 불러오기

0

18

2

2번문제 출력값 질문

0

25

2