2017-03-27 4 views
0

私は場所、緯度、経度、犯罪タイプなどの詳細を持つCSVファイルを持っています。 他の属性が与えられている場合、犯罪タイプを予測するような分類子を作りたいと思います。 今、私はsklearnを使ってこれを訓練したいが、sklearnは浮動小数点値を必要とする。文字列をターゲット(y)とフィーチャ(X)の値として持つpandasデータフレームを使用してsvmをトレーニングする方法はありますか?

import os 
    import glob 
    import pandas as pd 
    from sklearn.cross_validation import train_test_split 
    from sklearn import svm 
    from sklearn import preprocessing 
    from sklearn.feature_extraction.text import TfidfTransformer 

    input_file_name = [] 
    input_file = [] 
    frame = pd.DataFrame() 
    fields = ['Reported by', 'Falls within', 'Longitude', 'Latitude', 'Location', 'LSOA code', 'LSOA name', 'Crime type'] 
    to_drop = ['No Location'] 

    for filename in glob.glob(os.path.join('/home/prakhar/Programming/Minor/2017-01', '*.csv')): 
      input_file_name.append(filename) 

    for ifn in input_file_name: 
     input_file.append(pd.read_csv(ifn, error_bad_lines=False, skipinitialspace=True, usecols=fields)); 

    frame = pd.concat(input_file, ignore_index=True)  
    frame = frame[~frame['Location'].isin(to_drop)] 

    x, y = frame.iloc[:,:-1], frame.iloc[:, -1] 
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=1) 

frame.head()

  Reported by   Falls within Longitude Latitude \ 
0 Thames Valley Police Thames Valley Police -0.972323 51.991460 
1 Thames Valley Police Thames Valley Police -0.981511 51.997312 
2 Thames Valley Police Thames Valley Police -0.970516 51.992128 
3 Thames Valley Police Thames Valley Police -0.973412 51.992225 
4 Thames Valley Police Thames Valley Police -0.984120 51.997263 

        Location LSOA code   LSOA name \ 
0  On or near Osprey Walk E01017648 Aylesbury Vale 001A 
1 On or near Portfield Close E01017648 Aylesbury Vale 001A 
2  On or near Lime Avenue E01017648 Aylesbury Vale 001A 
3  On or near Martin Close E01017648 Aylesbury Vale 001A 
4 On or near Mckenzie Close E01017649 Aylesbury Vale 001B 

        Crime type 
0  Anti-social behaviour 
1  Anti-social behaviour 
2    Other theft 
3    Vehicle crime 
4 Criminal damage and arson 
+0

あなたはまだそれを試してみましたHVE?私は目標(y)を浮動させる必要があるとは思わない。 –

+0

はい私はそれを試してみました。実際には、ターゲットといくつかのデータ値も文字列です私はcsvの頭を追加させてください。 –

+0

他の値(Xの機能と同様)は文字列であってはなりません –

答えて

0

の出力は、次のことを試してみてください。

from sklearn.preprocessing import LabelEncoder, OneHotEncoder, StandardScaler 

y = frame['Crime type'].values 
# convert the labels from strings to numbers (0,1,2....) 
y = LabelEncoder().fit_transform(y) 

frame = frame.drop(['Crime type'], axis=1) 

for f in frame.columns: 
    if frame[f].dtype == 'object': 
     lbl_enc = LabelEncoder() 
     # same as above encoding. it takes every object dtype from 
     # pandas dataframe and converts to numerical labels 
     frame[f] = lbl_enc.fit_transform(frame[f].values) 

X = frame.values 
# binarize the encoded columns. this is not needed if you are using a tree based algorithm 
ohe = OneHotEncoder(categorical_features=[0, 1, 4, 5, 6]) 
X = ohe.fit_transform(X) 

# use the following for SVMs (with_mean=False for sparse data) 
scl = StandardScaler(with_mean=False) 
X = scl.fit_transform(X) 
# fit model here: model.fit(X, y) 
+0

ValueError:スパース行列を中心に置くことはできません。代わりに 'with_mean = False'を渡してください。動機付けと代替案については、docstringを参照してください。 また、何が起こっているか説明してください。 –

+0

私は自分の答えを更新しました。 –

+0

X = ohe.fit_transform(X)でメモリエラーが発生しました エラーは長すぎてここに投稿できません。 –

関連する問題