import sys
import time
import pandas as pd
import matplotlib.pyplot as plt
from pyspark import SparkConf,SparkContext
from pyspark.mllib.classification import LogisticRegressionWithSGD
from pyspark.mllib.regression import LabeledPoint
import numpy as np
from pyspark.mllib.evaluation import BinaryClassificationMetrics
from pyspark.mllib.feature import StandardScaler
def extract_features(fleld,catedoriesMap,featureEnd):
categoryidx = catedoriesMap[fleld[3]]
categoryfeatures = np.zeros(len(catedoriesMap))
categoryfeatures[categoryidx] = 1
numericalFeatures=[convert_float(fleld) for fleld in fleld[4:featureEnd]]
return np.concatenate((categoryfeatures,numericalFeatures))
def extract_label(field):
label = field[-1]
return float(label)
def convert_float(x):
return (0 if x=="?" else float(x))
global Path
if sc.master[0:5]=='local':
Path='file:/home/swt/pythonwork/PythonProject/'
else:
Path="hdfs://localhost:9000/user/swt/"
# def prepare_data(sc):
print('load data...')
rawDataWithHeader = sc.textFile(Path+'data/train.tsv')
header = rawDataWithHeader.first()
rawData = rawDataWithHeader.filter(lambda x:x != header)
rData = rawData.map(lambda x:x.replace("\"",""))
lines = rData.map(lambda x:x.split("\t"))
print("is "+str(lines.count<
pyspark之LogisticRegression算法
最新推荐文章于 2022-10-20 19:42:22 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)