Given
```
import pandas as pd
this_dict = {'Ohio':35000, 'Texas':71000, 'Oregon':16000, 'Utah':5000}
baz = pd.Series(this_dict)
```
What is the output?
```
baz.index = ['the', 'quick', 'brown', 'fox']
```
Given
```
import pandas as pd
this_dict = {'Ohio':35000, 'Texas':71000, 'Oregon':16000, 'Utah':5000}
baz = pd.Series(this_dict)
```
What is the output?
```
baz.index = ['the', 'quick', 'brown', 'fox']
```
#data_question
Use `normal` to generate normally distributed steps with some mean and standard deviation.
hint: edit your `steps` filter from the preceding question, with 5000 walks at 1000 steps each, to resemble this array
```
steps = np.random.normal(loc = 0, scale = 0.25, size = (nwalks, nsteps))
```
Given 5000 random walks with 1000 steps each
How many walks reach a total of 30 steps?
Hint: return the sum of this array
```
hits30 = (np.abs(walks) >= 30).any(1)
```
Tags: #python #datascience #rstats #linux #tech
Given
```
import numpy as np
x = np.array([[1., 2., 3.], [4., 5., 6.]])
y = np.array([[6., 23.], [-1, 7], [8, 9]])
```
What is the result:
```
x.dot(y) == np.dot(x, y)
```
Tags: #python #linux #rstats #datascience #tech #ai #opensource
Given:
```
arr = np.random.randn(4, 4)
```
Replace only positive values with 2 but do nothing with negative values.
Use `np.where` and return the array if the value is not positive.
Given:
```
import numpy as np
x = np.random.randn(8)
y = np.random.randn(8)
```
Compute the square root of the sum of squared differences between x and y, and store the output in a vector called `result`.
Compute the Euclidean distance between `x` and `y`
Given:
```
import numpy as np
arr3d = np.array([[[1,2,3], [4,5,6]],[[7,8,9], [10,11,12]]])
```
What is the result:
```
old = arr3d[0].copy()
arr3d[0] = 42
print(arr3d,'\n')
arr3d[0] = old
foo = arr3d[1, 0]
bar = arr3d[1]
foo == bar[0]
```
Tags: #python #datascience #ai #tech #linux #opensource
Using NumPy:
Create a 3 x 6 array of `ones`
Create a 10 x 1 array of `zeroes`
Create a 2 x 3 x 2 `empty` array
Create an array-valued range of 15 integers
Given:
> data = [[1, 2, 3, 4], [5, 6, 7, 8]]
Create an array using the `array` function.
What is its `shape`?
What is its data type?
Tags: #python #ai #datascience
read returns a certain number of characters from the file.
The read method advances the file handle’s position by the number of bytes read.
> f.read(int(some_num_bytes))
tell gives you the current position
> f.tell()
If 10 characters is read from the file, the position would be 11 because it took that many bytes to decode 10 characters using the default encoding. You can check the default encoding in the sys module
Import the sys module and run
> sys.getdefaultencoding()
You can control the amount of context shown using the %xmode magic command, from Plain (same as the standard Python interpreter) to Verbose (which inlines function argument values and more).
you can step into the stack (using the %debug or %pdb magics) after an error has occurred for interactive post-mortem debugging.
Write a function to cast an input as a float with error handling for type and value. If it is a TypeError, return the input as a string.
Currying is computer science jargon (named after the mathematician Haskell Curry) that means deriving new functions from existing ones by partial argument application.
Having a consistent way to iterate over sequences, like objects in a list or lines in a file, is an important Python feature. This is accomplished by means of the iterator protocol, a generic way to make objects iterable. For example, iterating over a dict yields the dict keys. (Image 6)
What is happening?
Given:
> def remove_punctuation(value):
> ... return re.sub('[!#?]', '', value)
You can use functions as arguments to other functions like the built-in map function, which applies a function to a sequence of some kind.
> for state in map(remove_punctuation, states):
> ... print(state)
In your own words, what is happening in the loop?
Given https://mastodon.social/@drmorrisj/114336973556278952
and its reply
https://mastodon.social/@drmorrisj/114336978172526096 :
What makes image a bad pseudocode?
Compare it to the prior example and express your answer in your own words.
Suppose we have a list of lists containing some English and Spanish names. (Image 7)
You might have gotten these names from a couple of files and decided to organize them by language.
Now, suppose we wanted to get a single list containing all names with two or more e’s in them
Write a for loop to accomplish this task. Be mindful of computation speed
You can actually wrap this whole operation up in a single nested list comprehension. What would it look like?
Given:
> words = ['apple', 'bat', 'bar', 'atom', 'book']
> by_letter ={}
What is the output for image 2?
The setdefault dict method lets you rewrite image 2 as image 3.
Given seq1 and seq2 from Image 7 here: https://mastodon.social/@drmorrisj/114314342855860511
What is the result of image 9?
What is the order of evaluation?
Which is the faster code block in image 10?
insert is computationally expensive compared with append because references to subsequent elements have to be shifted internally to make room for the new element. If you need to insert elements at both the beginning and end of a sequence, you may wish to explore collections.deque, a double-ended queue, for this purpose.
The inverse operation to insert is pop, which removes and returns an element at a particular index.
What is the result:
> my_list.pop()
> my_list = ['foo']
Elements can be appended to the end of the list with the append method.
> my_list.append('baz')
Using insert, you can insert an element at a specific location in the list.
> my_list.insert(1, 'bar')
In common terms, what did that insert command do?
Given:
> a = 'foo'
> b = 'bar'
1. Swap the values of the two variables.
2. Swap the values of the two variables in one line.