问题
I'm currently indexing a CSV with values below and running into the error:
ValueError: Found input variables with inconsistent numbers of samples: [1, 514]
It's examining it as 1 row with 514 columns which emphasize that I have called a specific parameter wrong or is it due to me removing NaN's (which most of the data would default as?)
"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:
As there is only 1 feature in your X_train, its current shape is (n_samples,)
. But scikit estimators require X to be of shape (n_samples, n_features)
. So you need to reshape your data.
Use this:
logreg.fit(X_train.reshape(-1,1), y_train).fillna(0.0)
来源:https://stackoverflow.com/questions/44429600/indexing-a-csv-running-into-inconsistent-number-of-samples-for-logistic-regressi