<jupyter_start><jupyter_text>Chapter 2: Basic Operations  Vectors, Data, Matrices and Subsetting<jupyter_code>from __future__ import division
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.array([2,7,5])    # explicit vector creation
x
y = np.arange(4, 13, 3)  # vector creation from a sequence (start, stop, step)
y
x + y    # vectors can be added
x / y    # divided
x ** y    # exponentiated
x[1]    # vector elements can be selected by position
x[1:3]  # multiple elements can be selected using slices
x[-2]  # elements can be specified as offset from end
x[np.array([0,1])]  # elements can be specified as an array
Z = np.matrix(np.arange(1,13)).reshape((4, 3))
Z                 # note: R arranges the elements column-wise
Z[2:4, 1:3]    # R is 1-based and includes ending index, Python is 0 based and does not.
Z[:, 1:3]    # column slice
Z.shape<jupyter_output><empty_output><jupyter_text>Generating Random Data, Graphics<jupyter_code>x = np.random.uniform(0.0, 1.0, 50)
x
y = np.random.normal(0.0, 1.0, 50)
y
fig, ax = plt.subplots()
plt.scatter(x, y)
fig, ax = plt.subplots()
plt.xlabel("Random Uniform")
plt.ylabel("Random Normal")
plt.scatter(x, y, marker='o', color='red')  # plot customizations
plt.subplot(121)    # parameter indicates 1 rows, 2 col, first figure
plt.scatter(x, y)
plt.subplot(122)
plt.hist(y)<jupyter_output><empty_output><jupyter_text>Reading in data<jupyter_code># data comes from ISL book site: http://www-bcf.usc.edu/~gareth/ISL/data.html
auto_df = pd.read_csv("../data/Auto.csv")
auto_df.columns     # column names
auto_df.shape    # number of rows, number of columns
type(auto_df)
auto_df.describe()  # equivalent of R's DataFrame.summary()
plt.ylabel("MPG")
auto_df.plot(x="cylinders", y="mpg", style='o')
auto_df.boxplot(column="mpg", by="cylinders") # MPG distribution by number of cylinders
# similar to R pairs, shows correlation scatter plots between columns and distribution for each 
# column along the diagonal.
# The R version that uses formulas does not seem to have a Python equivalent (and doesn't seem
# to be very useful for exploratory analysis IMO).
axes = pd.tools.plotting.scatter_matrix(auto_df, color="brown")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12099644128113879, 'fraction_numerical': 0.02090747330960854, 'mean_word_length': 4.679292929292929, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '31510', 'license': 'MIT', 'n_tokens_mistral': 819, 'n_tokens_neox': 767, 'n_words': 249, 'path': 'src/chapter2.ipynb', 'repo_name': 'ChrisMichael11/statlearning', 'size': '2248'}