Hello everyone, today I will share with you two ways to import CSV files when we use Python for data analysis.
Introduction to iris data set CSV
Dataset: iris.csv
Its structure is as follows. It is a matrix with 150 rows and 5 columns. The first four columns belong to the input features, and the last column belongs to the classification label.
numpy import CSV
import numpy as np
filename='D:/数学建模2022/算法/SVM/iris.csv'
with open(filename,'rt') as raw_data: #一个文件如果要导入python进行后续操作,必须先打开它,并返回一个文件对象
iris=np.loadtxt(raw_data,delimiter=',')
print(iris)
print(iris.shape)
Partial results:
pandas import CSV (my favorite for this)
Pandas has the advantage that you can name the columns
import pandas as pd
file_name='D:/数学建模2022/算法/SVM/iris.csv'
names=['separ-length','separ-width','petal_length','petal_width','class']
data=pd.read_csv(file_name,names=names)
print(data)
print(data.shape)
Well, that's the end of today's sharing, see you next time!