from __future__ import print_function from pandas import DataFrame,Series import pandas as pd datafile='/root/dataset/air_customer_Data/air_data.csv' data=pd.read_csv(datafile,encoding='utf-8') cleanedfile='cleaned.csv' data1=data[data['SUM_YR_1'].notnull() & data['SUM_YR_2'].notnull()] data1 index1=data['SUM_YR_1']!=0 index2=data['SUM_YR_2']!=0 index3=data['SEG_KM_SUM']>0 data1 = data1[(index1 | index2) & index3] data1.to_csv(cleanedfile) data2=data1[['LOAD_TIME','FFP_DATE','LAST_TO_END','FLIGHT_COUNT','SEG_KM_SUM','avg_discount']] data2.to_csv('datadecrese.csv') 3 import numpy as np data=pd.read_csv('datadecrese.csv') data['L']=pd.to_datetime(data['LOAD_TIME'])-pd.to_datetime(data['FFP_DATE']) data['L'] =data['L'].astype("str").str.split().str[0] # 去除数据中的days字符,只输出数字,再转化为数值型数据 data['L'] = data['L'].astype("int") / 30 data.drop(columns=['LOAD_TIME','FFP_DATE'], inplace=True) data.rename(columns = {'LAST_TO_END':'R','FLIGHT_COUNT':'F','SEG_KM_SUM':'M','avg_discount':'C'},inplace=True) data.drop(columns=['Unnamed: 0'], inplace=True) 4 data.describe() 5 P108 data=(data-data.mean())/data.std() 6 import pandas as pd from pandas import DataFrame,Series from sklearn.cluster import KMeans k=5 kmodel=KMeans(n_clusters=k,random_state=3) kmodel.fit(data) 7 import matplotlib import matplotlib.pyplot as plt clu=kmodel.cluster_centers_ x=[1,2,3,4,5] plt.rcParams['font.sans-serif'] = 'SimHei' plt.rcParams['axes.unicode_minus'] = False for i in range(5): plt.plot(x,clu[i]) plt.show
时间: 2024-01-25 09:02:44 浏览: 180
air_netcdf_python_AIR_
这段代码是用Python的Pandas和Scikit-learn库进行数据预处理和聚类分析的。首先,从一个csv文件中读取数据,然后进行数据清洗,去除缺失值和异常值,再将数据保存为新的csv文件。接着,读取新的csv文件,对数据进行标准化处理,再使用KMeans算法进行聚类分析。最后,使用Matplotlib库对聚类结果进行可视化展示。
阅读全文