问题描述
我刚刚从Andrew Ng的deeplearning.ai Coursera课程中学到了矩阵卷积,我想尝试使用numpy进行自己的2D矩阵卷积,但是我一直遇到以下错误。 (代码底部)
我不知道我做错了什么,因为第一次迭代f = 100且w = 0,所以我不明白为什么dcast(setDT(df),g1 ~ g2,value.var = "val",fill = 0)
是一个100 x 99的矩阵。我尝试对“ horiEnd”执行+1,但改为101变为100。我也尝试过aSlice = dataColor[vertStart:vertEnd,vertStart:vertEnd]
,但这似乎也不是问题。我认为问题是在for循环之后开始的。任何帮助将不胜感激。
assert(dataColor.shape == (1200,1200))
解决方法
在循环的第一步中,
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class WallpaperCalculator implements ActionListener {
private static JTextField lengthText2;
private static JTextField heightText;
private static JButton button2;
private static JLabel total;
public static void Wallpaper() {
JPanel panel = new JPanel();
panel.setLayout(null);
JFrame frame = new JFrame();
frame.setSize(350,300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocation(555,318);
frame.add(panel);
//sets the title
JLabel title = new JLabel();
title.setBounds(10,20,300,25);
title.setText("Wallpaper Calculator");
title.setForeground(Color.BLUE);
panel.add(title);
//sets the lengths title
JLabel length = new JLabel();
length.setBounds(10,55,25);
length.setText("Length Input");
panel.add(length);
//creates a field for the length text input
lengthText2 = new JTextField();
lengthText2.setBounds(120,90,25);
panel.add(lengthText2);
//creates a width text label
JLabel height = new JLabel();
height.setBounds(10,80,25);
height.setText("height Input");
panel.add(height);
//created a field for the width text input
heightText = new JTextField();
heightText.setBounds(120,25);
panel.add(heightText);
//makes a field to set a text box so people can see where the total will be
JLabel totalAmount = new JLabel();
totalAmount.setBounds(10,150,25);
totalAmount.setText("Total Amount:");
panel.add(totalAmount);
//makes a field to present the answer
total = new JLabel();
total.setBounds(125,25);
panel.add(total);
//makes a button to get the two lengths and start the multiplication process
button2 = new JButton();
button2.setBounds(10,130,25);
button2.setText("Find");
button2.setForeground(Color.blue);
button2.addActionListener(new TrimCalculator());
panel.add(button2);
frame.setVisible(true);
}
//gets the button and changes it from a string into a int so it can be calculated
@Override
public void actionPerformed(ActionEvent c) {
String lengthAm = lengthText2.getText();
String heightAm = heightText.getText();
//changes the strings into a double
double length = Double.parseDouble(lengthAm);
double height = Double.parseDouble(heightAm);
// multiplies the two sides
double sidesTotal = length * height;
//divides the sidesTotal to get Total
double Total = sidesTotal / 40;
System.out.println(Total);
//changes the double variable newTotal into a string
String f = String.valueOf(Total);
//makes the button actually print the amount of trim needed
if (c.getSource() == button2){
total.setText(f+" ft");
}
}
}
这一切都很好,并且按预期运行。
但是进入第1,100步(11,101)
pip3 --install --upgrade pip
pip3 install numpy pytest
python -m pytest test.py
现在,您可以使用h = 0,vertEnd = 100
w = 0,horiEnd = 100
aSlice.shape = (100,100)
将形状设置为超出数据范围:
h = 1100,vertEnd = 1200 # Note the index is 1200,which requires length of 1201
w = 0,horiEnd = 100
在这里
vertEnd = 1200