Python Forum
Python changed array shape without telling me
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python changed array shape without telling me
#1
I was trying to write some simple code to practice using the linear regression in sklearn. For God knows what reason, LinearRegression.fit(x,y) expects x to be a 2d array and y to be a 1d array. That means if x is actually has 1d data, it needs to be expressed with:

[[
.
.
.
numbers
.
.
.
]]

Note that it has 2 sets of brackets, not just 1 set of brackets.

I wrote a for loop to delete nan values and had a line of code

x = np.delete(x,i)

Apparently this shitty line of code changed x to a 1d array without telling me! Then my code wouldn't work and I didn't know why! If there is a syntax error in that code, I want to be told! I don't want it to be fixed without telling me!

I fixed it by writing the line

x = np.reshape(x,(x.size,1))


Python tries to help me like this a lot and it makes me mad. I hate that it loops back around when indices are negative, instead of giving me an out of bounds error. I hate that it will automatically fix the type for me instead of giving me an error. It makes it so much harder to find the source of the problem. I learned C++ before python, and so far it seems to me that C++ is superior in all respects. It is faster, you can do more things with it (try writing for(int i = 10; i > -1; i-=2){} in python), and all of the user friendliness of python (such as the existence of lists) could be added to C with some libraries. I hate python so much right now. This project should have taken 30 minutes and it ended up taking 3 hours because python kept trying to help me without telling me what it had done. I know if I had known what I was doing it wouldn't have taken that long, but I hadn't used sklearn on my own before and I hadn't used matlab in a few years, so I checked lots of other things first before realizing that python had done something in secret without telling me again.
Reply
#2
please show your code
Reply
#3
Got your vent. It is simpler than you think.
for i in range(10,-2,-2):
Reply
#4
The behaviour of a package written by someone is not the fault of the language.

Did you read the Docs
https://numpy.org/doc/1.18/reference/gen...elete.html Wrote:numpy.delete
numpy.delete(arr, obj, axis=None)[source]
Return a new array with sub-arrays along an axis deleted. For a one dimensional array, this returns those entries not returned by arr[obj].

Parameters
arrarray_like
Input array.

objslice, int or array of ints
Indicate indices of sub-arrays to remove along the specified axis.

axisint, optional
The axis along which to delete the subarray defined by obj. If axis is None, obj is applied to the flattened array.

Returns
outndarray
A copy of arr with the elements specified by obj removed. Note that delete does not occur in-place. If axis is None, out is a flattened array.

See also
insert
Insert elements into an array.

append
Append elements at the end of an array.

Notes

Often it is preferable to use a boolean mask. For example:

>>>
arr = np.arange(12) + 1
mask = np.ones(len(arr), dtype=bool)
mask[[0,2,4]] = False
result = arr[mask,...]
Is equivalent to np.delete(arr, [0,2,4], axis=0), but allows further use of mask.

Examples

>>>
arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
np.delete(arr, 1, 0)
array([[ 1,  2,  3,  4],
       [ 9, 10, 11, 12]])
>>>
np.delete(arr, np.s_[::2], 1)
array([[ 2,  4],
       [ 6,  8],
       [10, 12]])
np.delete(arr, [1,3,5], None)
array([ 1,  3,  5,  7,  8,  9, 10, 11, 12])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issues with Shape/Reshape for CNN moddy10 0 1,452 Oct-12-2021, 03:54 PM
Last Post: moddy10
  cannot reshape array of size 0 into shape Roro 2 6,243 Jun-14-2020, 11:28 AM
Last Post: Roro
  Bad input shape for SVC Scott 1 4,598 Dec-07-2019, 08:03 AM
Last Post: scidam
  ValueError: could not broadcast input array from shape (75) into shape (25) route2sabya 0 6,474 Mar-14-2019, 01:14 PM
Last Post: route2sabya

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020