私は場所、緯度、経度、犯罪タイプなどの詳細を持つ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
あなたはまだそれを試してみましたHVE?私は目標(y)を浮動させる必要があるとは思わない。 –
はい私はそれを試してみました。実際には、ターゲットといくつかのデータ値も文字列です私はcsvの頭を追加させてください。 –
他の値(Xの機能と同様)は文字列であってはなりません –