Please solve the numpy problem mentioned in the body - numpy

Write a Program in Python, which accepts an numpy array of integer and divide all those array elements
by 7 which are divisible by 7 and multiply other array elements by 3.

import numpy as np
def func(array):
return np.array([item if item%7 == 0 else item*3 for item in arr ])
arr = np.array([1,7,7,4,14,21,5]) #example
func(arr) import numpy as np
arr = np.array([1,7,7,4,14,21,5]) #example
result = np.array([item if item%7 == 0 else item*3 for item in arr ])

Related

scipy convert coo string directly to numpy matrix

I already have a string in coo matrix format(row, col, value):
0 0 -1627.761282
0 1 342.811259
0 2 342.811259
0 3 171.372276
0 4 342.744553
0 5 342.744553
Now I want to convert my string directly to numpy matrix. Currently I have to write my string to file, then create a numpy matrix from file:
from scipy.sparse import coo_matrix
import numpy as np
with open("Output.txt", "w") as text_file:
text_file.write(matrix_str)
text = np.loadtxt( 'Output.txt', delimiter=' ' , dtype=str)
rows,cols,data = text.T
matrix = coo_matrix((data.astype(float), (rows.astype(int), cols.astype(int)))).todense()
How can I convert my string directly to numpy matrix without writing to file ? Please help
You could use StriongIO as follows.
import numpy as np
from scipy.sparse import coo_matrix
import io
with io.StringIO(matrix_str) as ss:
rows, cols, data = np.loadtxt(ss).T
matrix = coo_matrix((data.astype(float), (rows.astype(int), cols.astype(int)))).todense()

Convert numpy array of shape x,y to a list of size x with every element a numpy array of shape (y,)

So if the shape is 100,10
the list should have 100 elements. Each element is a numpy array of shape (10,)
Anything wrong with np.split?
Here is a quick example:
import numpy as np
some_array = np.random.rand(100, 10)
list_of_arrays = [np.squeeze(x, axis=0) for x in np.split(some_array, len(some_array))]
# some assertions that it does what you ask
assert len(list_of_arrays) == 100
assert isinstance(list_of_arrays, list)
assert all([isinstance(x, np.ndarray) for x in list_of_arrays])
assert all([x.shape == (10,) for x in list_of_arrays])
If you are okay with a shape of (1, 10) you can save the list comprehension:
import numpy as np
some_array = np.random.rand(100, 10)
list_of_arrays = np.split(some_array, len(some_array))

how do you reverse the sign of elements in an axis of a numpy array

Is there any way to reverse the sign (postive=negative, negative=positive) of each individual element of a numpy array without iterating through the array?
An easy solution would be to multiple your numpy array with -1.
For example:
data = np.array([1,2,3,4,-1,-2,-3,-4])
print(data)
>> array([1,2,3,4,-1,-2,-3,-4])
data = data * -1
print(data)
>> array([-1,-2,-3,-4, 1,2,3,4]
Get the axis you want and mutliply it by -1.
Exemple :
import numpy as np
arr = np.array([[1,-2],[-3,4]])
arr[0,:] = arr[0,:] *-1

Operation in Numpy multi-dimensional array

I have two multi-dimensional numpy array. I would like to convert the entry in the second array to NaN, if the corresponding element in first is zero. Below is example to manually mimic the same: (Can this be done programmatically)
import numpy as np
a = np.random.rand(4,5)
a[0][0] = 0
a[1][0] = 0
a[1][1] = 0
b = np.random.rand(4,5)
b[0][0] = np.nan
b[1][0] = np.nan
b[1][1] = np.nan
Can we use masking here?
Write it like you say it:
b[a==0] = np.nan

Using pdsit with string value in python scipy

I have a following code and I want to calculate the hamming strings of the strings:
from pandas import DataFrame
import numpy as np
import pandas as pd
from scipy.spatial.distance import pdist, squareform
df = pd.read_csv("3d_printing.csv", encoding='utf-8', error_bad_lines=False, low_memory=False, names=['file_name', 'phash', 'dhash', 'file_date'])
def hamming_distance(s1, s2):
if len(s1) != len(s2):
raise ValueError("Undefined for sequences of unequal length")
return sum(el1 != el2 for el1, el2 in zip(s1, s2))
df.sort_values(by='file_date', ascending=0)
x = pd.DataFrame(np.triu(squareform(pdist(df[['phash']], hamming_distance))),
columns=df.file_name.str.split('_').str[0],
index=df.file_name.str.split('_').str[0]).replace(0, np.nan)
z = x[x.apply(lambda col: col.index != col.name)].max(1).max(level=0)
z.to_csv("3d_printing_x.csv", mode='a')
When I run the code I get
ValueError: could not convert string to float: '002889898888b8a9'
I know that pdist requires float values, but at this point I don't know what to do