시작하기무료로 시작하기

여러 데이터 타입 가져오기

파일 seaslug.txt

  • 문자열로 이루어진 텍스트 헤더가 있고
  • 탭으로 구분되어 있어요.

이 데이터는 주어진 기간 동안 변태를 완료한 바다민달팽이 유생의 비율을 담고 있어요. 자세한 내용은 여기를 참고하세요.

헤더 때문에 np.loadtxt()를 그대로 사용해 가져오면 Python이 ValueError를 발생시키며 could not convert string to float라고 알려줄 거예요. 이를 처리하는 방법은 두 가지예요. 첫째, 데이터 타입 인수 dtypestr(문자열)로 설정하는 방법이에요.

또는 이전에 본 것처럼 skiprows 인수를 사용해 첫 번째 행을 건너뛸 수도 있어요.

이 연습은 강의의 일부입니다

Python에서 데이터 가져오기 입문

강의 보기

연습 안내

  • 첫 번째 np.loadtxt() 호출에서 첫 번째 인수로 file을 전달해 완료하세요.
  • print(data[0])를 실행해 data의 첫 번째 원소를 출력하세요.
  • 두 번째 np.loadtxt() 호출을 완성하세요. 가져올 file탭으로 구분되어 있고, 데이터 타입은 float이며, 첫 번째 행을 건너뛰어야 해요.
  • 이전 print() 호출을 참고해 print() 명령을 완성하여 data_float의 10번째 원소를 출력하세요.
  • 나머지 코드를 실행해 데이터를 시각화하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Assign filename: file
file = 'seaslug.txt'

# Import file: data
data = np.loadtxt(____, delimiter='\t', dtype=str)

# Print the first element of data
print(data[0])

# Import file as floats and skip the first row: data_float
data_float = np.loadtxt(____, delimiter='____', dtype=____, skiprows=____)

# Print the 10th element of data_float
print(____)

# Plot a scatterplot of the data
plt.scatter(data_float[:, 0], data_float[:, 1])
plt.xlabel('time (min.)')
plt.ylabel('percentage of larvae')
plt.show()
코드 편집 및 실행