<jupyter_start><jupyter_text>Homework 00 - Python, NumPy and Matplotlib basics<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d  # for 3d plots<jupyter_output><empty_output><jupyter_text>(1 pt) Print out all of the values in the list [1, 2, 3, 5, 6, 9, 12, 99, 4] that are less than 5.<jupyter_code>for val in [1, 2, 3, 5, 6, 9, 12, 99, 4]:
    if val < 5:
        print(val)<jupyter_output>1
2
3
4<jupyter_text>(1 pt) Repeat the above and also print both the value and it's index in the list.<jupyter_code>for index, val in enumerate([1, 2, 3, 5, 6, 9, 12, 99, 4]):
    print(index, val)<jupyter_output>0 1
1 2
2 3
3 5
4 6
5 9
6 12
7 99
8 4<jupyter_text>(1 pt) For the same list, print every third value only.<jupyter_code>alist = [1, 2, 3, 5, 6, 9, 12, 99, 4]
num_items = len(alist)
for index in range(0, num_items, 2):
    print(index, alist[index])<jupyter_output>0 1
2 3
4 6
6 12
8 4<jupyter_text>(1 pt) Assign the above list to a variable and also create a second variable that is a view of the last two elements in the list. Change the value of the two elements in the view. What happened to the original list?  (1 pt) Create variables for a list and a tuple both with the same values True, False, "Hi", "Bye", 1, 0. Assign each to a new variable name such that the new variables do NOT represent the same data as the original variables.  (1 pt) Create a dictionary with three entries that can be used to find the capitol city of three different states.<jupyter_code>capitals_dict = dict()
capitals_dict['Texas'] = 'Austin'

print(capitals_dict['Texas'])<jupyter_output>Austin<jupyter_text>(1 pt) Define a function that takes a single argument $x$ and returns the value $sqrt(x)$. Apply the function to each element in the list above and print the result.<jupyter_code>def square_root(x):
    return np.sqrt(x)
for val in [1, 2, 3, 5, 6, 9, 12, 99, 4]:
    print(val, square_root(val))
x = 2.0
x
x = int(x)
x<jupyter_output><empty_output><jupyter_text>(1 pt) Define a function that takes a single numeric argument and returns True if the number is even and False if it is odd.<jupyter_code>def is_even_or_odd(x):
    if x % 2 == 0:
        return True
    else:
        return False

result = is_even_or_odd(4)
print(result)<jupyter_output>True<jupyter_text>(1 pt) Starting at $i = 33$, write a loop that increments $i$ by 5 each time and stops once $i$ has reached 100. At the end report how many times the loop executed.  (1 pt) Write a loop over integer values from 1 to 10 that only prints prime numbers.  (1 pt) Write a function that converts Celcius to Fahrenheit.  (1 pt) Write a function that checks if a number is positive, negative or zero and prints the result. Make it so that calling the function without supplying a number reports zero.  (1 pt) Print the version of Python that you are using.  (1 pt) Generate a 10 x 10 matrix of random integers with values between 5 and 9.<jupyter_code>mat = np.random.randint(5, 10, (10, 10))

mat<jupyter_output><empty_output><jupyter_text>(1 pt) Use numpy.linspace to generate 100 equally spaced values of $x$ between and including -5 and 5. For each $x$ value, evaluate $y(x) = x^2$ and plot the result.<jupyter_code>x = np.linspace(-5, 5, 100)
y = x**2

plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y');<jupyter_output><empty_output><jupyter_text>(1 pt) For the same values of $x$ above, plot $y(x) = a x^2 + b x + c$. Choose your own values for $a$, $b$ and $c$. Overaly two plots with different values of $c$.<jupyter_code>a = 2
b = -3
c = 50
y1 = (a * (x**2)) + (b * x) + c

a = 3
b = -56
c = 3
y2 = a * x**2 + b * x + c

plt.plot(x, y1)
plt.plot(x, y2)
plt.xlabel('x')
plt.ylabel('y');<jupyter_output><empty_output><jupyter_text>(1 pt) For one of the above polynomials, compute the distance from every point (x,y) to the origin and plot those distances vs $x$.<jupyter_code>xy = np.random.rand(1000, 2)
x = xy[:, 0]
y = xy[:, 1]

dist = np.sqrt((xy**2).sum(axis=1))

plt.hist(dist, 10)
distance_from_origin = np.sqrt(x**2 + y2**2)

plt.plot(x, distance_from_origin)
plt.xlabel('x')
plt.ylabel('distance from origin');<jupyter_output><empty_output><jupyter_text>(1 pt) Generate 100 random (x, y, z) coordinates in bounded by a box with corners (0, 0, 0) and (5, 3, 1). Plot each point in a 3D graph.<jupyter_code>xyz = np.random.rand(100, 3)  # each row is an x, y, z point with random values in [0,1)

# create views into each column
x = xyz[:, 0]
y = xyz[:, 1]
z = xyz[:, 2]

# scale dimensions to fit bounding box
x *= 5
y *= 3

# plot points in 3D
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.scatter(x, y, z)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
fig.tight_layout();  # helps improve margins between plots<jupyter_output><empty_output><jupyter_text>(1 pt) Shift the above (x, y, z) coordinates so they are centered about the origin and replot.<jupyter_code>cx = x.mean()
cy = y.mean()
cz = z.mean()

x -= cx
y -= cy
z -= cz

x[0] = 100
print(xyz[0, :])

x = x - cx
x[0] = 200
print(xyz[0, :])

# plot points in 3D
# fig = plt.figure()
# ax = fig.add_subplot(1, 1, 1, projection='3d')
# ax.scatter(x, y, z)
# ax.set_xlabel('x')
# ax.set_ylabel('y')
# ax.set_zlabel('z')
# fig.tight_layout();  # helps improve margins between plots<jupyter_output>[100.           1.56494264   0.49190544]
[100.           1.56494264   0.49190544]
{'fraction_non_alphanumeric': 0.12518573551263001, 'fraction_numerical': 0.050148588410104014, 'mean_word_length': 4.202898550724638, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 47, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '435400', 'license': 'MIT', 'n_tokens_mistral': 2197, 'n_tokens_neox': 1989, 'n_words': 769, 'path': 'lecture_03_numpy_and_matplotlib/homework_00_python_basics.ipynb', 'repo_name': 'marcel-goldschen-ohm/CompNeuroSpring2019', 'size': '5384'}