对运行为不一致样本数的CSV建立索引以进行逻辑回归

时间:2017-06-08 07:43:15

标签: python pandas scikit-learn logistic-regression sklearn-pandas

我目前正在使用以下值对CSV进行索引并遇到错误:

  

ValueError:找到数量不一致的输入变量   样本:[1,514]

它正在检查它是一行有514列,强调我调用了一个特定的参数错误,或者是因为我删除了NaN(大多数数据默认为?)

"Classification","DGMLEN","IPLEN","TTL","IP"
"1","0.000000","192.168.1.5","185.60.216.35","TLSv1.2"
"2","0.000160","192.168.1.5","185.60.216.35","TCP"
"3","0.000161","192.168.1.5","185.60.216.35","TLSv1.2"


import pandas  
df = pandas.read_csv('wcdemo.csv', header=0,
                  names = ["Classification", "DGMLEN", "IPLEN", "TTL", "IP"], 
                  na_values='.')

df = df.apply(pandas.to_numeric, errors='coerce')
#Data=pd.read_csv ('wcdemo.csv').reset_index()#index_col='false')
feature_cols=['Classification','DGMLEN','IPLEN','IP']

X=df[feature_cols]


    #datanewframe = pandas.Series(['Classification', 'DGMLEN', 'IPLEN', 'TTL', 'IP'], dtype='object')

#df = pandas.read_csv('wcdemo.csv')
#indexed_df = df.set_index(['Classification', 'DGMLEN','IPLEN','TTL','IP']


df['IPLEN'] = pandas.to_numeric(df['IPLEN'], errors='coerce').fillna(0)
df['TTL'] = pandas.to_numeric(df['TTL'], errors='coerce').fillna(0)

#DEFINE X TRAIN
X_train = df['IPLEN']
y_train = df['TTL']

#s = pandas.Series(['Classification', 'DGMLEN', 'IPLEN', 'TTL', 'IP'])

Y=df['TTL'] 

from sklearn.linear_model import LogisticRegression

logreg=LogisticRegression()
logreg.fit(X_train,y_train,).fillna(0.0)

#with the error being triggered here 
logreg.fit(X_train,y_train,).fillna(0.0)

1 个答案:

答案 0 :(得分:1)

由于X_train中只有一个功能,因此其当前形状为(n_samples,)。但是scikit估计器要求X具有(n_samples, n_features)的形状。所以你需要重塑你的数据。

使用此:

logreg.fit(X_train.reshape(-1,1), y_train).fillna(0.0)