1.第一步讀入泰坦尼克號數據集
import pandas as pd data = pd.read_csv(r".\Narrativedata.csv" ,index_col=0 )#index_col=0將第0列作為索引,不寫則認為第0列為特征 data.head()
2.通過df的loc的函數從df中取出一列的數據,該數據格式為 : <class 'pandas.core.series.Series'>
age1=data.loc[:,"Age"] print(type(age1)) age1.head()
會發現數據維度:(891,)
3.數據維度的轉換 升維
因為sklearn里面的數據必須是二維
1.使用Series類的 to_dataframe
print(type(Age.to_frame())) print(Age.to_frame().shape) Age.to_frame().head()
2.使用reshape和values.reshape
age1=Age.reshape(-1,1)
age2=Age.values.reshape(-1,1)
print(type(age2)) age2.shape
3.數據的降維
DataFrame 有個 apply
方法,就是把函數映射到 DataFrame 里面每個 Series 上,對 Series 進行操作。這是一種降維操作。