def main(): gnb = GNB() with open('train.json', 'rb') as f: j = json.load(f) #print j.keys() X = j['states'] Y = j['labels'] #print len(X), len(Y) print "-----start training-------" gnb.train(X, Y) with open('test.json', 'rb') as f: j = json.load(f) X = j['states'] Y = j['labels'] score = 0 for coords, label in zip(X,Y): predicted = gnb.predict(coords) if predicted == label: score += 1 fraction_correct = float(score) / len(X) print "You got {} percent correct".format(100 * fraction_correct)
def person_test(): # Create an empty dataframe data = pd.DataFrame() # Create our target variable data['Gender'] = [ 'male', 'male', 'male', 'male', 'female', 'female', 'female', 'female' ] # Create our feature variables data['Height'] = [6, 5.92, 5.58, 5.92, 5, 5.5, 5.42, 5.75] data['Weight'] = [180, 190, 170, 165, 100, 150, 130, 150] data['Foot_Size'] = [12, 11, 12, 10, 6, 8, 7, 9] # Create an empty dataframe person = pd.DataFrame() # Create some feature values for this single row person['Height'] = [6] person['Weight'] = [130] person['Foot_Size'] = [8] gnb = GNB() gnb.train(data, "Gender") for idx, row in person.iterrows(): prob_label, prob = gnb.predict(row) print("Probability Label: %s --> Probability: %s" % (prob_label, prob))
def main(): gnb = GNB() with open('train.json', 'r') as f: j = json.load(f) print(j.keys()) X = j['states'] Y = j['labels'] gnb.train(X, Y) skl_nb = GaussianNB() LE = LabelEncoder() skl_nb.fit(X, LE.fit_transform(Y)) with open('test.json', 'r') as f: j = json.load(f) X = j['states'] Y = j['labels'] score = 0 for coords, label in zip(X, Y): predicted = gnb.predict(coords) if predicted == label: score += 1 fraction_correct = float(score) / len(X) print("You got {0:3.2f} percent correct".format(100 * fraction_correct)) Y_test = LE.transform(Y) Y_pred = skl_nb.predict(X) skl_fraction_correct = sum([1 for a, b in zip(Y_test, Y_pred) if a == b ]) / float(len(Y_test)) print("Sklearn got {0:3.2f} percent correct".format(100 * skl_fraction_correct))
def main(): # Load training data with open('train.json', 'r') as f: j = json.load(f) X = j['states'] Y = j['labels'] # Train classifier gnb = GNB() gnb.train(X, Y) # Open test data with open('test.json', 'r') as f: j = json.load(f) X = j['states'] Y = j['labels'] # Evaluate classifier score = 0 for coords, label in zip(X, Y): predicted = gnb.predict(coords) if predicted == label: score += 1 fraction_correct = float(score) / len(X) print("You got {} percent correct".format(100 * fraction_correct))
def main(): gnb = GNB() with open('train.json', encoding='utf-8') as f: j = json.load(f) print(j.keys()) X = j['states'] Y = j['labels'] gnb.train(X, Y) with open('test.json', encoding='utf-8') as f: j = json.load(f) X = j['states'] Y = j['labels'] score = 0 for coords, label in zip(X,Y): predicted = gnb.predict(coords) if predicted == label: score += 1 percent_correct = 100 * float(score) / len(X) print(str(percent_correct) + " percent correct.")
def main(): gnb = GNB() train_df = convert_to_labels(pd.read_json('train.json')) test_df = convert_to_labels(pd.read_json('test.json')) gnb.train(train_df, 'labels') for idx, row in test_df.iterrows(): prediction_label, prob = gnb.predict(row) print("Predicted: %s Actual: %s" % (prediction_label, row['labels']))
def main(): gnb = GNB() with open('train.json', 'r') as f: j = json.load(f) X = j['states'] Y = j['labels'] gnb.train(X, Y) with open('test.json', 'r') as f: j = json.load(f) X = j['states'] Y = j['labels'] score = 0 for coords, label in zip(X, Y): predicted = gnb.predict(coords) if predicted == label: score += 1 fraction_correct = float(score) / len(X) print("You got %.2f percent correct" % (100 * fraction_correct))
def main(): # TRAINING gnb = GNB() with open('train.json', 'r') as f: j = json.load(f) #print(j.keys()) ''' X - array of N observations - Each observation is a tuple with 4 values: s, d, s_dot and d_dot. ''' X = j['states'] X = np.array(X) Y = j['labels'] Y = np.array(Y) # d-values seem biased where 0 is the center of the left lane. X[:, 1] = X[:, 1] + 2 # just feed delta d to classifier #X = np.array([X[:,3]]).T # feed d and delta_d into classifier X = np.vstack((X[:, 2], X[:, 3])).T gnb.train(X, Y) # CLASSIFICATION with open('test.json', 'r') as f: j = json.load(f) X = j['states'] X = np.array(X) Y = j['labels'] Y = np.array(Y) X[:, 1] = X[:, 1] + 2 #X = np.array([X[:,3]]).T X = np.vstack((X[:, 2], X[:, 3])).T score = 0 for coords, label in zip(X, Y): predicted = gnb.predict(coords) if predicted == label: score += 1 fraction_correct = float(score) / len(X) print("You got {} percent correct".format(100 * fraction_correct))
def main(): clf = GNB() with open('train.json', encoding='utf-8') as f: j = json.load(f) print(j.keys()) X = j['states'] Y = j['labels'] clf.train(X, Y) with open('test.json', encoding='utf-8') as f: j = json.load(f) X = j['states'] Y = j['labels'] score = 0 for coords, label in zip(X, Y): predicted = clf.predict(coords) if predicted == label: score += 1 fraction_correct = float(score) / len(X) print("You got {} percent correct".format(100 * fraction_correct))
def main(): gnb = GNB() with open('train.json', 'rb') as f: j = json.load(f) print("JSON file keys =", j.keys()) X = j['states'] Y = j['labels'] gnb.train(X, Y) with open('test.json', 'rb') as f: j = json.load(f) X = j['states'] Y = j['labels'] score = 0 for coords, label in zip(X,Y): predicted = gnb.predict(coords) print('pred=',predicted,' label=',label) if predicted == label: score += 1 fraction_correct = float(score) / len(X) print("You got {} percent correct".format(100 * fraction_correct))