Numpy.dot TypeError:根据规则'safe',无法将数组数据从dtype('float64')转换为dtype('S32')

时间:2015-12-09 07:32:27

标签: python arrays numpy

使用variable value1 value2 x1 a b x2 a b 时,为什么会出现此错误:

np.dot(a,b.T)

a和b属于TypeError: Cannot cast array data from dtype('float64') to dtype('S32') according to the rule 'safe' 类型。我的numpy.ndarray版本是1.11.0。

2 个答案:

答案 0 :(得分:19)

刚从BrenBarn和Warren Weckesser那里获取输入,提供一个应该运行的代码片段(通过将你的字符串转换为float):

a = map(float,a)
b = map(float,b)
np.dot(a,b.T)

或更简单,如@JLT

所示
public class YourActivity extends Activity implements View.OnClickListener{

    TextView [] tvs = new TextView[N];

    public void onCreate(Bundle savedInstance){
        super.onCreate(savedInstance);
        LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        LinearLayout rootView = (LinearLayout)findViewById(R.id.your_id_of_ll);

        for (int i = 0 ; i < N ; i++){                
            tvs[i] = new TextView(this); //this or reference to activity
            tvs[i].setText("TextView " + i);
            tvs[i].setLayoutParams(lpView);
            tvs[i].setTag(i);
            tvs[i].setOnClickListener(this);
            rootView.addView(tvs[i]);

        }
    }

    public void onClick(View v) {
         int tag = (int)v.getTag();
         System.out.println("TextView with tag " + tag + " Clicked");
    }
}

但正如Warren Weckesser所说,你应该检查数组的类型,很可能已经包含浮点数。

答案 1 :(得分:5)

尝试将整个numpy数组转换为float 例如:

train = train.astype(float)
train_target = train_target.astype(float)