일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- 적외선 중계기
- 스마트싱스허브
- 맥북m1
- IOT
- LinearRegression
- 스마트 플러그
- 한글경로
- RM MINI
- Zwave
- QQ플롯
- 필립스 휴
- 필립스 휴 플레이
- slicing
- 가우시안표준정규분포
- It
- 포엠아이
- 브로드링크
- 다윈
- RM MINI3
- Python
- 오뭘샀
- randn
- 리뷰
- newaxis
- 2610
- numpy
- St
- reshape
- 노션
- 스마트싱스
- Today
- Total
창작은 어렵다. 하지만 기록은 쉽다.
python : ValueError: Expected 2D array, got 1D array instead: 본문
python : ValueError: Expected 2D array, got 1D array instead:
2610 2020. 9. 19. 03:00Linear_Regression(선형 회귀분석) 예제를 하던 와중에 한 실수....
fit 메소드는 2차원 배열을 input으로 받는다. 그런데 1차원 배열을 input으로 놨기 때문에 에러 발생
import numpy as np
x = height_weight[:,0] #실수!
y = height_weight[:,1] #실수!
line_fitter = LinearRegression()
line_fitter.fit(x,y) #ERROR!
height_weight 는 1열은 height가, 2열은 weight가 저장된 2차원 행렬이다. shape은 (200,2)
그에 비해 x, y의 shape은 (200, )인 1dim으로 잡히길래
x = np.expand_dims(height_weight[:,0],axis=1)
y = np.expand_dims(height_weight[:,1],axis=1)
나는 이렇게 수동으로 축을 하나 늘려줬다.
하지만 더 쉬운 방법이 있었으니...
x = height_weight[:,:1]
y = height_weight[:,1:]
이렇게 슬라이싱하는 것이었다. 파이썬 슬라이싱에 대한 이해도를 더 늘려야겠다.
참고로 다른 방법도 있다.
1. reshape메소드로 shape바꾸기.
reshape('행',''열')
으로 원하는 shape으로 변환 가능하다.
여기선 reshape(-1,1)을 사용하면 된다. -1은 자동 결정하라는 뜻
2. newaxis로 1차원 증가시키기
arr[:,np.newaxis]
로 원하는곳에 1차원만 증가 가능하다.
참고:
reshape(),newaxis : homeproject.tistory.com/7
slicing 연습 : twpower.github.io/119-python-list-slicing-examples
'Python > Error Code' 카테고리의 다른 글
[python] openCV 한글 경로 입력 (0) | 2021.08.03 |
---|---|
python : TypeError: list indices must be integers or slices, not list (0) | 2020.09.15 |