sklearn-导入数据(第1讲)

导入数据     2020/5/27
================================================================================= 
1.1.sklearn中导入数据方法有:pandas.read_csv,np.loadtxt,python csv.reader
1.2.sklearn中数据多为numpy 2D,1D,pd.Series,pd.DataFrame,list
1.3.数据类型多为np.float64,int64

=================================================================================
2.实例:
import csv,pandas as pd,numpy as np

# 使用numpy导入CSV数据
filename = 'pima_data.csv'
with open(filename, 'rt') as raw_data:
    data = np.loadtxt(raw_data, delimiter=',')
    print(data.shape)

# 使用Pandas导入CSV数据
filename = 'pima_data.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = pd.read_csv(filename, names=names)
print(data.shape)

# 使用标准的Python类库导入CSV数据
filename = 'pima_data.csv'
with open(filename, 'rt') as raw_data:
    readers = csv.reader(raw_data, delimiter=',')
    x = list(readers)
    data = np.array(x).astype('float')
    print(data.shape)

================================================================================== 

 

你可能感兴趣的:(sklearn)