<jupyter_start><jupyter_text>Lesson 6 - Regression Continuous Supervised learning- The output can be any number of values- When dealing with numbers which don't really have a order then that feature is discrete else continuous Slope and Intercept![](slope_and_intercept.png)<jupyter_code>%matplotlib inline

import random
import numpy
import matplotlib
import pickle

import matplotlib.pyplot as plt
from sklearn.cross_validation import train_test_split
from sklearn.linear_model import LinearRegression

def get_data():
    ages = pickle.load( open(projects_home + "/outliers/practice_outliers_ages.pkl", "r") )
    net_worths = pickle.load( open(projects_home + "/outliers/practice_outliers_net_worths.pkl", "r") )

    ages       = numpy.reshape( numpy.array(ages), (len(ages), 1))
    net_worths = numpy.reshape( numpy.array(net_worths), (len(net_worths), 1))

    return train_test_split(ages, net_worths, test_size=0.1, random_state=42)


def show_plot(reg):
    plt.clf()
    plt.scatter(ages_train, net_worths_train, color="b", label="train data")
    plt.scatter(ages_test, net_worths_test, color="r", label="test data")
    plt.plot(ages_test, reg.predict(ages_test), color="black")
    plt.legend(loc=2)
    plt.xlabel("ages")
    plt.ylabel("net worths")
    plt.show()

    
def ageNetWorthData():

    random.seed(42)
    numpy.random.seed(42)

    ages = []
    for ii in range(100):
        ages.append( random.randint(20,65) )
    net_worths = [ii * 6.25 + numpy.random.normal(scale=40.) for ii in ages]
    
    ### need massage list into a 2d numpy array to get it to work in LinearRegression
    ages       = numpy.reshape( numpy.array(ages), (len(ages), 1))
    net_worths = numpy.reshape( numpy.array(net_worths), (len(net_worths), 1))

    from sklearn.cross_validation import train_test_split
    ages_train, ages_test, net_worths_train, net_worths_test = train_test_split(ages, net_worths)

    return ages_train, ages_test, net_worths_train, net_worths_test

ages_train, ages_test, net_worths_train, net_worths_test = ageNetWorthData()

reg = LinearRegression()
reg.fit(ages_train, net_worths_train)
show_plot(reg)
print "net worth prediction: ", reg.predict([27])
print "slope: ", reg.coef_
print "intercept: ", reg.intercept_

print "\n stats on test dataset"
print "r-squared score: ", reg.score(ages_test, net_worths_test)

print "\n stats on train dataset"
print "r-squared score: ", reg.score(ages_train, net_worths_train)
### get Katie's net worth (she's 27)
### sklearn predictions are returned in an array, so you'll want to index into
### the output to get what you want, e.g. net_worth = predict([[27]])[0][0] (not
### exact syntax, the point is the [0] at the end). In addition, make sure the
### argument to your prediction function is in the expected format - if you get
### a warning about needing a 2d array for your data, a list of lists will be
### interpreted by sklearn as such (e.g. [[27]]).

prediction = reg.predict([[27]])

km_net_worth = prediction[0][0]

### get the slope
### again, you'll get a 2-D array, so stick the [0][0] at the end
slope = reg.coef_[0][0] ### fill in the line of code to get the right value

### get the intercept
### here you get a 1-D array, so stick [0] on the end to access
### the info we want
intercept = reg.intercept_[0] ### fill in the line of code to get the right value


### get the score on test data
test_score = reg.score(ages_test, net_worths_test) ### fill in the line of code to get the right value


### get the score on the training data
training_score = reg.score(ages_train, net_worths_train) ### fill in the line of code to get the right value<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11410675381263617, 'fraction_numerical': 0.013888888888888888, 'mean_word_length': 4.539969834087481, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '314665', 'license': 'MIT', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1207, 'n_words': 394, 'path': 'udacity_data_science_notes/intro_machine_learning/lesson_06/lesson_06.ipynb', 'repo_name': 'anshbansal/anshbansal.github.io', 'size': '3672'}